Untitled
user_9124840
plain_text
a year ago
793 B
8
Indexable
class MyQueue {
stack<int>inStack;
stack<int>outStack;
public:
MyQueue() {
}
void push(int x) {
inStack.push(x);
}
int pop() {
if(outStack.empty()){
while(!inStack.empty()){
outStack.push(inStack.top());
inStack.pop();
}
}
int x=outStack.top();
outStack.pop();
return x;
}
int peek() {
if(outStack.empty()){
while(!inStack.empty()){
outStack.push(inStack.top());
inStack.pop();
}
}
return outStack.top();
}
bool empty() {
return (inStack.empty()&& outStack.empty());
}
};
Editor is loading...
Leave a Comment