-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9queueUsingStack.cpp
More file actions
105 lines (105 loc) · 1.71 KB
/
Copy path9queueUsingStack.cpp
File metadata and controls
105 lines (105 loc) · 1.71 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
#include <iostream>
#include <climits>
#define MAX 10
using namespace std;
class myStack
{
public:
int arr[MAX];
int top;
myStack()
{
top = -1;
}
int isEmpty()
{
return top == -1;
}
int isFull()
{
return top == MAX - 1;
}
void push(int k)
{
if (isFull())
return;
arr[++top] = k;
}
int pop()
{
if (isEmpty())
return -1;
int k = arr[top--];
return k;
}
int peek()
{
if (isEmpty())
return -1;
return arr[top];
}
void display()
{
if (isEmpty())
{
cout << "nothing is in stack";
return;
}
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << "\n";
}
};
class queue{
myStack S1;
myStack S2;
public:
void enqueue(int x){
S1.push(x);
}
int dequeue(){
if(S1.isEmpty()&&S2.isEmpty()){
cout<<"queue is empty.";
return INT_MIN;
}
if (S2.isEmpty())
{
while (!S1.isEmpty())
{
S2.push(S1.pop());
}
}
int topVal=S2.pop();
return topVal;
}
void display(){
if(S1.isEmpty()&&S2.isEmpty()){
cout<<"queue is empty.";
return;
}
if (S2.isEmpty())
{
while (!S1.isEmpty())
{
S2.push(S1.pop());
}
}
S2.display();
}
bool isEmpty(){
if(S1.isEmpty()&&S2.isEmpty())
return true;
return false;
}
};
int main() {
queue Q;
Q.enqueue(3);
Q.enqueue(5);
Q.enqueue(6);
Q.enqueue(8);
cout<<"dequeue: "<<Q.dequeue()<<endl;
cout<<"dequeue: "<<Q.dequeue()<<endl;
Q.display();
return 0;
}