您的位置:首页 > 其它

单向链表的创建和遍历

2017-08-08 15:40 543 查看

单向链表

简介

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;

链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;

链表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向nuLL的指针。

创建单向链表

一个简单的单向链表



创建过程

说明:

图中的p为我代码中的p1

p1为我代码中的p2

首先定义一个结构体

struct node
19 {
20     int data;//存储数据
21     struct node *next;//指向下一节点的指针
22 };


建立节点

第一步

p1 = (struct node *)malloc(LEN);//创建第一个节点
34     p2 = p1;//如果创建成功,把第一个节点保存下来
35     if(p1==NULL)//创建失败则返回0
36     {
37         printf("creat fail\n");
38         return 0;
39     }
40     else
41     {
42         head = NULL;//头指针赋值为0
43         printf("please input %d node\n",n+1);//打印这是第几个节点
44         scanf("%d",&(p1->data));//向第一个节点输入数据
45     }




第二步

n = 1;
48     head = p1;//让头指针指向第一个节点
49     p2->next = NULL;//现在还没有创建第二个节点,因此第一个节点的next指向为空
50
51     p2 = p1;//将p1保存到p2
52
53     p1 = (struct node *)malloc(LEN);//p1指向新创建的节点
54     printf("Please input %d node\n ", n+1);
55     scanf("%d",&(p1->data));//向新节点输入数据




第三步

57     p2->next = p1;//在第一个节点和第二个节点间建立连接




依次向下进行,创建整个链表

代码示例,创建三个节点

1 /*********************************************************************************
2  *      Copyright:  (C) 2017 fanmaolin<fanmaolinn@gmail.com>
3  *                  All rights reserved.
4  *
5  *       Filename:  make2.c
6  *    Description:  This file
7  *
8  *        Version:  1.0.0(08/05/2017)
9  *         Author:  fanmaolin <fanmaolinn@gmail.com>
10  *      ChangeLog:  1, Release initial version on "08/05/2017 03:06:33 AM"
11  *
12  ********************************************************************************/
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #define LEN sizeof(struct node)
18 struct node
19 {
20     int data;//输入数据
21     struct node *next;//指向下一个节点
22 };
23
24 int n;//统计是第几个节点
25
26 int main(void)
27 {
28     struct node *head;//头节点指针
29     struct node *p1 = NULL;//p1保存创建的新节点的地址
30     struct node *p2 = NULL;//p2保存原链表最后一个节点的地址
31
32     n = 0;
33     p1 = (struct node *)malloc(LEN);//新建一个节点
34     p2 = p1;
35     if(p1==NULL)//创建节点失败
36     {
37         printf("creat fail\n");
38         return 0;
39     }
40     else//创建节点成功
41     {
42         head = NULL;
43         printf("please input %d node\n",n+1);
44         scanf("%d",&(p1->data));//输入数据
45     }
46
47     n = 1;
48     head = p1;//头指针指向第一个节点
49     p2->next = NULL;//因为没有创建下一个节点,所以第一个节点的next指向为空
50
51     p2 = p1;
52
53     p1 = (struct node *)malloc(LEN);//新建第二个节点
54     printf("Please input %d node\n ", n+1);
55     scanf("%d",&(p1->data));
56
57     p2->next = p1;//第一个节点的next指向第二个节点,把第一个节点和第二个节点连接
58
59     p2 = p1;
make2.c
60
61     n = 2;
62     p1 = (struct node *)malloc(LEN);//新建第三个节点
63     printf("Please input %d node\n", n+1);
64     scanf("%d", &(p1->data));
65
66     p2->next = p1;//把第二个节点与第三个节点建立连接
67     p2 = p1;
68
69     p2->next = NULL;//第三个节点为最后一个节点,所以它的next指向为空
70
71
72    //p1 = NULL;
73 /*输出节点内容*/
74     struct node *p;
75     p = head;//从第一个节点开始
76
77     printf("head is %o\n", head);
78     do
79     {
80     printf("p is %o ,data is  %d, pnext is %o\n", p, p->data, p->next);//分别输出p的地址,data数据,下一个节点的地址
81     p = p->next;//指向下一个节点
82
83     }while(p != NULL);
84
85
86     free(p1);//释放占用的内存
87     p1 = NULL;//防止p1变为野指针
88 }


执行结果:

[fanmaolin@Centeros lianbiao]$ gcc make2.c
[fanmaolin@Centeros lianbiao]$ ./a.out
please input 1 node
1
Please input 2 node
2
Please input 3 node
3
head is 74710020
p is 74710020 ,data is  1, pnext is 74710060
p is 74710060 ,data is  2, pnext is 74710120
p is 74710120 ,data is  3, pnext is 0


总结:

上面是不带头结点的单向链表的创建方式,节点的创建最重要的是理清思路,从头结点开始,最后一个节点p2->next必须为空,表示一个链表的结束,还要注意释放内存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息