入队列输入1234,出队列输出1234,C语言,帮忙。
#include<stdio.h> #include<malloc.h> #include<stdbool.h> #include<stdlib.h> #define length 100 typedef struct Queue{ int* pBase; int front; int rear; }QUEUE, *PQUEUE; void init_queue(PQUEUE pQueue); bool is_full(PQUEUE pQueue); bool is_empty(PQUEUE pQueue); bool enter_queue(PQUEUE pQueue, int value); bool out_queue(PQUEUE pQueue, int* value); void traverse_queue(PQUEUE pQueue); int main(void) { QUEUE myqueue; init_queue(&myqueue); enter_queue(&myqueue, 1); enter_queue(&myqueue, 2); enter_queue(&myqueue, 3); enter_queue(&myqueue, 4); traverse_queue(&myqueue); int value1, value2, value3, value4; out_queue(&myqueue, &value1); out_queue(&myqueue, &value2); out_queue(&myqueue, &value3); out_queue(&myqueue, &value4); printf("%d %d %d %d\n", value1, value2, value3, value4); return 0; } void init_queue(PQUEUE pQueue) { pQueue->pBase = (int*)malloc(sizeof(int)* length );//数组 pQueue->front = 0; pQueue->rear = 0; } bool is_full(PQUEUE pQueue) { if((pQueue->rear+1) % length == pQueue->front){ return true; }else{ return false; } } bool is_empty(PQUEUE pQueue) { if(pQueue->front == pQueue->rear){ return true; }else{ return false; } } bool enter_queue(PQUEUE pQueue, int value) { if(is_full(pQueue)){ return false; }else{ pQueue->pBase[pQueue->rear] = value; pQueue->rear = (pQueue->rear+1) % length; return true; } } void traverse_queue(PQUEUE pQueue) { int i = pQueue->front; while(pQueue->rear != i){ printf("%d ", pQueue->pBase[i]); i = (i+1) % length; } printf("\n"); } bool out_queue(PQUEUE pQueue, int* value) { if(is_empty(pQueue)){ return false; }else{ *value = pQueue->pBase[pQueue->front]; pQueue->front = (pQueue->front+1) % length; } }