| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 350 人关注过本帖
标题:这个链表为啥无法输出啊?求解!
只看楼主 加入收藏
tudou2xigua
Rank: 2
等 级:论坛游民
帖 子:87
专家分:54
注 册:2011-3-20
结帖率:90.32%
收藏
已结贴  问题点数:20 回复次数:2 
这个链表为啥无法输出啊?求解!
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int num;
    struct node*next;
};
void main()
{
    int n=0;
    struct node*head,*p1,*p2,*p;
    p1=p2=(struct node*)malloc(sizeof(struct node));
    head=NULL;
    printf("输入数据:");
    scanf("%d",&p1->num);
    while(p1->num!=0)
    {
        n=n+1;
        if(n==0)
            head=p1;
        else
            p2->next=p1;
        p1=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p1->num);
    }
    p2->next=NULL;
    p=head;
    printf("输出数据:");
    while(p!=NULL)
    {
        printf("%d",p->num);
        p=p->next;
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: include 
2011-06-17 13:15
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:10 
// 这个永远不会被执行
if(n==0) head=p1;


而且楼主添加链表元素的代码写的不对

[ 本帖最后由 voidx 于 2011-6-17 13:38 编辑 ]
2011-06-17 13:35
Toomj
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:257
专家分:1826
注 册:2011-5-17
收藏
得分:10 
在你的基础上改得:
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int num;
    struct node*next;
};
int main()
{
    int n=0;
    struct node*head,*newp,*p;
    head=p=(struct node*)malloc(sizeof(struct node));
    printf("输入数据:");
    scanf("%d",&p->num);
    head=p;
    while(p->num!=0)
    {
        newp=(struct node*)malloc(sizeof(struct node));
        p->next=newp;
        scanf("%d",&newp->num);
        p=newp;
    }
    p=head;
    printf("%d\n",p->num);
    printf("输出数据:");
    while(p->num!=0)
    {
        printf("%d\n",p->num);
        p=p->next;
    }
    return 0;
}
2011-06-17 14:05
快速回复:这个链表为啥无法输出啊?求解!
数据加载中...
 
   



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

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