forked from anish2210/hacktober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_queue.c
70 lines (62 loc) · 1.88 KB
/
linear_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
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
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 (*r == MAXSIZE - 1) {
printf("Queue Overflow\n");
} else {
if (*r == -1) {
*f = *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 {
(*f)++;
}
}
}
void disp(int* queue, int* f, int* r) {
if (*f == -1) {
printf("Queue Underflow\n");
} else {
printf("Queue Elements: ");
for (int i = *f; i <= *r; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}