-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
112 lines (94 loc) · 2.13 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
int front = -1;
int rear = -1;
int queue[10];
int size = sizeof(queue)/sizeof(queue[0]);
void enqueue(int val){
// overflow
if((rear+1)%size==front){
printf("Overflow");
}
if(front==-1 && rear==-1){
front = 0;
rear=0;
queue[rear]=val;
}
else{
rear = (rear+1)%size;
queue[rear]=val;
}
}
int dequeue(){
//underflow
int ele;
if(front==-1 && rear==-1){
printf("Underflow!!!!!");
}
if(front==rear){
ele = queue[front];
front=-1;
rear=-1;
printf(" The deleted element is : %d\n",ele);
return ele;
}
else{
ele = queue[front];
printf(" The deleted element is : %d\n",ele);
front = (front + 1)%size;
return ele;
}
}
void show(){
printf("The element in queue is : \n");
// FIRST APPROACH
// if(front==-1){
// printf("Stack is empty");
// }
// // printf("\n");
// else{
// if (rear >= front){
// for (int i = front; i <= rear; i++)
// printf("%d \t",queue[i]);
// }
// else{
// for (int i = front; i < size; i = i++)
// printf("%d \t", queue[i]);
// for (int i = 0; i <= rear; i++)
// printf("%d ", queue[i]);
// }
// }
// SECONF APPROACH
int i = front;
while(i!=rear){
printf("%d\t",queue[i]);
i = (i+1)%size;
}
printf("%d\t",queue[rear]);
printf("\n");
}
int main(){
for(int i=1 ;i<=10;i++){
enqueue(i*3);
}
show();
printf("F = %d\n",front);
printf("R = %d\n",rear);
for(int i=1 ;i<=10;i++){
printf(" %d: ",i);
dequeue();
}
enqueue(6);
printf("F = %d\n",front);
printf("R = %d\n",rear);
show();
printf("F = %d\n",front);
printf("R = %d\n",rear);
dequeue();
printf("F = %d\n",front);
printf("R = %d\n",rear);
show();
// dequeue();
// dequeue();
// dequeue();
// show();
}