以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  C语言程序设计  (http://www.njcie.com/bbs/list.asp?boardid=29)
----  [推荐]第十一课代码讲义:链表及其节点创建  (http://www.njcie.com/bbs/dispbbs.asp?boardid=29&id=1599)

--  作者:admin
--  发布时间: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编辑过]