C用数组模拟即可
typedef struct {
int a[100];
int front;
int tail;
int size;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* q=(MyQueue*)malloc(sizeof(MyQueue));
q->front=0;
q->tail=0;
q->size=0;
for(int i=0;i<100;i++){
q->a[i]=0;
}
return q;
}
void myQueuePush(MyQueue* obj, int x) {
obj->a[obj->tail]=x;
obj->tail=(obj->tail+1)%100;
obj->size++;
}
int myQueuePop(MyQueue* obj) {
int temp=obj->a[obj->front];
obj->size--;
obj->front=(obj->front+1+100)%100;
return temp;
}
int myQueuePeek(MyQueue* obj) {
return obj->a[obj->front];
}
bool myQueueEmpty(MyQueue* obj) {
if(obj->size==0) return true;
return false;
}
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);
*/
结果:
标签:obj,队列,tail,int,用栈,front,MyQueue,232,size From: https://www.cnblogs.com/llllmz/p/18045192