数据结构的“链队列”,编译出来了,但运行时却出问题了??
这样的程序应该不用多注释了吧。高手帮我调试一下。应该是逻辑错误,运行之后提示:“链队列.exe.已停止工作”。帮忙解决一下。先谢了!
//链队列
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int status;
typedef struct qnode
{
int data;
struct qnode *next;
}linkqlist;
typedef struct
{
linkqlist *front;
linkqlist *rear;
}linkqueue;
status init(linkqueue *p)//构造空队列
{
p->rear=p->front=(linkqlist *)malloc(sizeof(linkqlist));
if(!p->front) exit(0);
p->front->next=NULL;
return 0;
}
status destory(linkqueue *q)//销毁队列
{
while(q->front)
{
q->rear=q->front->next;
free(q->front);
q->front=q->rear;
}
return 0;
}
status empty(linkqueue *q)//判断队列是否为空
{
int v;
if(q->front==q->rear) v=1;
else v=0;
return v;
}
status gethead(linkqueue *q)//返回队头元素
{
int v;
if(q->front==q->rear) v=-1;
else v=q->front->next->data;
return v;
}
status enqueue(linkqueue *q,int e)//插入元素
{
q->rear->next=(linkqlist *)malloc(sizeof(linkqlist));
q->rear=q->rear->next;
if(!q->rear) return -1;///????
q->rear->data=e;
q->rear->next=NULL;
return 0;
}
status dequeue(linkqueue *q)//删除头元素
{
linkqlist *p;
int e;
if(q->front==q->rear)
printf("the linklist is overflow");
else p=q->front->next;
q->front->next=p->next;
e=p->data;
if(q->rear==p) q->rear=q->front;
free(p);
return e;
}
status visit(linkqueue *p)//输出队列
{
linkqlist *q;
q=p->front->next;
printf("display the linklist:\n");
if(p->front==p->rear)
printf("the linklist is empty!");
else
{
while(q)
{ printf("%d->",q->data); q=q->next; }
printf("\n");
}
return 0;
}
int main(linkqueue *head)//主函数
{
int n,i;
int select;
int a,x1,x3,x5,e;
printf("create a empty linkqlist\n");
init(head);
printf("please input linkqlist length:\n");
scanf_s("%d",&n);
for(i=1;i<=n;i++)
{
printf("please input linkqlist value\n");
scanf_s("%d",&a);
enqueue(head,a);
}
visit(head);
printf("select 1======destory()\n");
printf("select 2======empty()\n");
printf("select 3======gethead()\n");
printf("select 4======enqueue()\n");
printf("select 5======dequeue()\n");
printf("please select ( 1====5 ):");
scanf_s("%d",&select);
switch(select)
{
case 1:
{
destory(head);
visit(head);
break;}
case 2:
{
x1=empty(head);
if(x1==1) printf("the linklist is empty!");
else printf("the linklist is full");
break;}
case 3:
{
x3=gethead(head);
printf("head->front->%d\n",x3);
break;}
case 4:
{
printf("please inster the value:");
scanf_s("%d",&e);
enqueue(head,e);
visit(head);
break;}
case 5:
{
x5=dequeue(head);
printf("x5",x5);
visit(head);
break;}
}
return 0;
}