#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 struct queue{ int front; int rear; int *p; }; void init(struct queue* s ) { s->front=0; s->rear=0; s->p=(int*)malloc(sizeof(int*)*MAXSIZE); } void enqueue(struct queue* s,int e) { if((s->rear+1)%MAXSIZE==s->front) { puts("queue full\n"); exit; } s->p[s->rear]=e; s->rear++; } int dequeue(struct queue* s) { int e; if(s->rear==s->front) { puts("queue empty\n"); exit; } e=s->p[s->front]; s->front++; return e; } void print_queue(struct queue* s) { int i,j; i=s->front;j=s->rear; if(s->rear==s->front) { puts("queue empty can't print\n"); exit; } if(s->rear>s->front) for(i;i<j;i++) printf("%d ",s->p[i]); printf("\n"); } int main() { struct queue tom; init(&tom); printf("tom.age=%d,tome.high=%d,tom->p=%p\n",tom.front,tom.rear,tom.p); enqueue(&tom,5); enqueue(&tom,10); enqueue(&tom,11); print_queue(&tom); dequeue(&tom); print_queue(&tom); return 0; }
输出结果
tom.age=0,tome.high=0,tom->p=0x138f260
5 10 11
10 11