队列的链式存储,出队的一个错误,大家帮我看看什么地方错了吧
程序代码:
#include <stdio.h> #include <malloc.h> struct node{ int data; struct node *next; }; struct queuenode{ struct node *front; struct node *rear; }; struct queuenode *init(struct queuenode *s){ s=(struct queuenode *)malloc(sizeof(struct queuenode)); s->rear=(struct node *)malloc(sizeof(struct node)); s->front=(struct node *)malloc(sizeof(struct node)); s->rear->next=NULL; s->front=s->rear; return s; } int empty(struct queuenode *s){ if(s->front==s->rear) return 1; else return 0; } void in_queue(struct queuenode *s,int n){ struct node *p; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=s->rear->next; s->rear=p; } int out_queue(struct queuenode *s){ struct node *p; int e; if(empty(s)){ printf("空队!"); return -1; } else{ p=s->front->next;//printf("%d",p->data); e=p->data; s->front->next=p->next; free(p); return e; } } void main(){ struct queuenode *s; int i; s=init(s); for(i=0;i<5;i++) in_queue(s,i); //printf("%d",s->rear->data); while(!empty(s)){ printf("%3d",out_queue(s)); } }