-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCircularQueue.c
85 lines (71 loc) · 1.61 KB
/
CircularQueue.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
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int front=0, rear=-1, size;
void enqueue(int Arr[]){
int a;
printf("Enter: ");
scanf("%d",&a);
if( !( (front==0&&rear==size-1) || (front>0&&rear==front-1) ) ){
rear = (rear+1)%size;
Arr[rear] = a;
}
}
int dequeue(int Arr[]){
int a = INT_MIN;
if (front == rear)
{
a = Arr[front];
front = 0;
rear = -1;
}
else if(!(front==0&&rear==-1)){
a = Arr[front];
front = (front +1) % size;
}
return a;
}
void display_elements(int Arr[]){
if (!(front==0&&rear==-1))
{
int diff = (rear-front+1+size)%size, i = front;
if (diff == 0) {
diff = size;
}
printf("%d\n", diff);
while(diff > 0){
printf("%d ",Arr[i] );
i = (i+1) %size;
diff--;
}
}
}
int main(int argc, char const *argv[])
{
int loop = 1;
printf("Size: \n");
scanf("%d",&size);
int * Arr = (int *) (malloc(size*sizeof(int)));
while(loop == 1)
{
int choice,a;
printf("Enter the choice 1-Enqueue, 2-Dequeue, 3-Display, 4-Stop\n");
scanf("%d",&choice);
switch(choice){
case 1:
enqueue(Arr);
break;
case 2:
a = dequeue(Arr);
printf("%d\n", a);
break;
case 3:
display_elements(Arr);
break;
case 4:
loop = 0;
break;
}
}
return 0;
}