-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_array.c
98 lines (87 loc) · 1.7 KB
/
queue_array.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
#include<stdio.h>
#include<stdlib.h>
#define max 50
struct Queue {
int arr[max];
int front, rear;
};
void initializeQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
int isFull(struct Queue *q) {
if(q->rear==max-1)
{
return 1;
}
return 0;
}
int isEmpty(struct Queue *q) {
if(q->front == -1 || q->front > q->rear)
{
return 1;
}
return 0;
}
void enqueue(struct Queue *q, int data) {
if(isFull(q))
{
printf("Queue is full! \n");
return;
}
if (isEmpty(q)) {
q->front++;
}
q->rear++;
q->arr[q->rear] = data;
}
void dequeue(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty! \n");
return;
}
int dequeuedValue = q->arr[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
printf("Dequeued: %d \n", dequeuedValue);
}
void display(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty! \n");
return;
}
printf("Queue: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
}
int main() {
struct Queue *q=(struct Queue*)malloc(sizeof(struct Queue));
initializeQueue(q);
int choice, data;
do{
printf("\n1. Enqueue \n2. Dequeue \n3. Display \n4. Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter data: ");
scanf("%d",&data);
enqueue(q, data);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
case 4:
exit(0);
default:
printf("Enter a valid choice. \n");
}
}while(choice!=4);
return 0;
}