| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:向各位求助,关于链表的问题
只看楼主 加入收藏
王捉熊
Rank: 2
等 级:论坛游民
帖 子:46
专家分:61
注 册:2012-10-23
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
向各位求助,关于链表的问题
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *next;
};
int n;
struct student *creat()
{
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=p2=(struct student*)malloc(LEN);
    scanf("%ld,%f",&p1->num,&p2->score);
    head=NULL;
    while (p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;////////
        p2=p1;//////////////////问题(1)想不通程序怎么通过p2对该链表进行扩增的
        p1=(struct student *)malloc(LEN);
        scanf("%ld,%f",&p1->num,&p1->score);
    }
    p1->next=NULL;
    return (head);
}
void print(struct student *head)
{
    struct student *p;
    printf("\nNow,These %d records are :\n",n);
    p=head;
    if(head!=NULL)
        do
        {
            printf("%ld %5.1lf\n",p->num,p->score);
            p=p->next;
        }while(p!=NULL);
}
void main()
{
    struct student *head;
    head=creat();
    print(head);
}
这是谭老先生编的书上的程序,不过我有一个问题!问题在上面了,请各位多多帮助!
搜索更多相关主题的帖子: next head include 
2013-03-23 20:24
王捉熊
Rank: 2
等 级:论坛游民
帖 子:46
专家分:61
注 册:2012-10-23
收藏
得分:0 
请大家都发表点意见!

新手上路,说错请勿见怪!
2013-03-23 21:22
ldzy
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:64
专家分:339
注 册:2012-7-30
收藏
得分:20 
p2->next=p1;//p2的后缀指向p1,则p2,p1连起来了。(1)
 p2=p1;//p2指针向后走,指向原本p1指向的单元。(2)
p1=(struct student *)malloc(LEN);//p1指向新生成的单元。现在跳到第一步(1)。
2013-03-25 11:15
快速回复:向各位求助,关于链表的问题
数据加载中...
 
   



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

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