-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_Circular_ArrayV2
251 lines (188 loc) · 5.71 KB
/
Queue_Circular_ArrayV2
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
The queue is considered as a circular queue when the positions 0 and MAX-1 are adjacent. Any position before front is also after rear.
See the logical circularity of the queue. Addition causes the increment in REAR. It means that when REAR reaches MAX-1 position then Increment in REAR causes REAR to reach at first position that is 0.
if( rear == MAX -1 )
rear = 0;
else
rear = rear + 1;
The short-hand equivalent representation may be
rear = ( rear + 1) % MAX;
As we know that, Deletion causes the increment in FRONT. It means that when FRONT reaches the MAX-1 position, then increment in FRONT, causes FRONT to reach at first position that is 0.
if( front == MAX -1 )
front = 0;
else
front = front + 1;
The short-hand equivalent representation may be
front = ( front + 1) % MAX;
Drawback of Circular Queue
The drawback of circular queue is , difficult to distinguished the full and empty cases. It is also known as boundary case problem.
In circular queue it is necessary that:
Before insertion, fullness of Queue must be checked (for overflow).
Before deletion, emptiness of Queue must be checked (for underflow).
Condition to check FULL Circular Queue
if( ( front == MAX-1) || ( front ==0 && rear == MAX -1 ) )
Condition to check EMPTY Circular Queue
if( ( front == MAX-1) || ( front ==0 && rear == MAX -1 ) )
Solution of the drawback of circular Queue
Use count variable to hold the current position ( in case of insertion or deletion).
Operation of Circular Queue using count
Initialize Operation.
Is_Full check.
Is_Empty check.
Addition or Insertion operation.
Deletion operation.
Algorithms
INIT(QUEUE,FRONT,REAR,COUNT)
INSERT-ITEM(QUEUE, FRONT, REAR, MAX, COUNT, ITEM)
REMOVE-ITEM(QUEUE, FRONT, REAR, COUNT, ITEM)
FULL-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,FULL)
EMPTY-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,EMPTY)
INIT(QUEUE,FORNT,REAR,COUNT)
This algorithm is used to initialize circular queue.
1. FRONT := 1;
2. REAR := 0;
3. COUNT := 0;
4. Return;
INSERT-ITEM( QUEUE, FRONT, REAR, MAX, COUNT, ITEM)
This algorithm is used to insert or add item
into circular queue.
1. If ( COUNT = MAX ) then
a. Display “Queue overflow”;
b. Return;
2. Otherwise
a. If ( REAR = MAX ) then
i. REAR := 1;
b. Otherwise
i. REAR := REAR + 1;
c. QUEUE(REAR) := ITEM;
d. COUNT := COUNT + 1;
3. Return;
REMOVE-ITEM( QUEUE, FRONT, REAR, COUNT, ITEM)
This algorithm is used to remove or delete item
from circular queue.
1. If ( COUNT = 0 ) then
a. Display “Queue underflow”;
b. Return;
2. Otherwise
a. ITEM := QUEUE(FRONT)l
b. If ( FRONT =MAX ) then
i. FRONT := 1;
c. Otherwise
i. FRONT := FRONT + 1;
d. COUNT := COUNT + 1;
3. Return;
EMPTY-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,EMPTY)
This is used to check queue is empty or not.
1. If( COUNT = 0 ) then
a. EMPTY := true;
2. Otherwise
a. EMPTY := false;
3. Return ;
FULL-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,FULL)
This algorithm is used to check queue is full or not.
1. If ( COUNT = MAX ) then
a. FULL := true;
2. Otherwise
a. FULL := false;
3. Return ;
#include <stdio.h>
#define MAX 5
/*Declaration of circular queue.*/
typedef struct CirQueue
{
int front ;
int rear ;
int count ;
int ele[MAX] ;
}CirQueue;
/*Initailization of circular queue.*/
void initCirQueue(CirQueue * q)
{
q->front = 0;
q->rear = -1;
q->count = 0;
}
/*Check Queue is full or not*/
int isFull(CirQueue * q)
{
int full=0;
if(q->count == MAX)
full = 1;
return full;
}
/*Check Queue is empty or not*/
int isEmpty(CirQueue * q)
{
int empty=0;
if(q->count == 0)
empty = 1;
return empty;
}
/*To insert item into circular queue.*/
void insertCirQueue(CirQueue * q, int item)
{
if( isFull(q) )
{
printf("\nQueue Overflow");
return;
}
q->rear = (q->rear+1)%MAX;
q->ele[q->rear] = item;
q->count++;
printf("\nInserted item : %d",item);
}
/*To delete item from queue.*/
int deleteCirQueue(CirQueue * q, int *item)
{
if( isEmpty(q) )
{
printf("\nQueue Underflow");
return -1;
}
*item = q->ele[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return 0;
}
int main()
{
int item=0;
CirQueue q;
initCirQueue(&q);
insertCirQueue(&q, 10);
insertCirQueue(&q, 20);
insertCirQueue(&q, 30);
insertCirQueue(&q, 40);
insertCirQueue(&q, 50);
insertCirQueue(&q, 60);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
insertCirQueue(&q, 60);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);
printf("\n");
return 0;
}
Inserted item : 10
Inserted item : 20
Inserted item : 30
Inserted item : 40
Inserted item : 50
Queue Overflow
Deleted item is : 10
Deleted item is : 20
Deleted item is : 30
Deleted item is : 40
Deleted item is : 50
Inserted item : 60
Deleted item is : 60
Queue Underflow