Skip to content

Instantly share code, notes, and snippets.

@tolinwei
Last active January 4, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolinwei/8587492 to your computer and use it in GitHub Desktop.
Save tolinwei/8587492 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stack>
using namespace std;
class MyQueue
{
private:
stack<int> in;
stack<int> out;
public:
void push(int v) {
in.push(v);
}
void pop() {
if (out.empty()) {
while (!in.empty()) {
out.push(in.top());
in.pop();
}
}
out.pop();
}
int front() {
if (out.empty()) {
while (!in.empty()) {
out.push(in.top());
in.pop();
}
}
return out.top();
}
};
int main(int argc, char *argv[]) {
MyQueue mq;
mq.push(1);
mq.push(2);
mq.push(3);
cout << "front(): " << mq.front() << endl;
mq.pop();
cout << "poped." << endl;
cout << "front(): " << mq.front() << endl;
mq.pop();
cout << "poped." << endl;
cout << "front(): " << mq.front() << endl;
mq.pop();
cout << "poped." << endl;
cout << "it should be empty." << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment