-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.c
253 lines (173 loc) · 4.61 KB
/
3.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
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
252
253
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define CAPACITY 100 // Queue max capacity
typedef struct node
{
int data;
struct node * next;
} Queue; // Queue is a typedef of struct node
/* Queue size */
unsigned int size = 0;
int enqueue(Queue ** rear, Queue ** front, int data);
int dequeue(Queue ** front);
int getRear(Queue * rear);
void display(Queue * rear);
int getFront(Queue * front);
int isEmpty();
int isFull();
int main()
{
int ch, data;
Queue *rear, *front;
rear = NULL;
front = NULL;
/* Run indefinitely until user manually terminates */
while (1)
{
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("4. Get Rear\n");
printf("5. Get Front\n");
printf("6. Display\n");
printf("0. Exit\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(&rear, &front, data))
printf("Element added to queue.");
else
printf("Queue is full.");
break;
case 2:
data = dequeue(&front);
// 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:
data = getRear(rear);
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Rear => %d", data);
break;
case 5:
data = getFront(front);
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Front => %d", data);
break;
case 6:
display(rear);
break;
case 0:
printf("Exiting from app.\n");
exit(0);
default:
printf("Invalid choice, please input number between (0-5).");
break;
}
printf("\n\n18BCE0559\n");
}
}
int enqueue(Queue ** rear, Queue ** front, int data)
{
Queue * newNode = NULL;
// Check queue out of capacity error
if (isFull())
{
return 0;
}
// Create a new node of queue type
newNode = (Queue *) malloc (sizeof(Queue));
// Assign data to new node
newNode->data = data;
// Initially new node does not point anything
newNode->next = NULL;
// Link new node with existing last node
if ( (*rear) )
{
(*rear)->next = newNode;
}
// Make sure newly created node is at rear
*rear = newNode;
// Link first node to front if its NULL
if ( !( *front) )
{
*front = *rear;
}
// Increment quque size
size++;
return 1;
}
int dequeue(Queue ** front)
{
Queue *toDequque = NULL;
int data = INT_MIN;
if (isEmpty())
{
return INT_MIN;
}
toDequque = *front;
data = toDequque->data;
*front = (*front)->next;
size--;
free(toDequque);
return data;
}
int getRear(Queue * rear)
{
return (isEmpty())
? INT_MIN
: rear->data;
}
int getFront(Queue * front)
{
return (isEmpty())
? INT_MIN
: front->data;
}
int isEmpty()
{
return (size <= 0);
}
int isFull()
{
return (size > CAPACITY);
}
void display(Queue ** front)
{
Queue *toDequque = NULL;
int data = INT_MIN;
if (isEmpty())
{
return INT_MIN;
}
toDequque = *front;
data = toDequque->data;
printf("%d",&data);
*front = (*front)->next;
size--;
free(toDequque);
}