Last active
January 4, 2016 07:19
-
-
Save tolinwei/8587492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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