-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10stackUsingQueueA.cpp
More file actions
109 lines (108 loc) · 1.95 KB
/
Copy path10stackUsingQueueA.cpp
File metadata and controls
109 lines (108 loc) · 1.95 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 <iostream>
#include <climits>
#define MAX 10
using namespace std;
class queue
{
private:
int arr[MAX], front, rear;
public:
queue()
{
front = -1;
rear = -1;
}
bool isEmpty()
{
return front == rear;
}
bool isFull()
{
return rear == MAX - 1;
}
void enqueue(int k)
{
if (isFull())
{
cout << "queue is full";
return;
}
arr[++rear] = k;
}
int dequeue()
{
int a;
if (isEmpty())
return INT_MIN;
front = front + 1;
return arr[front];
if (front == MAX)
return INT_MAX;
}
int peek()
{
if (isEmpty())
return INT_MIN;
return arr[front + 1];
}
void display()
{
if (isEmpty())
{
cout << "nothing is in stack\n";
return;
}
for (int i = rear; i >= front + 1; i--)
cout << arr[i] << " ";
cout << "\n";
}
};
class Stack{
queue Q1;
queue Q2;
public:
void push(int item){
Q2.enqueue(item);
if(!Q1.isEmpty()){
while(!Q1.isEmpty())
Q2.enqueue(Q1.dequeue());
}
queue temp= Q1;
Q1= Q2;
Q2=temp;
}
int pop(){
if (Q1.isEmpty())
{
cout << "nothing is in stack\n";
return INT_MIN;
}
return Q1.dequeue();
}
int peek(){
if (Q1.isEmpty())
{
cout << "nothing is in stack\n";
return INT_MIN;
}
return Q1.peek();
}
void display(){
if (Q1.isEmpty())
{
cout << "nothing is in stack\n";
return;
}
Q1.display();
}
};
int main(){
Stack S;
S.push(2);
S.push(7);
S.push(9);
S.push(3);
cout<<"pop: "<<S.pop()<<endl;
cout<<"peek: "<<S.peek()<<endl;
S.display();
}