|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本人小白,本想做一个链队,但是出了点问题。
代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct linkqueue
- {
- char Q;
- struct linkqueue *next;
- }linkqueue;
- void initqueue(linkqueue *lq)
- {
- lq = (linkqueue*)malloc(sizeof(struct linkqueue));
- lq->next = NULL;
- if(lq->next)
- {
- printf("error0");
- }
- }
- void enqueue(linkqueue *front,linkqueue *rear,linkqueue *lq,char x)
- {
- lq = (linkqueue*)malloc(sizeof(struct linkqueue));
- if(front->next)
- {
- printf("error1");
- }
- if(rear->next)
- {
- printf("error1");
- }
- if(isnull(front,rear))
- {
- front->next = lq;
- rear->next = lq;
- lq->Q = x;
- lq->next = NULL;
- }
- else
- {
- lq->next = rear->next;
- lq->Q = x;
- rear->next = lq;
- }
- }
- void dequeue(linkqueue *front,linkqueue *rear)
- {
- if(isnull(front,rear))
- {
- printf("队列为空!");
- }
- else
- {
- linkqueue *q = front;
- front = front->next;
- printf("出栈元素为:%c",front->Q);
- free(q);
- }
- }
- int isnull(linkqueue *front,linkqueue *rear)
- {
- if(front->next)
- {
- printf("error2");
- }
- if(rear->next)
- {
- printf("error2");
- }
- if(front->next&&rear->next)
- {
- return 1;
- }
- if(front->next == rear)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int main()
- {
- linkqueue *front,*rear,*lq;
- char x;
- int i;
- initqueue(front);
- initqueue(rear);
- for(i=0;i<5;i++)
- {
- enqueue(front,rear,lq,'a'+i);
- }
- while(isnull(front,rear))
- {
- dequeue(front,rear);
- }
- }
复制代码
实际操作时,入栈操作那里没有执行,调试的时候发现在第一次执行isnull语句的时候(也就是enqueue函数里面执行的时候),并没有return 1 ;
然后我加入error的判断语句,结果如下:(搞不了图片 )
error1error1error2error2
也就是没有error0,就是刚开始lq->next = NULL;是成功了的,但是后面再次调用的时候他又不为NULL了。
求问大佬怎么解决下午要交作业
进出队列要修改,队头和队尾,应改传入&rear,&front
|
|