-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueuusingstack.txt
53 lines (48 loc) · 1.11 KB
/
queueuusingstack.txt
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
#include<bits/stdc++.h>
class Queue {
// Define the data members(if any) here.
stack<int> s1;
stack<int> s2;
public:
Queue() {
// Initialize your data structure here.
}
void enQueue(int val) {
// Implement the enqueue() function.
s1.push(val);
}
int deQueue() {
// Implement the dequeue() function.
if(s1.empty()) return -1;
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
int temp=s2.top();
s2.pop();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
return temp;
}
int peek() {
// Implement the peek() function here
if(s1.empty()) return -1;
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
int temp=s2.top();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
return temp;
}
bool isEmpty() {
// Implement the isEmpty() function here.
if(s1.empty()) return true;
return false;
}
};