C语言单向链表:创建&&打印#ShadowFox个人作品

链表是很多人学习c语言的一个难点,本人刚好学到这一点,花了一些时间弄懂之后,想把它分享出来,以造福后来人。

由于水平实在有限,知识体系也不完全,如果有错误,请联系我,我会尽快改正。

*****************************************************************************************************************

链表是一种能够动态存储数据的一种数据结构。它能够跳跃式存储数据,可以方便数据的操作。


1.定义链表节点

struct note{

    char name[20];

    int heartlevel;

    struct note *next;

};

我们定义一个名为note的结构。结构内有我们想要存储的数据类型与一个结构指针。这个指针是对链表操作的关键。这里,我想要存储数据生物名称【name】与生物血量【heartlevel】两种数据。


2.头指针

根据我的理解,对一个链表操作的入口就是链表的头指针。对头指针的操作我们就可以对整个链表操作。我们在主函数内定义剪标的头指针head,并把它赋值为空值。

    struct note *head;

    head=NULL;


3.链表创建函数
这是一个形参为链表头指针,返回值为处理过的链表头指针的函数。先把代码放上来,我会一一说明。

struct note *create(struct note *head){

    struct note *p1=NULL,*p2=NULL;

    char name[20];

    int n;

    while(scanf("%s",name)!=EOF){

        p1=(struct note*)malloc(sizeof(struct note));

        strcpy(p1->name,name);

        scanf("%d",&n);

        p1->heartlevel=n;

        p1->next=NULL;

        if(head==NULL)

            head=p1;

        else

            p2->next=p1;

        p2=p1;

    }

    return head;

}

这里的关键是指针p1与p2,我的理解是p2更像是一个记录者,而p1是操作者。
先把p1与p2赋值为空。
 用

    char name[20];

    int n;

来承载输入的数据。
  当输入不为EOF时,即有输入时,p1来申请一个新的节点(malloc为内存申请函数,返回申请空间的头指针)。请注意:最后有一句p2=p1;,这意味着这里p1指向新节点后p2仍保存着p1原来的值,这是一个关键。
我们继续,将输入的数据放在p1申请的新节点后,p1->next赋值为NULL,因为我们不知道p1是不是最后一个节点,如果是最后一个节点,那么其不应该存在指向其他地方的指针。
  最后这个if语句是个关键,它分了两种情况:头指针是NULL或不是NULL。如果头指针是NULL,那么证明这一遍while循环是创建第一个节点的循环,头指针应该指向第一个节点的位置,即head=p1;。如果头指针不是NULL,那么这是一个创建中间节点的过程。大家是否记得上文:“p1指向新节点后p2仍保存着p1原来的值”。p2仍保存着上一个节点的值,上一个节点的next通过p2->next指向p1,即实现了本个节点与上一个节点的链接。最后p2=p1,p2存储了p1的值,然后p1进行下一个节点的操作。
  本函数返回值是创建好的链表第一个节点的指针,即此链表的头指针。

4.链表的打印
  单向链表只要知道其头指针即可对整个链表进行操作。我们创建一个形参为头指针,返回值为空的函数print
先放代码:

void print(struct note *head){

    struct note *temp;

    temp=head;

    while(temp!=NULL){

        printf("name=%s ,heartlevel=%d\n",temp->name,temp->heartlevel);

        temp=temp->next;

    }

}

为了改变头指针,我们创建一个临时指针temp来操作
  因为链表最后一个节点的next是NULL,我们只要让temp==NULL时停止循环即可
temp不断读取当前节点数据,然后指向下一节点,即可完成对整个链表的输出。

因为水平问题,可能有一些错误或短视的地方,请谅解。

附上全部代码

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct note{

    char name[20];

    int heartlevel;

    struct note *next;

};


struct note *create(struct note *head){

    struct note *p1=NULL,*p2=NULL;

    char name[20];

    int n;

    while(scanf("%s",name)!=EOF){

        p1=(struct note*)malloc(sizeof(struct note));

        strcpy(p1->name,name);

        scanf("%d",&n);

        p1->heartlevel=n;

        p1->next=NULL;

        if(head==NULL)

            head=p1;

        else

            p2->next=p1;

        p2=p1;

    }

    return head;

}


void print(struct note *head){

    struct note *temp;

    temp=head;

    while(temp!=NULL){

        printf("name=%s ,heartlevel=%d\n",temp->name,temp->heartlevel);

        temp=temp->next;

    }

}


int main(int argc, const char * argv[]) {

    struct note *head;

    head=NULL;

    head=create(head);

    print(head);

    return 0;

}


猜你喜欢

转载自blog.csdn.net/shadowfox_/article/details/78938804