-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularque.c
177 lines (170 loc) · 2.9 KB
/
circularque.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<stdio.h>
#define MAX_SIZE 5
int front = -1;
int rear = -1;
int queue[MAX_SIZE];
void enqueue(int item)
{
if(front==(rear+1)%MAX_SIZE)
{
printf("The queue is full....Cannot insert more elements :(\n");
}
else
{
if(front == -1)
{
front = 0;
}
rear = (rear+1)%MAX_SIZE;
queue[rear] = item;
}
}
void dequeue()
{
int del_item;
if(front==-1)
{
printf("The queue is empty\n");
}
else
{
del_item = queue[front];
printf("The item %d was deleted\n",del_item);
if(front==rear)
{
front = -1;
rear = -1;
}
else
{
front = (front+1)%MAX_SIZE;
}
}
}
void display()
{
if(front==-1)
{
printf("Queue is empty\n");
}
else
{
printf("The current queue is :");
int i=front;
while(i!=rear)
{
printf("%d ",queue[i]);
i=(i+1)%MAX_SIZE;
}
printf("%d\n",queue[rear]);
}
}
void main()
{
int i=0;
while(i==0)
{
int ch,item;
printf("1--Enqueue\n2--Dequeue\n3--Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the no to be inserted : ");
scanf("%d",&item);
enqueue(item);
display();
}
if(ch==2)
{
dequeue();
if(front!=-1)
{
display();
}
}
if(ch==3)
{
printf("Exiting.....\n");
break;
}
}
}
/*
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 5
The current queue is :5
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 7
The current queue is :5 7
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 3
The current queue is :5 7 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 8
The current queue is :5 7 3 8
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 3
The current queue is :5 7 3 8 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 1
Enter the no to be inserted : 7
The queue is full....Cannot insert more elements :(
The current queue is :5 7 3 8 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The item 5 was deleted
The current queue is :7 3 8 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The item 7 was deleted
The current queue is :3 8 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The item 3 was deleted
The current queue is :8 3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The item 8 was deleted
The current queue is :3
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The item 3 was deleted
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 2
The queue is empty
1--Enqueue
2--Dequeue
3--Exit
Enter your choice: 3
Exiting.....
*/