数据结构------链表
作者:互联网
链表分类
- 单向、双向
- 有头、无头
- 有循环、无循环
- 可以随机组合
链表与顺序表的区别
- 顺序表的空间是一段连续的空间,而链表的空间为不连续空间
- 顺序表支持随机访问,链表不支持随机访问
- 在任意位置插入/删除元素时,顺序表没有链表方便
- 顺序表不需要频繁申请空间,而链表需要,因为链表容易造成空间碎片
无头单向不循环链表的相关接口实现
1 #pragma once
2 #include <stdio.h>
3 #include <assert.h>
4 #include <stdlib.h>
5
6 typedef struct Node
7 {
8 int value;
9 struct Node *next;
10 } Node;
11
12 typedef struct Llist
13 {
14 Node *first;
15 } Llist;
16
17 void LinklistInit(Llist *llist)
18 {
19 llist->first = NULL;
20 }
21
22 void Linklistdestory(Llist *llist )
23 {
24 assert(llist);
25 Node *cur = llist->first;
26 while (cur != NULL)
27 {
28 Node *next = cur->next;
29 free(cur);
30 cur = next;
31 }
32 }
33
34 void LinklistPushFront(Llist *llist , int v)
35 {
36 Node *node = (Node*)malloc(sizeof(Node));
37 node->value = v;
38
39 node->next = llist->first;
40 llist->first = node;
41 }
42
43 void LinklistPushBack(Llist *llist ,int v)
44 {
45 Node *node = (Node*)malloc(sizeof(Node));
46 node->value = v;
47 node->next = NULL;
48 if(llist->first == NULL)
49 {
50 llist->first = node;
51 }
52 else{
53 Node *cur = llist->first;
54 while(cur->next !=NULL)
55 {
56 cur = cur->next;
57 }
58 cur->next = node;
59 }
60 }
61
62 void LinklistPopBack(Llist *s)
63 {
64 assert(s->first != NULL);
65 if(s->first->next == NULL)
66 {
67 free(s->first);
68 s->first = NULL;
69 return;
70 }
71 Node *c;
72 for(c = s->first; c->next->next != NULL ; c = c->next );
73 free(c->next);
74 c->next = NULL;
75 }
76
77 void LinklistPopFront(Llist *s)
78 {
79 assert(s);
80 Node *c = s->first->next;
81 free(s->first);
82 s->first = c;
83 }
84
85 Node *LinklistFind(Llist *s , int v)
86 {
87 for(Node *c = s->first ; c != NULL ; c = c->next)
88 {
89 if(c->value == v)
90 {
91 return c;
92 }
93 }
94 }
95
96 void LinklistInset(Node *pos , int v)
97 {
98 Node *node = (Node*)malloc(sizeof(Node));
99 node->value = v;
100 node->next = pos->next;
101 pos->next = node;
102 }
103
104 void LinklistErase(Node *pos)
105 {
106 Node *next = pos->next->next;
107 free(pos->next);
108 pos->next = next;
109 }
110
111 void LinklistRemoveAll(Llist *s, int v)
112 {
113 if(s->first == NULL)
114 {
115 return;
116 }
117 if(s->first->value == v)
118 {
119 Node *c = s->first->next;
120 s->first = c;
121 free(s->first);
122 }
123 Node *c = s->first;
124 while(c->next != NULL)
125 {
126 if(c->next->value == v)
127 {
128 Node *next = c->next;
129 c->next = c->next->next;
130 free(next);
131 }
132 else{
133 c = c->next;
134 }
135 }
136 }
标签:Node,node,NULL,next,链表,llist,数据结构,first 来源: https://blog.csdn.net/weixin_43512021/article/details/100807839