| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1653 人关注过本帖
标题:有人帮我看下我这个队列为什么不能出队不?
只看楼主 加入收藏
dzy123
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:379
专家分:820
注 册:2013-4-18
结帖率:82%
收藏
已结贴  问题点数:20 回复次数:7 
有人帮我看下我这个队列为什么不能出队不?
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct node {
    int data;
    struct node *next;
};
int main() {
    int k;
    struct node *front,*rear;
    struct node *p=(struct node *)malloc(sizeof(struct node));
    p->next=0;
    front=rear=p;
    for(int i=0; i<3; i++) {
        p->next=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&k);
        p->data=k;
        p->next=0;
        rear->next=p;
        rear=p;

    }
    for(p=front; front!=rear;) {
        printf("%d",p->data);
        front->next=p->next;
        if(p->next==0)rear=front;
        free(p);
    }
    return 0;
}
搜索更多相关主题的帖子: 队列 struct node int next 
2018-05-24 19:12
dzy123
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:379
专家分:820
注 册:2013-4-18
收藏
得分:0 
我自己想出来了谢谢大家.
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct node {
    int data;
    struct node *next;
};
int main() {
    int k;
    struct node *front,*rear,*p;
    rear=(struct node *)malloc(sizeof(struct node));
    rear->next=0;
    front=rear;
    for(int i=0; i<3; i++) {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&k);
        p->data=k;
        p->next=0;
        rear=rear->next=p;
    }
    while(front!=rear) {
        front=front->next;
        printf("%d ",front->data);
        if(front->next==0)rear=front;
        free(front);
    }
    return 0;
}]


[此贴子已经被作者于2018-5-24 20:18编辑过]

2018-05-24 19:58
dzy123
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:379
专家分:820
注 册:2013-4-18
收藏
得分:0 
没人来接下分?
2018-05-25 19:22
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:20 
以下是引用dzy123在2018-5-25 19:22:58的发言:

没人来接下分?

    for(int i=0; i<3; i++) {
        p->next=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&k);
        p->data=k;
        p->next=0;
        rear->next=p;
        rear=p;

    }

这里很明显对p->next赋值重复操作了~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-05-25 19:30
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct node {    
    struct node *next;
    int data;
};
int main() {
    int k;
    struct node *front,*rear,*p;
    rear=(struct node *)malloc(sizeof(struct node));
    rear->next=0;
    front=rear;
    for(int i=0; i<3; i++) {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&k);
        p->data=k;
        p->next=0;
        rear=rear->next=p;
    }
    while(front!=rear) {
        front=front->next;
        printf("%d ",front->data);
        if(front->next==0)rear=front;
        free(front);
    }
    return 0;
}


不知道为啥,我就改了一个地方,这个程序就不能正常输出了,看看是怎么回事?~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-05-25 19:39
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
第一个for循环中,p->next = 0;

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2018-05-25 19:45
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 5楼 九转星河
看错了

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2018-05-25 19:46
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 5楼 九转星河
大哥,你把节点free了,你还想要输出?

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2018-05-25 19:51
快速回复:有人帮我看下我这个队列为什么不能出队不?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018943 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved