问题
关键:利用两个栈来建立一个队列
typedef struct {
int LTop;
int Ldata[10010];
int RTop;
int Rdata[10010];
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate(int maxSize) {
MyQueue *A = malloc(sizeof(MyQueue));
A->LTop=L->RTop=-1;
return A;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
obi->LTop++;
obj->Ldata[obj->LTop] = x;
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
while(obj->LTop > 0){
obj->Rdata[++obj->RTop] = obj->Ldata[obj->LTop--];
}
int res = obj->Ldata[LTop]; //取出top元素
obj->LTop=-1;
while(obj->RTop > -1){
obj->Ldata[++obj->LTop] = obj->Rdata[obj->RTop--];
}
return res;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
return obj->Ldata[0];
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
if(obj->LTop == -1) return true;
return false;
}
void myQueueFree(MyQueue* obj) {
free(obj);
}