-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueCircular.c
145 lines (133 loc) · 2.43 KB
/
QueueCircular.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#define SIZE 5
struct Queue
{
int front;
int rear;
int queue[SIZE];
};
void create(struct Queue *q)
{
q->front = q->rear = -1;
}
int isFull(struct Queue *q)
{
if ((q->front == q->rear + 1) || (q->front == 0 && q->rear == SIZE - 1))
{
return 1;
}
return 0;
}
int isEmpty(struct Queue *q)
{
if (q->front == -1)
{
return 1;
}
return 0;
}
void Enqueue(struct Queue *q, int data)
{
if (isFull(q))
{
printf("!! Queue is full !!");
return;
}
else
{
if (q->front == -1)
{
q->front = 0;
}
q->rear = (q->rear + 1)%SIZE;
q->queue[q->rear] = data;
}
}
int Dequeue(struct Queue *q)
{
if (isEmpty(q))
{
return -1;
}
else
{
int data = q->queue[q->front];
if (q->rear == q->front)
{
q->front = q->rear = -1;
}
else
{
q->front = (q->front + 1) % SIZE;
}
return data;
}
}
void display(struct Queue *q)
{
if (isEmpty(q))
{
printf("!! Queue is Empty !!");
return;
}
else
{
for (int i = q->front; i != q->rear; i = (i + 1) % SIZE)
{
printf("|%d", q->queue[i]);
}
printf("|%d|", q->queue[q->rear]);
}
}
int main()
{
struct Queue *q;
create(q);
while (1)
{
int ch;
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
{
int n;
printf("\nEnter no. of element to Insert: ");
scanf("%d", &n);
Enqueue(q, n);
break;
}
case 2:
{
int d = Dequeue(q);
if (d == -1)
{
printf("!! Queue is Empty !!");
}
else
{
printf("Deleted Element is %d ", d);
}
break;
}
case 3:
{
printf("Queue is \n");
display(q);
printf("\n");
break;
}
case 4:
exit(1);
break;
default:
break;
}
}
return 0;
}