题目描述
没用两个栈实现
class MyQueue {
private:
int *arr;
int top;
int maxSize;
int bottom;
public:
/** Initialize your data structure here. */
MyQueue() {
top=-1;
bottom=-1;
maxSize=100;
arr=(int*)malloc(sizeof(int)*maxSize);
memset(arr,0,sizeof(int)*maxSize);
}
/** Push element x to the back of queue. */
void push(int x) {
top++;
arr[top]=x;
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(empty())throw "空栈!";
int n;
bottom++;
n=arr[bottom];
return n;
}
/** Get the front element. */
int peek() {
if(empty())throw "空栈!";
return arr[bottom+1];
}
/** Returns whether the queue is empty. */
bool empty() {
return top==bottom;
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/