-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_using_2-Stacks.cpp
More file actions
109 lines (91 loc) · 2.09 KB
/
Copy pathQueue_using_2-Stacks.cpp
File metadata and controls
109 lines (91 loc) · 2.09 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
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
class Queue
{
private:
stack<int> stack1;
stack<int> stack2;
public:
void enqueue(int data)
{
stack1.push(data);
}
void dequeue()
{
int data;
if (stack1.empty() && stack2.empty()) //if both stack-1 and stack-2 are empty
{
cout << endl
<< "Queue is empty." << endl;
return;
}
else if (stack2.empty()) //if stack-2 is empty then insert elements from stack-1 to stack-2
{
while (!stack1.empty())
{
data = stack1.top();
stack1.pop();
stack2.push(data);
}
}
data = stack2.top();
stack2.pop();
cout << endl
<< data << " is deleted." << endl;
}
void display()
{
stack<int> stack11;
stack<int> stack22;
stack11 = stack1;
stack22 = stack2;
while (!stack22.empty())
{
cout << endl
<< "Data = " << stack22.top();
stack22.pop();
}
while (!stack11.empty())
{
cout << endl
<< "Data = " << stack11.top();
stack11.pop();
}
}
};
int main()
{
Queue q;
int choice, data;
while (1)
{
cout << endl;
cout << "1. To Enter data into queue." << endl;
cout << "2. To Delete data from queue." << endl;
cout << "3. To Display the data." << endl;
cout << "4. TO EXIT." << endl;
cout << "\nENTER YOUR CHOICE... : ";
cin >> choice;
switch (choice)
{
case 1:
cout << endl
<< "Enter data to be inserted : ";
cin >> data;
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
break;
default:
cout << "\nINVALID CHOICE..." << endl;
}
}
return 0;
}