Leetcode 232 Solution
This article provides solution to leetcode question 232 (implement-queue-using-stacks)
Access this page by simply typing in "lcs 232" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/implement-queue-using-stacks
Solution
class Queue {
stack<int> s1;
stack<int> s2;
void adjust()
{
while (s1.empty() == false)
{
s2.push(s1.top());
s1.pop();
}
}
public:
// Push element x to the back of queue.
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (s2.empty())
adjust();
s2.pop();
}
// Get the front element.
int peek(void) {
if (s2.empty())
adjust();
return s2.top();
}
// Return whether the queue is empty.
bool empty(void) {
return s1.empty() && s2.empty();
}
};