Skip to content

Commit 6e02b41

Browse files
committed
Added Queue in c++
1 parent 24b1d76 commit 6e02b41

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

queue/Queue_Good_code.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int queue[100], size, front, rear;
6+
7+
void enqueue(int);
8+
void display();
9+
void dequeue();
10+
11+
int main(){
12+
int choice, element;
13+
front = 0, rear = 0;
14+
cout << "Enter size of the queue:";
15+
cout << "\n";
16+
cin >> size;
17+
cout << "Enter your choice: \n 0.Exit \n 1.Enqueue \n 2.Dequeue \n 3.Display\n\n";
18+
cin >> choice;
19+
20+
while(choice != 0) {
21+
switch(choice) {
22+
case 0:
23+
exit(0);
24+
25+
case 1:
26+
if(rear == size){
27+
cout << "Queue overflow :(\n";
28+
exit(0);
29+
}
30+
cout << "Enter the element you want to add to the queue\n";
31+
cin >> element;
32+
enqueue(element);
33+
break;
34+
35+
case 2:
36+
if(rear == front)
37+
{
38+
cout << "Queue underflow :|";
39+
exit(0);
40+
}
41+
dequeue();
42+
break;
43+
44+
case 3:
45+
display();
46+
break;
47+
}
48+
49+
cout << "\n Enter your choice: \n0.Exit \n1.Enqueue\n2.Dequeue\n3.Display\n\n";
50+
cin >> choice;
51+
}
52+
}
53+
54+
void enqueue(int element){
55+
int i,j;
56+
i = 0;
57+
while(queue[i]!=0)
58+
{
59+
i++;
60+
}
61+
queue[i] = element;
62+
rear = i + 1;
63+
}
64+
65+
void dequeue(){
66+
front++;
67+
}
68+
69+
void display(){
70+
cout << "\nElements are:\n";
71+
for(int i = front; i < rear; i++){
72+
cout << queue[i] << " ";
73+
}
74+
}
75+
76+

0 commit comments

Comments
 (0)