Skip to content

Commit 3d42122

Browse files
authored
Merge pull request larissalages#126 from Divyanshu-info/master
Added Circular queue
2 parents 5e7b83f + 415ea36 commit 3d42122

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
*.exe

data_structures/C++/circularqueue.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include <iostream>
2+
#include <stack>
3+
using namespace std;
4+
5+
#define SIZE 10
6+
7+
class CircularQueue
8+
{
9+
int a[SIZE];
10+
int rear;
11+
int front;
12+
13+
public:
14+
CircularQueue()
15+
{
16+
rear = front = -1;
17+
}
18+
19+
bool isFull()
20+
{
21+
if (front == 0 && rear == SIZE - 1)
22+
{
23+
return true;
24+
}
25+
if (front == rear + 1)
26+
{
27+
return true;
28+
}
29+
return false;
30+
}
31+
32+
bool isEmpty()
33+
{
34+
if (front == -1)
35+
{
36+
return true;
37+
}
38+
else
39+
{
40+
return false;
41+
}
42+
}
43+
44+
void enqueue(int x);
45+
int dequeue();
46+
void display();
47+
int size();
48+
};
49+
50+
void CircularQueue ::enqueue(int x)
51+
{
52+
if (isFull())
53+
{
54+
cout << "Queue is full";
55+
}
56+
else
57+
{
58+
if (front == -1)
59+
{
60+
front = 0;
61+
}
62+
rear = (rear + 1) % SIZE;
63+
64+
a[rear] = x;
65+
cout << endl
66+
<< "Inserted " << x << endl;
67+
}
68+
}
69+
70+
int CircularQueue ::dequeue()
71+
{
72+
int y;
73+
74+
if (isEmpty())
75+
{
76+
cout << "Queue is empty\n"
77+
<< endl;
78+
}
79+
else
80+
{
81+
y = a[front];
82+
if (front == rear)
83+
{
84+
front = -1;
85+
rear = -1;
86+
}
87+
else
88+
{
89+
front = (front + 1) % SIZE;
90+
}
91+
return (y);
92+
}
93+
}
94+
95+
void CircularQueue ::display()
96+
{
97+
int i;
98+
if (isEmpty())
99+
{
100+
cout << endl
101+
<< "\nEmpty Queue\n"
102+
<< endl;
103+
}
104+
else
105+
{
106+
cout << endl
107+
<< "Front -> " << front;
108+
cout << endl
109+
<< "Elements -> ";
110+
for (i = front; i != rear; i = (i + 1) % SIZE)
111+
{
112+
cout << a[i] << "\t";
113+
}
114+
cout << a[i];
115+
cout << endl
116+
<< "Rear -> " << rear << "\n";
117+
}
118+
}
119+
120+
int CircularQueue ::size()
121+
{
122+
if (rear >= front)
123+
{
124+
return (rear - front) + 1;
125+
}
126+
else
127+
{
128+
return (SIZE - (front - rear) + 1);
129+
}
130+
}
131+
132+
int main()
133+
{
134+
CircularQueue cq;
135+
int n;
136+
int x;
137+
while (1)
138+
{
139+
cout << "\n1. enqueue\n2. dequeue\n3. size\n4. Display Elements\n5. Exit\n";
140+
cin >> n;
141+
switch (n)
142+
{
143+
case 1:
144+
cout << "Enter the No. ";
145+
cin >> x;
146+
cq.enqueue(x);
147+
break;
148+
149+
case 2:
150+
cout << "Removed element: " << cq.dequeue() << endl;
151+
break;
152+
case 3:
153+
cout << endl
154+
<< "Size of queue: " << cq.size() << endl;
155+
break;
156+
case 4:
157+
cq.display();
158+
break;
159+
case 5:
160+
return 0;
161+
break;
162+
default:
163+
break;
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)