课外天地 李树青学习天地C语言程序设计 → [推荐]第十一课代码讲义:链表及其节点创建


  共有12649人关注过本帖树形打印复制链接

主题:[推荐]第十一课代码讲义:链表及其节点创建

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1939 积分:26594 威望:0 精华:34 注册:2003/12/30 16:34:32
[推荐]第十一课代码讲义:链表及其节点创建  发帖心情 Post By:2015/12/7 10:49:51 [只看该作者]


链表:
#include <stdio.h>
#define   NULL    0
struct  student
{
    long num;
    float  score;
    struct  student *next;
};

void main()
{
    struct student a,b,c,*head,*p;
    a.num=99101;
    a.score=89.5;
    b.num=99102;
    b.score=90;
    c.num=99103;
    c.score=85;
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    while(p!=NULL)
    {
        printf("%ld  %5.1f\n",p->num,p->score);
        p=p->next;
    }
}

 

生成链表1:
思路:
head=p1=p2=(struct student *)malloc(LEN);

scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;


scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;


scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;


scanf("%ld,%f",&p1->num, &p1->score);
p2->next=NULL;

代码:
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)

struct student
{
    long num;
    float score;
    struct student *next;
};

void main()
{
    struct student *creat(void);
    struct student *p=creat();
    while(p!=NULL)
    {
        printf("%ld  %5.1f\n",p->num,p->score);
        p=p->next;
    }
}

struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    head=p1=p2=(struct student *)malloc(LEN);
    while(1)
    {
        scanf("%ld,%f",&p1->num, &p1->score);
        if(p1->num==0)
        {
            if(head==p1)
                head=NULL;
            else
                p2->next=NULL;
            break;
        }
        else
            p2=p1;
        p1=(struct student *)malloc(LEN);
        p2->next=p1;
    }
    return(head);
}

 


生成链表2:
思路:
p1=p2=(struct student *)malloc(LEN);
head=NULL

scanf("%ld,%f",&p1->num, &p1->score);

head=p1
p2=p1
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);

p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);

p2->next=NULL;

代码:
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)

struct student
{
    long num;
    float score;
    struct student *next;
};

void main()
{
    struct student *creat(void);
    struct student *p=creat();
    while(p!=NULL)
    {
        printf("%ld  %5.1f\n",p->num,p->score);
        p=p->next;
    }
}

struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    int n=0;
    p1=p2=(struct student *) malloc(LEN);
    scanf("%ld,%f",&p1->num, &p1->score);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        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);
}

 

 

 

[此贴子已经被作者于2015-12-12 09:04:41编辑过]

 回到顶部