#include<stdio.h>#include<stdlib.h>#include"queueDAT2.h"#defineMAX10/*定义结构体*/structnode{
Item *value;int front, rear;};/*创建队列*/
Queue create(void){
Queue q;
q =(structnode*)malloc(sizeof(structnode));
q->value =(Item *)malloc(sizeof(Item));
q->front = MAX -1;
q->rear = MAX -1;return q;}/*队空*/boolis_empty(Queue q){return q->front == q->rear;}/*队满*/boolis_full(Queue q){return(q->rear +1)% MAX == q->front;}/*入队*/voidpush(Queue q, Item x){if(is_full(q))printf("队满!");
q->rear =(q->rear +1)% MAX;
q->value[q->rear]= x;}/*出队*/
Item pop(Queue q){
Item x;if(is_empty(q))printf("队空!");
q->front =(q->front +1)% MAX;
x = q->value[q->front];return x;}/*显示队列元素*/voidshowqueue(Queue q){int n=0,m;while(!is_empty(q)){
q->front =(q->front +1)% MAX;
m = q->value[q->front];printf(" %d ", m);
n++;}for(int i =0; i < n; i++){
q->front =(q->front -1)% MAX;}}
测试函数:
#include<stdio.h>#include<stdlib.h>#include"queueDAT2.h"intmain(void){
Queue p;
p =create();push(p,3);push(p,27);push(p,15);showqueue(p);pop(p);printf("\n");showqueue(p);getchar();}