|
2鱼币
- #include<stdio.h>
- #include<stdlib.h>
- typedef char ElemType;
- typedef struct QNode{
- ElemType date;
- struct QNode* next;
- }QNode,*QueuePrt;
- typedef struct{
- QueuePrt front,rear;
- }LinkQueue;
- void InitQueue(LinkQueue *q)
- {
- q->front=(QueuePrt)malloc(sizeof(QNode));
- if(!q->front)
- exit(0);
- q->front=NULL;
- q->rear=q->front;
- }
- void InsetQueue(LinkQueue *q,ElemType c)
- {
- QueuePrt p;
- p=(QueuePrt)malloc(sizeof(QNode));
- if(!p)
- return;
- p->date=c;
- p->next=NULL;
- q->rear->next=p;
- q->rear=p;
- }
- void DelteQueue(LinkQueue *q)
- {
- QueuePrt p;
- if(q->front==q->rear)
- return;
- p=q->front;
- q->front=q->front->next;
- if(p==q->rear)
- {
- q->rear=q->front;
- }
- free(p);
- }
- void DestoryQueue(LinkQueue *q)
- {
- while(q->front)
- {
- q->rear=q->front;
- q->front=q->front->next;
- free(q->rear);
- }
- }
- void DisplayQueue(LinkQueue *q)
- {
- while(q->front)
- {
- printf("%c ",q->front->next->date);
- q->front++;
- }
- }
- int main()
- {
- LinkQueue q;
- ElemType c;
- InitQueue(&q);
- printf("请输入进入队列的数:\n");
- scanf("%c",&c);
- while(c!='#')
- {
- InsetQueue(&q,c);
- scanf("%c",&c);
- }
- DisplayQueue(&q);
- DestoryQueue(&q);
- return 0;
- }
复制代码
第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了
|
最佳答案
查看完整内容
第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了
|