-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular_Queue.c
172 lines (163 loc) · 3.42 KB
/
Circular_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//circular queue
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 5
int A[MAX_SIZE];
int front = -1, rear = -1;
int IsEmpty()
{
if (front == -1 && rear == -1)
{
return 1;
}
else
return 0;
}
int IsFull()
{
if ((rear == MAX_SIZE-1 && front == 0) || (rear == front-1))//((rear+1) % MAX_SIZE == front)
{
return 1;
}
else
return 0;
}
void Enqueue(int x)
{
if(IsFull())
{
printf("Error: Queue is Full\n");
return;
}
if (IsEmpty())
{
front = rear = 0;
}
else
{
if (rear == MAX_SIZE - 1)
rear = 0;
else
rear ++;
//rear = (rear+1) % MAX_SIZE;
}
A[rear] = x;
printf("Data %d inserted Successfully\n",x);
}
void Dequeue()
{
if(IsEmpty())
{
printf("Error: Queue is Empty\n");
return;
}
else
{
if (front == rear)
{
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after dequeing it. ? */
else if (front == MAX_SIZE-1)
front = 0;
else
front++;
// else
// {
// front = (front+1) % MAX_SIZE;
// }
}
}
int Front()
{
if(front == -1)
{
printf("Error: empty queue\n");
return -1;
}
return A[front];
}
int QueueSize()
{
int count =-1;
count = (rear + MAX_SIZE - front) % MAX_SIZE + 1;
return count;
}
//void Print()
//{
// int count = QueueSize();
// if(IsEmpty())
// {
// printf("Error: Queue is Empty\n");
// return;
// }
// else
// {
// printf("Queue is as follows : ");
// for(int i = 0; i <count; i++)
// {
// int index = (front+i) % MAX_SIZE;
// printf("%d ",A[index]);
// }
// printf("\n\n");
// }
//}
void Print()
{
if (IsEmpty())
{
printf("\nQueue is Empty");
return;
}
printf("\nElements in Circular Queue are: \n");
if (rear >= front)
{
for (int i = front; i <= rear; i++)
printf("%d ",A[i]);
}
else
{
for (int i = front; i < MAX_SIZE; i++)
printf("%d ", A[i]);
for (int i = 0; i <= rear; i++)
printf("%d ", A[i]);
}
printf("\n");
}
int main()
{
int ch,n;
printf("Enter your choice\n");
printf("1 to insert element\n2 to delete Element\n3 to return front element\n4 to find size of queue\n5 to check if queue is empty or full\n6 to Display elements\n7 to exit\n");
while (1)
{
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter data to be inserted\n");
scanf("%d",&n);
Enqueue(n);
break;
case 2:
Dequeue();
break;
case 3:printf("%d\n",Front());
break;
case 4:printf("%d\n",QueueSize());
break;
case 5:if (IsEmpty() == 1)
printf("Queue is Empty\n");
else if (IsFull())
printf("Queue is Full\n");
else
printf("Queue is not empty\n");
break;
case 6:Print();
break;
case 7:exit(1);
break;
default:printf("Enter Valid option\n");
break;
}
}
}