-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_using_stacks.cpp
More file actions
97 lines (92 loc) · 2.24 KB
/
Copy pathqueue_using_stacks.cpp
File metadata and controls
97 lines (92 loc) · 2.24 KB
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
/* Dev C++ */
#include <iostream>
#define size 3
using namespace std;
class stack{
int top=-1;
int s[size];
public:
void push(int x){
if(top==size-1){
cout<<"Queue Overflow\n";
return;
}
s[++top]=x;
cout<<x<<" enQueued\n";
}
int pop(){
return s[top--];
}
bool isEmpty(){
return top==-1 ? true : false ;
}
void print1(){
int t=0;
while(t<=top) cout<<s[top]<<" ";
}
void print2(){
int t=top;
while(t>-1) cout<<s[t--]<<" ";
}
};
class queue{
public:
void enqueue(stack *s1, int x){
s1->push(x);
}
void dequeue(stack *s1 ,stack *s2){
if(s2->isEmpty()){
if(s1->isEmpty()){
cout<<"Queue is empty can't deQueue\n";
return;
}else{
while(!s1->isEmpty()){
s2->push(s1->pop());
}
cout<<s2->pop()<<" dequeued\n";
return;
}
}
cout<<s2->pop()<<" dequeued\n";
}
void print(stack *s1, stack *s2){
cout<<"Currently Queue is : ";
s2->print2();
s1->print1();
cout<<endl;
}
};
int main() {
stack s1,s2;
queue q;
cout<<"Note : Currently, the stacks used to implement queue has size '"<<size<<"', so please change 'size' in second line of the code if you want to enQueque larger number of integers.\n";
while(1){
cout<<"\nMenu:\n";
cout<<"1.enQueue()\n";
cout<<"2.deQueue()\n";
cout<<"3.Print Queue\n";
cout<<"4.Exit\n";
cout<<"Enter a choice : ";
int n;
cin>>n;
switch(n){
case 1:
{
int x;
cin>>x;
q.enqueue(&s1, x);
break;
}
case 2:
q.dequeue(&s1, &s2);
break;
case 3:
q.print(&s1, &s2);
break;
case 4:
exit(1);
default:
cout<<"Please enter a valid choice\n";
}
}
}