forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_queue.c
83 lines (75 loc) · 2.44 KB
/
circular_queue.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
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 3
void insert(int*, int*, int*, int*);
void delete(int*, int*, int*);
void disp(int*, int*, int*);
int main() {
int queue[MAXSIZE], f = -1, r = -1, c, val;
printf("1. INSERT\n");
printf("2. DELETE\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
while (1) {
printf("Enter choice: ");
scanf("%d", &c);
switch (c) {
case 1: printf("Enter value to insert: ");
scanf("%d", &val);
insert(queue, &f, &r, &val); break;
case 2: delete(queue, &f, &r); break;
case 3: disp(queue, &f, &r); break;
case 4: exit(1);
default: printf("Invalid option\n");
}
}
return 0;
}
void insert(int* queue, int* f, int* r, int* val) {
if ((*f == 0 && *r == MAXSIZE - 1) || (*f == *r + 1)) {
printf("Queue Overflow\n");
} else {
if (*r == -1) {
*f = *r = 0;
} else if (*r == MAXSIZE - 1) {
*r = 0;
} else {
(*r)++;
}
queue[*r] = *val;
}
}
void delete(int* queue, int* f, int* r) {
if (*f == -1) {
printf("Queue Underflow\n");
} else {
printf("%d\n", queue[*f]);
if (*f == *r) {
*f = *r = -1;
} else if (*f == MAXSIZE - 1) {
*f = 0;
} else {
(*f)++;
}
}
}
void disp(int* queue, int* f, int* r) {
if (*f == -1) {
printf("Queue Underflow\n");
} else {
printf("Queue Elements: ");
if (*f <= *r) {
for (int i = *f; i <= *r; i++) {
printf("%d ", queue[i]);
}
} else {
for (int i = 0; i <= *r; i++) {
printf("%d ", queue[i]);
}
for (int i = *f; i <= MAXSIZE - 1; i++) {
printf("%d ", queue[i]);
}
}
printf("\n");
}
}