-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
216 lines (161 loc) · 4.24 KB
/
queue.c
File metadata and controls
216 lines (161 loc) · 4.24 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Queue capacity
#define CAPACITY 100
int queue[CAPACITY];
unsigned int size = 0;
unsigned int rear = CAPACITY - 1; // Initally assumed that rear is at end
unsigned int front = 0;
/* Function declaration for various operations on queue */
int enqueue(int data);
int dequeue();
int isFull();
int isEmpty();
int getRear();
int getFront();
int main()
{
int ch, data;
/* Run indefinitely until user manually terminates */
while (1)
{
/* Queue menu */
printf("--------------------------------------\n");
printf(" QUEUE ARRAY IMPLEMENTATION PROGRAM \n");
printf("--------------------------------------\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("4. Get Rear\n");
printf("5. Get Front\n");
printf("0. Exit\n");
printf("--------------------------------------\n");
printf("Select an option: ");
scanf("%d", &ch);
/* Menu control switch */
switch (ch)
{
case 1:
printf("\nEnter data to enqueue: ");
scanf("%d", &data);
// Enqueue function returns 1 on success
// otherwise 0
if (enqueue(data))
printf("Element added to queue.");
else
printf("Queue is full.");
break;
case 2:
data = dequeue();
// on success dequeue returns element removed
// otherwise returns INT_MIN
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Data => %d", data);
break;
case 3:
// isEmpty() function returns 1 if queue is emtpy
// otherwise returns 0
if (isEmpty())
printf("Queue is empty.");
else
printf("Queue size => %d", size);
break;
case 4:
if (isEmpty())
printf("Queue is empty.");
else
printf("Rear => %d", getRear());
break;
case 5:
if (isEmpty())
printf("Queue is empty.");
else
printf("Front => %d", getFront());
break;
case 0:
printf("Exiting from app.\n");
exit(0);
default:
printf("Invalid choice, please input number between (0-5).");
break;
}
printf("\n\n");
}
}
/**
* Enqueue/Insert an element to the queue.
*/
int enqueue(int data)
{
// Queue is full throw Queue out of capacity error.
if (isFull())
{
return 0;
}
// Ensure rear never crosses array bounds
rear = (rear + 1) % CAPACITY;
// Increment queue size
size++;
// Enqueue new element to queue
queue[rear] = data;
// Successfully enqueued element to queue
return 1;
}
/**
* Dequeue/Remove an element from the queue.
*/
int dequeue()
{
int data = INT_MIN;
// Queue is empty, throw Queue underflow error
if (isEmpty())
{
return INT_MIN;
}
// Dequeue element from queue
data = queue[front];
// Ensure front never crosses array bounds
front = (front + 1) % CAPACITY;
// Decrease queue size
size--;
return data;
}
/**
* Checks if queue is full or not. It returns 1 if queue is full,
* overwise returns 0.
*/
int isFull()
{
return (size == CAPACITY);
}
/**
* Checks if queue is empty or not. It returns 1 if queue is empty,
* otherwise returns 0.
*/
int isEmpty()
{
return (size == 0);
}
/**
* Gets, front of the queue. If queue is empty return INT_MAX otherwise
* returns front of queue.
*/
int getFront()
{
return (isEmpty())
? INT_MIN
: queue[front];
}
/**
* Gets, rear of the queue. If queue is empty return INT_MAX otherwise
* returns rear of queue.
*/
int getRear()
{
return (isEmpty())
? INT_MIN
: queue[rear];
}