链表初尝试-链表的构建与输出-指针

来源:互联网 发布:php curl setcookie 编辑:程序博客网 时间:2024/06/06 16:25

参考:链表基本操作的实现

代码:

//链表creat and output#include<stdio.h>#include<stdlib.h>#define LEN sizeof(struct Student)struct Student{    long num;    double score;    struct Student * next;} ;int n;//统计节点数struct Student*creat(void)//返回一个指向链表头的指针 {    struct Student * head;    struct Student *p1,*p2;//指针类型:struct Student     n=0;    //(begin)    p1=p2=(struct Student *)malloc(LEN);//malloc返回一个指向新开辟空间的指针     scanf("%ld%lf",&p1->num,&p1->score);//输入节点的数据     head=NULL;//此时链表为空     while(p1->num!=0 && p1->score!=0)//开始构建链表 输入0 0时结束    {        //(1)         n++;//节点数增加         if(n==1)head=p1;//开始时头指针指向首节点         else p2->next=p1;//每一个节点含有一个指向下一个节点的指针                //(2)        p2=p1;//移动p2到p1的位置,准备进行对下一个节点的操作(准备操作现在的这个节点的next指向下一个节点)                 //(3)        p1=(struct Student *)malloc(LEN);//p1继续向下一个节点移动         scanf("%ld%lf",&p1->num,&p1->score);//输入下一节点的数据     }    //(4)end    p2->next=NULL;//最后的一个节点的next置空    p1=NULL;    p2=NULL;    return(head);//返回struct Student类型的头指针 } void print(struct Student*head){    struct Student*p;    p=head;        if(head!=NULL)    {        while(p!=NULL)//链表节点next指针指向NULL时结束        {            printf("%ld %lf\n",p->num,p->score);            p=p->next;        }    }}int main()//主函数调用头指针{    struct Student * p;    p=creat();//返回的是链表第一个节点的地址     printf("\nnum:%ld\nscore:%f\n",p->num,p->score);//输出第一个节点的成员值        print(p);//输出链表中的元素         return 0;} 

构建部分:

一.
用C语言实现一个链表的操作,首先定义一个结构体作为节点,开辟节点的空间就需要用到stdlib库里的malloc,使用malloc的时候,需要指明开辟空间的大小,我这里define一个LEN(1)为一个结构体节点的大小,malloc开辟完以后返回的是void类型的指针,这里我们强制转换成结构体节点类型(2)

(1)
#define LEN sizeof(struct Student)

(2)
p1=p2=(struct Student *)malloc(LEN);//malloc返回一个指向新开辟空间的指针


二.
定义Creat函数的类型为:struct Student*,定义一个头指针head指向单链表的首部(不要忘记对头指针的操作,比如置NULL),定义p1,p2(1)。其中p1的功能是在“第一时间来到”新开辟的节点处,输入该节点的值(2)。而p2则慢慢吞吞的走在p1后面,完成连接节点的作用(3),但是虽然p2走的很慢,它始终跟在p1的后面:

(1)
struct Student *p1,*p2;//指针类型:struct Student

(2)
p1=(struct Student *)malloc(LEN);//p1继续向下一个节点移动
scanf("%ld%lf",&p1->num,&p1->score);//输入下一节点的数据

(3)
if(n==1)head=p1;//开始时头指针指向首节点
else p2->next=p1;//每一个节点含有一个指向下一个节点的指针

(4)
p2=p1;//移动p2到p1的位置,准备进行对下一个节点的操作(准备操作现在的这个节点的next指向下一个节点)


三.
当p1走到绝路时,(即输入的是结束的象征0 0),p2不会跟着傻,它会悬崖勒马。
p2->next=NULL;//最后的一个节点的next置空
p2->next的置空象征着链表构建的结束。


构建部分代码:

struct Student*creat(void)//返回一个指向链表头的指针 {    struct Student * head;    struct Student *p1,*p2;//指针类型:struct Student     n=0;    //(begin)    p1=p2=(struct Student *)malloc(LEN);//malloc返回一个指向新开辟空间的指针     scanf("%ld%lf",&p1->num,&p1->score);//输入节点的数据     head=NULL;//此时链表为空     while(p1->num!=0 && p1->score!=0)//开始构建链表 输入0 0时结束    {        //(1)         n++;//节点数增加         if(n==1)head=p1;//开始时头指针指向首节点         else p2->next=p1;//每一个节点含有一个指向下一个节点的指针                //(2)        p2=p1;//移动p2到p1的位置,准备进行对下一个节点的操作(准备操作现在的这个节点的next指向下一个节点)                 //(3)        p1=(struct Student *)malloc(LEN);//p1继续向下一个节点移动         scanf("%ld%lf",&p1->num,&p1->score);//输入下一节点的数据     }    //(4)end    p2->next=NULL;//最后的一个节点的next置空    p1=NULL;    p2=NULL;    return(head);//返回struct Student类型的头指针 } 

输出部分:

void print(struct Student*head){    struct Student*p;    p=head;        if(head!=NULL)    {        while(p!=NULL)//链表节点next指针指向NULL时结束        {            printf("%ld %lf\n",p->num,p->score);            p=p->next;        }    }}

传入头指针,定义一个指针p,将链表的首地址赋值给p(p=head),然后输出当前指向的节点的存储值,然后指向下一个节点,不断的往复。最后当它遇见“悬崖”时“勒马”。

                                                                                                                      2016/3/20

0 0
原创粉丝点击