-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc6ca9b
commit 77386a9
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <stack> | ||
|
||
class MyQueue | ||
{ | ||
public: | ||
/** Initialize your data structure here. */ | ||
MyQueue() | ||
{ | ||
} | ||
|
||
/** Push element x to the back of queue. */ | ||
void push(int x) | ||
{ | ||
while(!s2.empty()) | ||
{ | ||
temp = s2.top(); | ||
s2.pop(); | ||
s1.push(temp); | ||
} | ||
s1.push(x); | ||
} | ||
|
||
/** Removes the element from in front of queue and returns that element. */ | ||
int pop() | ||
{ | ||
int x = peek(); | ||
s2.pop(); | ||
return x; | ||
} | ||
|
||
/** Get the front element. */ | ||
int peek() | ||
{ | ||
while(!s1.empty()) | ||
{ | ||
temp = s1.top(); | ||
s1.pop(); | ||
s2.push(temp); | ||
} | ||
return s2.top(); | ||
} | ||
|
||
/** Returns whether the queue is empty. */ | ||
bool empty() | ||
{ | ||
return s1.empty() && s2.empty(); | ||
} | ||
|
||
private: | ||
std::stack<int> s1; | ||
std::stack<int> s2; | ||
int temp; | ||
}; | ||
|
||
/** | ||
* Your MyQueue object will be instantiated and called as such: | ||
* MyQueue* obj = new MyQueue(); | ||
* obj->push(x); | ||
* int param_2 = obj->pop(); | ||
* int param_3 = obj->peek(); | ||
* bool param_4 = obj->empty(); | ||
*/ |