Skip to content

Commit 0cb0d8f

Browse files
committed
added cpp implementation for queue<->stack
1 parent 911004a commit 0cb0d8f

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stack>
2+
#include <cassert>
3+
4+
template<class T>
5+
class queue_from_stack {
6+
std::stack<T> st1, st2;
7+
void transfer();
8+
public:
9+
queue_from_stack();
10+
bool empty() const;
11+
size_t size() const;
12+
T &front();
13+
void push(const T &);
14+
void pop();
15+
};
16+
17+
template<class T>
18+
queue_from_stack<T>::queue_from_stack() {}
19+
20+
template<class T>
21+
void queue_from_stack<T>::transfer() {
22+
while(!st1.empty()) {
23+
st2.push(st1.top());
24+
st1.pop();
25+
}
26+
}
27+
28+
template<class T>
29+
bool queue_from_stack<T>::empty() const {
30+
return st1.empty() && st2.empty();
31+
}
32+
33+
template<class T>
34+
size_t queue_from_stack<T>::size() const {
35+
return st1.size() + st2.size();
36+
}
37+
38+
template<class T>
39+
T &queue_from_stack<T>::front() {
40+
if (st2.empty()) transfer();
41+
return st2.top();
42+
}
43+
44+
template<class T>
45+
void queue_from_stack<T>::push(const T &x) {
46+
st1.push(x);
47+
}
48+
49+
// removes the element at front
50+
template<class T>
51+
void queue_from_stack<T>::pop() {
52+
if (st2.empty()) transfer();
53+
st2.pop();
54+
}
55+
56+
int main() {
57+
58+
queue_from_stack<int> qfs;
59+
assert(qfs.size() == 0);
60+
assert(qfs.empty());
61+
62+
qfs.push(1);
63+
qfs.push(2);
64+
qfs.push(3);
65+
66+
assert(qfs.front() == 1);
67+
68+
qfs.pop();
69+
70+
assert(qfs.front() == 2);
71+
72+
qfs.pop();
73+
qfs.push(4);
74+
qfs.push(5);
75+
76+
assert(qfs.size() == 3);
77+
assert(qfs.front() == 3);
78+
79+
qfs.pop();
80+
81+
assert(qfs.front() == 4);
82+
83+
return 0;
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <queue>
2+
#include <cassert>
3+
4+
template<class T>
5+
class stack_from_queue {
6+
std::queue<T> q1, q2;
7+
public:
8+
stack_from_queue();
9+
bool empty() const;
10+
size_t size() const;
11+
T &top();
12+
void push(const T &);
13+
void pop();
14+
};
15+
16+
template<class T>
17+
stack_from_queue<T>::stack_from_queue() {}
18+
19+
template<class T>
20+
bool stack_from_queue<T>::empty() const {
21+
return q1.empty();
22+
}
23+
24+
template<class T>
25+
size_t stack_from_queue<T>::size() const {
26+
return q1.size();
27+
}
28+
29+
template<class T>
30+
T &stack_from_queue<T>::top() {
31+
return q1.front();
32+
}
33+
34+
template<class T>
35+
void stack_from_queue<T>::push(const T &x) {
36+
std::queue<T> qtemp;
37+
qtemp.push(x);
38+
while(!q1.empty()) {
39+
qtemp.push(q1.front());
40+
q1.pop();
41+
}
42+
std::swap(q1, qtemp);
43+
}
44+
45+
template<class T>
46+
void stack_from_queue<T>::pop() {
47+
q1.pop();
48+
}
49+
50+
int main() {
51+
52+
stack_from_queue<int> sfq;
53+
assert(sfq.size() == 0);
54+
assert(sfq.empty());
55+
56+
sfq.push(1);
57+
sfq.push(2);
58+
sfq.push(3);
59+
60+
assert(sfq.top() == 3);
61+
62+
sfq.pop();
63+
64+
assert(sfq.top() == 2);
65+
66+
sfq.pop();
67+
sfq.push(4);
68+
sfq.push(5);
69+
70+
assert(sfq.size() == 3);
71+
assert(sfq.top() == 5);
72+
73+
sfq.pop();
74+
75+
assert(sfq.top() == 4);
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)