-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueUsingPointers.c
68 lines (62 loc) · 1.69 KB
/
queueUsingPointers.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
#include <stdio.h>
#define SIZE 100
void enqueue(int *inp_arr, int *Rear, int *Front);
void dequeue(int *inp_arr, int *Rear, int *Front);
void display(int *inp_arr, int *Rear, int *Front);
int main() {
int inp_arr[SIZE];
int Rear = -1;
int Front = -1;
int ch;
while (1) {
printf("Enter your choice of operations :\t1)Enqueue\t2)Dequeue\t3)Display\t4)Exit\n");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue(inp_arr, &Rear, &Front);
break;
case 2:
dequeue(inp_arr, &Rear, &Front);
break;
case 3:
display(inp_arr, &Rear, &Front);
break;
case 4:
return 0;
default:
printf("Incorrect choice \n");
}
}
}
void enqueue(int *inp_arr, int *Rear, int *Front) {
int insert_item;
if (*Rear == SIZE - 1)
printf("Overflow \n");
else {
if (*Front == -1)
*Front = 0;
printf("Element to be inserted in the Queue: ");
scanf("%d", &insert_item);
*Rear = *Rear + 1;
inp_arr[*Rear] = insert_item;
}
}
void dequeue(int *inp_arr, int *Rear, int *Front) {
if (*Front == -1 || *Front > *Rear) {
printf("Underflow \n");
return;
} else {
printf("Element deleted from the Queue: %d\n", inp_arr[*Front]);
*Front = *Front + 1;
}
}
void display(int *inp_arr, int *Rear, int *Front) {
if (*Front == -1)
printf("Empty Queue \n");
else {
printf("Queue:\t");
for (int i = *Front; i <= *Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}