-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
112 lines (96 loc) · 1.8 KB
/
circularqueue.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
include<stdio.h>
#define capacity 6
int queue[capacity];
int front = -1, rear = -1;
// Here we check if the Circular queue is full or not
int checkFull ()
{
if ((front == rear + 1) || (front == 0 && rear == capacity - 1))
{
return 1;
}
return 0;
}
// Here we check if the Circular queue is empty or not
int checkEmpty ()
{
if (front == -1)
{
return 1;
}
return 0;
}
// Addtion in the Circular Queue
void enqueue (int value)
{
if (checkFull ())
printf ("Overflow condition\n");
else
{
if (front == -1)
front = 0;
rear = (rear + 1) % capacity;
queue[rear] = value;
printf ("%d was enqueued to circular queue\n", value);
}
}
// Removal from the Circular Queue
int dequeue ()
{
int variable;
if (checkEmpty ())
{
printf ("Underflow condition\n");
return -1;
}
else
{
variable = queue[front];
if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % capacity;
}
printf ("%d was dequeued from circular queue\n", variable);
return 1;
}
}
// Display the queue
void print ()
{
int i;
if (checkEmpty ())
printf ("Nothing to dequeue\n");
else
{
printf ("\nThe queue looks like: \n");
for (i = front; i != rear; i = (i + 1) % capacity)
{
printf ("%d ", queue[i]);
}
printf ("%d \n\n", queue[i]);
}
}
int main ()
{
// Not possible as the Circular queue is empty
dequeue ();
enqueue (15);
enqueue (20);
enqueue (25);
enqueue (30);
enqueue (35);
print ();
dequeue ();
dequeue ();
print ();
enqueue (40);
enqueue (45);
enqueue (50);
enqueue (55); //Overflow condition
print ();
return 0;
}