-
Notifications
You must be signed in to change notification settings - Fork 44
/
Queue_using_array.c
77 lines (68 loc) · 1.44 KB
/
Queue_using_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<malloc.h>
#include<limits.h>
//structure of Queue
struct queue
{
int front,rear,size;
unsigned capacity;
int *array;
};
struct queue *create(unsigned capacity)
{
struct queue *Queue=(struct queue*)malloc(sizeof(struct queue));
Queue->capacity=capacity;
Queue->front=Queue->size=0;
Queue->rear=capacity-1;
Queue->array=(int*)malloc(capacity*sizeof(int));
return Queue;
}
int isEmpty(struct queue *Queue)
{
return(Queue->size==0);
}
int isFULL(struct queue *Queue)
{
return(Queue->size==Queue->capacity);
}
void enqueue(struct queue *Queue,int n)
{
if(isFull(Queue))
return;
Queue->rear=(Queue->rear+1)%Queue->capacity;
Queue->array[Queue->rear]=n;
Queue->size=Queue->size+1;
printf("Enqueued Element%d",n);
}
int dequeue(struct queue *Queue)
{
if (isEmpty(Queue))
return 0;
int item=Queue->array[Queue->front];
Queue->front=(Queue->front+1)%Queue->capacity;
Queue->size=Queue->size-1;
return(item);
}
int front(struct queue *Queue)
{
int item=Queue->array[Queue->front];
return(item);
}
int rear( struct queue *Queue)
{
int item=Queue->array[Queue->rear];
return(item);
}
int main()
{
struct queue *Queue=create(50);
enqueue(Queue,12);
enqueue(Queue,13);
enqueue(Queue,14);
enqueue(Queue,15);
enqueue(Queue,16);
dequeue(Queue);
printf("Element At front %d\n\n",front(Queue));
printf("Element At Rear%d",rear(Queue));
return 0;
}