Skip to content

Commit faefb14

Browse files
committed
Queue Programs
1 parent 5bc5dae commit faefb14

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

Queue/queue_using_stack.cpp

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,69 @@ struct Queue {
66
// Add an item to the queue
77
void enQueue(int x)
88
{
9-
// Push item into the first stack (s1)
9+
// Move all elements from first stack (s1) to second stack (s2)
10+
while (!s1.empty()) {
11+
s2.push(s1.top());
12+
s1.pop();
13+
}
14+
// Push item into first stack (s1)
1015
s1.push(x);
16+
// Push everything back to first stack (s1)
17+
while (!s2.empty()) {
18+
s1.push(s2.top());
19+
s2.pop();
20+
}
1121
}
22+
1223
// Remove an item from the queue
1324
int deQueue()
1425
{
15-
// If both stacks are empty, then exit
16-
if (s1.empty() && s2.empty()){
17-
cout << "Q is empty";
18-
exit(0);
26+
// If first stack (s1) is empty, then exit
27+
if (s1.empty()) {
28+
cout << "Queue is Empty, thus deletion cannot be performed\n";
29+
return -1;
1930
}
2031

21-
// If second stack (s2) is empty, move elements from first stack (s1) to second stack (s2)
22-
if (s2.empty()) {
23-
while (!s1.empty()){
24-
s2.push(s1.top());
25-
s1.pop();
26-
}
27-
}
28-
// Return the top item from secodn stack (s2)
29-
int x = s2.top();
30-
s2.pop();
32+
// Else return top of first stack (s1)
33+
int x = s1.top();
34+
s1.pop();
3135
return x;
3236
}
3337
void sizeQueue()
3438
{
35-
cout<<"Size of the Queue: "<<s1.size()+s2.size()<<endl;
39+
cout<<"Size of the Queue: "<<s1.size()<<endl;
3640
}
3741
};
3842

3943
int main()
4044
{
4145
Queue q;
4246
int x, data;
43-
char ch;
4447
do
4548
{
46-
cout<<"\t Main Menu \n";
47-
cout<<"\t 1. Insertion to Queue \n";
48-
cout<<"\t 2. Deletion from Queue \n";
49-
cout<<"\t 3. Size of Queue\n";
50-
cout<<"\t 4. Quit \n";
51-
cout<<"Enter your choice: ";
52-
cin>>x;
53-
switch(x)
54-
{
55-
case 1: cout<<"Enter the value to be inserted in Queue: ";
56-
cin>>data;
57-
q.enQueue(data);
58-
break;
59-
case 2: cout<<"The deleted element is: "<<q.deQueue()<<endl;
60-
break;
61-
case 3: q.sizeQueue();
62-
break;
63-
case 4: exit(0);
64-
default: cout<<"Wrong Choice Entered\n";
65-
}
66-
cout<<"Do you want to go to the main menu (y/n): ";
67-
cin>>ch;
49+
cout<<"\nMain Menu: \n";
50+
cout<<"1. Insertion to Queue \n";
51+
cout<<"2. Deletion from Queue \n";
52+
cout<<"3. Size of Queue\n";
53+
cout<<"4. Quit\n";
54+
cout<<"Enter your choice (1-4): ";
55+
cin>>x;
56+
switch(x)
57+
{
58+
case 1: cout<<"Enter the value to be inserted in Queue: ";
59+
cin>>data;
60+
q.enQueue(data);
61+
break;
62+
case 2: data = q.deQueue();
63+
if(data!=-1)
64+
cout<<"The deleted element is: "<<data<<endl;
65+
break;
66+
case 3: q.sizeQueue();
67+
break;
68+
case 4: exit(0);
69+
default: cout<<"Invalid Choice Entered\n";
70+
}
6871
}
69-
while(ch=='Y'||ch=='y');
72+
while(true);
7073
return 0;
7174
}

0 commit comments

Comments
 (0)