leetcode232. 用栈实现队列
C语言实现
#define MaxSize 100
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int top;//栈顶指针
}SqStack;
SqStack* InitStack(){
//初始化栈
SqStack *s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
return s;
}
bool StackEmpty(SqStack *s){
//判断栈是否为空
return s->top==-1;
}
bool StackOverflow(SqStack *s){
//判断栈是否已满
return s->top==MaxSize;
}
bool Push(SqStack *s,ElemType x){
//元素入栈
if(StackOverflow(s))
return false;
s->data[++s->top]=x;
return true;
}
bool Pop(SqStack *s,ElemType *x){
//元素出栈
if(StackEmpty(s))
return false;
*x=s->data[s->top--];
return true;
}
bool GetTop(SqStack *s,ElemType *x){
//获得栈顶元素
if(StackEmpty(s))
return false;
*x=s->data[s->top];
return true;
}
typedef struct {
SqStack *S1,*S2;//两个栈来模拟队列
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue *q=(MyQueue*)malloc(sizeof(MyQueue));
q->S1=InitStack();
q->S2=InitStack();
return q;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
if(StackOverflow(obj->S1)&&!StackEmpty(obj->S2))//第一个栈满了,第二个栈不为空
return ;
if(StackOverflow(obj->S1)){
//第一个栈满了,把元素全部放入第二个栈中
while(!StackEmpty(obj->S1)){
ElemType temp;//临时变量
Pop(obj->S1,&temp);
Push(obj->S2,temp);
}
}
Push(obj->S1,x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
if(StackEmpty(obj->S1)&&StackEmpty(obj->S2)) //队列为空
return -1;
if(StackEmpty(obj->S2)){
//把第一个栈中的元素全部放入第二个栈中
while(!StackEmpty(obj->S1)){
ElemType temp;//临时变量
Pop(obj->S1,&temp);
Push(obj->S2,temp);
}
}
ElemType x;
Pop(obj->S2,&x);
return x;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(obj->S1)&&StackEmpty(obj->S2)) //队列为空
return -1;
if(StackEmpty(obj->S2)){
//把第一个栈中的元素全部放入第二个栈中
while(!StackEmpty(obj->S1)){
ElemType temp;//临时变量
Pop(obj->S1,&temp);
Push(obj->S2,temp);
}
}
ElemType x;
GetTop(obj->S2,&x);
return x;
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(obj->S1)&&StackEmpty(obj->S2);//两个栈都为空队列为空
}
void myQueueFree(MyQueue* obj) {
free(obj);
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
C++实现
typedef int ElemType;
class MyQueue {
public:
stack<ElemType> S1,S2;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
S1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(empty())//队列为空
return -1;
if(S2.empty()){
while(!S1.empty()){
//把第一个栈中的元素全部放入第二个栈中
S2.push(S1.top());
S1.pop();
}
}
ElemType x=S2.top();
S2.pop();
return x;
}
/** Get the front element. */
int peek() {
if(empty())//队列为空
return -1;
if(S2.empty()){
while(!S1.empty()){
//把第一个栈中的元素全部放入第二个栈中
S2.push(S1.top());
S1.pop();
}
}
return S2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return S1.empty()&&S2.empty();//两个栈都为空队列为空
}
};
/**
* 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();
*/