| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 782 人关注过本帖
标题:求教链表的基础问题
只看楼主 加入收藏
ALU
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-3-11
收藏
 问题点数:0 回复次数:4 
求教链表的基础问题
#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct line)
#define NULL 0
struct line
{
    int a;
    int b;
    int c;
    struct line *next;
};
int n;

struct line *creat(void)
{
    struct line *head;
    struct line *p1,*p2;
    n=0;
    p1=p2=(struct line *)malloc(LEN);
    scanf("%d",&p1->c);
    head=NULL;
    while(p1->c!=0)
    {n=n+1;
    if(n==1)head=p1;
    else p2->next=p1;
    p1=(struct line *)malloc(LEN);
    scanf("%d",&p1->c);
    }
    p2->next=NULL;
    return(head);
}
void main()
{ line *h;
h=creat();
   while(h!=NULL)
   {printf("%d",h->c);
    h=h->next;
   }
}

为何不能将链表完全输出而是只输出第一个数??
如何才能将链表完全输出?
搜索更多相关主题的帖子: 链表 line struct int head 
2008-03-11 11:53
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct line)
#define NULL 0
struct line
{
    char c;
    struct line *next;
};
int n;

struct line *creat(void)
{
    struct line *head;
    struct line *p1,*p2;
    n=0;
    p2=head=(struct line *)malloc(LEN);
    printf("输入第%d个结点:",n);
    scanf("%c",&head->c);
    head->next=NULL;
    while(p2->c!='0')
    {
        n=n+1;
        p1=(struct line *)malloc(LEN);
        p2->next=p1;
        p2=p1;
        printf("输入第%d个结点:",n);
        fflush(stdin);
        scanf("%c",&p1->c);
        
    }
    p2->next=NULL;
    return head;
}
void main()
{ line *h;
  h=creat();
   while(h!=NULL)
   {printf("%c",h->c);
    h=h->next;
   }
}

学习需要安静。。海盗要重新来过。。
2008-03-11 12:38
ALU
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-3-11
收藏
得分:0 
为何这两个程序有不同的结果呢?
2008-03-11 16:01
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
自己分析啊....

学习需要安静。。海盗要重新来过。。
2008-03-11 16:06
ALU
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-3-11
收藏
得分:0 
谢了
2008-03-11 16:15
快速回复:求教链表的基础问题
数据加载中...
 
   



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

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