| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:输入两个单向链表并输出第二个链表
只看楼主 加入收藏
wxh525123
Rank: 2
等 级:论坛游民
帖 子:38
专家分:37
注 册:2010-4-18
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:3 
输入两个单向链表并输出第二个链表
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
    int num;
    float score;
    struct student *next;
    };
int n=0;
void main()
{
    struct student *creat(void);
    void print();
    struct student *heada,*headb;
    printf("input list a;\n");
    heada=creat();
    printf("input list b:\n");
    headb=creat();
    print(headb);
    getch();
    }
struct student *creat()
{
    struct student *head,*p1,*p2;
    printf("input number&score of student:\n");
    p1=p2=(struct student *)malloc(LEN);
    scanf("%ld%f",&p1->num,&p2->score);
    head=NULL;
    while(p1->num!=0)
        {
            n++;
            if(n==1)
                head=p1;
            else
                p2->next=p1;
            p2=p1;
            p1=(struct student *)malloc(LEN);
            scanf("%ld%f",&p1->num,&p1->score);
            }
    p2->next=NULL;
    return head;
    }
void print(struct student *head)
{
    struct student *p;
    p=head;
    if(head==NULL)
        printf("the empty list!\n");
    else
        while(p!=NULL)
            {
                printf("%d %4.2f\n",p->num,p->score);
                p=p->next;
                }
    }
求高人指点一下,为什么第二次调用creat()函数执行错误?是不是只能建立一个链表呢?
谢谢了
搜索更多相关主题的帖子: 链表 输出 输入 
2010-05-13 10:33
yc2575757
Rank: 7Rank: 7Rank: 7
来 自:北京
等 级:黑侠
威 望:1
帖 子:113
专家分:522
注 册:2010-5-7
收藏
得分:15 
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
    int num;
    float score;
    struct student *next;
    };
int n=0;
void main()
{
    struct student *creat(void);
    void print(struct student *head);   //这里也得带参数~~
    struct student *heada,*headb;
    printf("input list a;\n");
    heada=creat();
    printf("input list b:\n");
    headb=creat();
    print(headb);
    getchar();
    }
struct student *creat()
{
    struct student *head,*p1,*p2;
    printf("input number&score of student:\n");
    p1=p2=(struct student *)malloc(LEN);
    scanf("%ld%f",&p1->num,&p2->score);
    head=NULL;
    n = 0;  //这里n要初始化为0,不然继续调用creat,会导致n不从0开始,所以headb没为空~~
    while(p1->num!=0)
        {
            n++;
            if(n==1)
                head=p1;
            else
                p2->next=p1;
            p2=p1;
            p1=(struct student *)malloc(LEN);
            scanf("%ld%f",&p1->num,&p1->score);
            }
    p2->next=NULL;
    return head;
    }
void print(struct student *head)
{
    struct student *p;
    p=head;
    if(head==NULL)
        printf("the empty list!\n");
    else
        while(p!=NULL)
            {
                printf("%d %4.2f\n",p->num,p->score);
                p=p->next;
                }
}
2010-05-13 10:41
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:15 
你用的是什么环境 ,
如果是vc的话,不用费劲自己写这个,
微软提供了,这类现成的链表。
可以直接用省事多了

南国利剑
2010-05-13 10:41
wxh525123
Rank: 2
等 级:论坛游民
帖 子:38
专家分:37
注 册:2010-4-18
收藏
得分:0 
回复 2楼 yc2575757
非常感谢
2010-05-13 13:16
快速回复:输入两个单向链表并输出第二个链表
数据加载中...
 
   



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

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