-
Notifications
You must be signed in to change notification settings - Fork 1
/
15_circular_queue.c
66 lines (63 loc) · 1.03 KB
/
15_circular_queue.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
#include<stdio.h>
int a[100];
int maxsize=3;
int front=-1,rear=-1;
void enqueue(int n){
if(front==(rear+1)%maxsize)
printf(" Queue Overflow \n");
else{
if(front==-1)
front=0;
rear=(rear+1)%maxsize;
a[rear]=n;
}
}
void dequeue(){
if( rear==front&&rear==-1)
printf(" Queue Underflow\n");
else if(front==rear){
printf(" Element %d Popped \n", a[ front]);
front=rear=-1;
}
else{
printf(" Element %d Popped \n", a[ front]);
a[front]=0;
( front)=(front+1)%maxsize;
}
}
void display(){
int i;
if( rear==front&&rear==-1)
printf(" Empty Queue\n");
else{
printf(" Queue : ");
for(i=front;;i=(i+1)%maxsize)
{
printf("%d ", a[i]);
if(i==rear)
break;
}
printf("\n");
}
}
int main(){
int o;
while(1){
printf(" Enter Option\n\t1. Enqueue\n\t2. Display\n\t3. Dequeue\n\t4. Exit \n ");
scanf("%d",&o);
if(o==1){
printf(" enter element : ");
int temp;
scanf("%d",&temp);
enqueue(temp);
}
else if(o==2){
display();
}
else if (o==3){
dequeue();
}
else
break;
}
}