-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue using stack
125 lines (108 loc) · 1.81 KB
/
queue using stack
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Online C++ compiler to run C++ program online
#include <iostream>
#include<bits/stdc++.h>
#define mxx 5
using namespace std;
class Stack
{
public:
int top1=-1;
int top2=-1;
int s1[mxx];
int s2[mxx];
int count=0;
void push1(int x)
{
if(top1==mxx-1)
{
cout<<"full";
}
else
{
top1++;
s1[top1]=x;
}
}
void push2(int x)
{
if(top2==mxx-1)
{
cout<<"full";
}
else
{
top2++;
s2[top2]=x;
}
}
int pop1()
{
if(top1==-1)
{
cout<<"empty";
}
else
{
int x= s1[top1];
top1--;
return x;
}
}
int pop2()
{
if(top2==-1)
{
cout<<"empty";
}
else
{
int x= s2[top2];
top2--;
return x;
}
}
void enqueue(int x)
{
push1(x);
count++;
}
void dequeue()
{
int a,b,i;
if(top1==-1 && top2==-1)
{
cout<<"empty";
}
else
{
for(i=0;i<count;i++)
{
a=pop1();
push2(a);
}
b=pop2();
count--;
for( i=0;i<count;i++)
{
a=pop2();
push1(a);
}
}
}
void display()
{
int i;
for(i=0;i<=top1;i++)
{
cout<<s1[i];
}
}
};
int main() {
Stack s;
s.enqueue(4);
s.enqueue(5);
s.enqueue(6);
s.dequeue();
s.display();
}