-
Notifications
You must be signed in to change notification settings - Fork 1
/
8_24QUEUE.c
86 lines (78 loc) · 1.92 KB
/
8_24QUEUE.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
#include <stdio.h>
#define MAX_SIZE 10
struct Lqueue {
int q[MAX_SIZE];
int front, rear;
};
void init(struct Lqueue* queue) {
queue->front = queue->rear = -1;
}
void qinsert(struct Lqueue* queue) {
if (queue->rear == MAX_SIZE - 1) {
printf("Sorry! Queue is full\n");
} else {
int no;
printf("Enter Number: ");
scanf("%d", &no);
queue->rear++;
queue->q[queue->rear] = no;
if (queue->front == -1) {
queue->front = 0;
}
}
}
void qdelete(struct Lqueue* queue) {
if (queue->front == -1) {
printf("Sorry! No elements\n");
} else {
int no;
no = queue->q[queue->front];
printf("Deleted element is %d\n", no);
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front++;
}
}
}
void print(struct Lqueue* queue) {
if (queue->front != -1) {
printf("Queue elements are:\n");
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d\t", queue->q[i]);
}
printf("\n");
} else {
printf("Queue is Empty!\n");
}
}
int main() {
struct Lqueue L;
int opt;
init(&L);
do {
printf("\nMENU\n");
printf("1. Insertion\n");
printf("2. Deletion\n");
printf("3. Print\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &opt);
switch (opt) {
case 1:
qinsert(&L);
break;
case 2:
qdelete(&L);
break;
case 3:
print(&L);
break;
case 4:
break;
default:
printf("Invalid option!\n");
}
} while (opt != 4);
return 0;
}