鱼C论坛

 找回密码
 立即注册
查看: 4325|回复: 5

这样创建和打印两个链表a和b有问题吗?

[复制链接]
发表于 2011-3-25 23:51:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{
        int num;
        int score;
        struct student *next;
};
int n=0;

struct student *creat()//创建链表
{
        struct student *head;
        struct student *p1,*p2;
        p1=p2=(struct student *)malloc(LEN);
        scanf("%d,%d",&p1->num,&p1->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("%d,%d",&p1->num,&p1->score);
        }
        p2->next=NULL;
        return head;
}

void print(struct student *head)//输出函数
{
        struct student *p;
        p=head;
        if(head!=NULL)
        {
                while(p!=NULL)
                {
                        printf("%-6d%5d\n",p->num,p->score);
                        p=p->next;
                }
        }
}

void main()
{
        struct student *a,*b;
        printf("input LL a here:\n");
        a=creat();
        printf("input LL b here:\n");
        b=creat();
        printf("LL a:\n");
        print(a);
        printf("LL b:\n");
        print(b);
}
得到这个结果,不知道为什么链表b无法输出呢?是输入函数的问题还是输出函数的问题?
未命名.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-3-26 11:49:38 | 显示全部楼层
回复 1# zhangtpku


    今天的视频正准备讲输出了~
    被你抢先一步了~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-3-26 14:17:56 | 显示全部楼层
回复 2# 小甲鱼
期待视频啊,自学完了的做题老错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-3-26 23:29:00 | 显示全部楼层
继续求解答,还是没搞明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-3-26 23:34:31 | 显示全部楼层
终于懂了!想了一整天了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-8-18 21:29:46 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-6 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表