-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWith2Stacks.cpp
More file actions
96 lines (75 loc) · 1.39 KB
/
QueueWith2Stacks.cpp
File metadata and controls
96 lines (75 loc) · 1.39 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
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int size;
int top;
int *s;
};
bool isFull(struct Stack stk){return stk.top == stk.size-1;}
bool isEmpty(struct Stack stk){return stk.top == -1;}
void push(struct Stack *stk, int x)
{
if(isFull(*stk))
{
cout << "Stack full\n";
return;
}
stk->top++;
stk->s[stk->top] = x;
}
int pop(struct Stack *stk)
{
if(isEmpty(*stk))
{
cout << "Stack empty\n";
return -1;
}
int x = stk->s[stk->top];
stk->top--;
return x;
}
void enqueue(struct Stack *s1, int x)
{
push(s1,x);
}
int dequeue(struct Stack *s1, struct Stack *s2)
{
for(int i=s1->top; i>=0 ; i--)
{
push(s2,pop(s1));
}
return pop(s2);
}
void print(struct Stack stk)
{
cout << "Stack : \n";
if(isEmpty(stk))
{
cout << "Stack is empty\n";
return;
}
for(int i=stk.top; i>=0; i--)
{
cout << stk.s[i] << "\n";
}
}
int main()
{
struct Stack s1,s2;
s1.size = s2.size = 7;
s1.top = s2.top = -1;
s1.s = new int[s1.size];
s2.s = new int[s2.size];
enqueue(&s1,6);
enqueue(&s1,3);
enqueue(&s1,9);
enqueue(&s1,5);
print(s1);
cout << "Dequeue : " << dequeue(&s1,&s2) << endl;
print(s1);
print(s2);
cout << "Dequeue : " << dequeue(&s1,&s2) << endl;
print(s1);
print(s2);
}