当前位置:编程学习 > C/C++ >>

这个链队列不能正常执行。求问怎么回事?

#include<stdio.h> #include <stdlib.h> #include<malloc.h> #define MAX typedef struct node{ char s; struct node *next; }node; typedef struct{ struct node *front; struct node *rear; }Queue; Queue *Init_Queue(Queue *x) { node *y; y=malloc(sizeof(node)); y->next=NULL; x->front=y;x->rear=y; return x; } void In_Queue(Queue *x,char y) { node *p; p=(node *)malloc(sizeof(node)); p->next=NULL; p->s=y; x->rear->next=p; x->rear=p; } int Empty_Queue (Queue *q) { if(q->front==q->rear) return 1; else return 0; } int Out_Queue(Queue *q,char *x) { node *p; if(Empty_Queue){printf("队空!");return 0;} else{ p=q->front->next; q->front->next=p->next; x=p->s; free(p); if(q->front->next==NULL) q->rear=q->front; return x; } } main() { Queue *Q; char s='a'; char *p; Q=Init_Queue(Q); In_Queue(Q,s); In_Queue(Q,s); In_Queue(Q,s); printf("%c\n",Out_Queue(Q,p)); } 报错!貌似没有正确插入节点,因为出队列的时候总是打印对空。
答案:给你改写了,错误有点多。。。没给你11指出,你自己看下吧。有问题再讨论

#include<stdio.h>
#include  <stdlib.h>
#include<malloc.h>
#define MAX
typedef struct node{
char s;
struct node *next;
}node;
typedef struct{
struct node *front;
struct node *rear;
}Queue;
Queue Init_Queue(Queue x)//初始化队列,头尾都赋NULL即可
{
x.front=x.rear=NULL;
return x;
}
void In_Queue(Queue &x,char y)//进队
{
node *p;
p=(node *)malloc(sizeof(node));
p->next=NULL;
p->s=y;
if(x.front==NULL)//队列中没有元素
x.front=x.rear=p;
else
{
x.rear->next=p;
x.rear=p;
}
}
int Empty_Queue (Queue q)
{
if(q.front==NULL) return 1;
else return 0;
}
int Out_Queue(Queue &q,char x)//出队
{
node *p;
if(Empty_Queue(q)){printf("队空!");return 0;}
else{
p=q.front;
q.front=p->next;
x=p->s;
free(p);
return x;
}
}
int main()
{
Queue Q;
char s='a';
Q=Init_Queue(Q);
In_Queue(Q,s);
In_Queue(Q,s);
In_Queue(Q,s);
printf("%c\n",Out_Queue(Q,s));
printf("%c\n",Out_Queue(Q,s));
printf("%c\n",Out_Queue(Q,s));
printf("%c\n",Out_Queue(Q,s));
return 1;
}

上一个:跪求今年九月的的二级c语言上机南开一百题
下一个:c++Windows编程的字体问题,显示乱码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,