-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.c
More file actions
199 lines (188 loc) · 3.8 KB
/
LinkedList.c
File metadata and controls
199 lines (188 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
1,单链表
2,链表遍历,增删查改
3,双向链表
4,操作系统链表的应用
1,
#include<stdio.h>
typedef struct node{
int data;
struct node *pNext;
}node;
node *create_node(int data)
{
node *p = (node *)malloc(sizeof(node));
if(p == NULL){
printf("malloc error!\n");
return NULL;
}
}
void insert_tail(node * ph,node * new)
{
node *p = ph;
while(NULL != p->pNext){
p = p->pNext;
};
p->pNext = new;
}
int main()
{
node * ph =create_tail(1);
insert_tail(ph,create_tail(2));
insert_tail(ph,create_tail(3));
printf("node1 data:%d\n",ph->data);
printf("dode2 data:%d\n",ph->pNext->data);
printf("node3 data:%d\n",ph->pNext->pNext->data);
}
//无头节点
void list_for_each1(ndoe *ph)
{
node * p =ph;
while(NULL != p->pNext){
printf("node data:%d\n",p->data);
p=p->pNext;
}
printf("node data:%d\n",p->data);
}
//有头节点
void list_for_each2(node *ph)
{
node * p = ph;
while(NULL != p ){
p = p -> pNext;
printf("node data:%d\n",p->data);
}
}
//增删查改
void delete_node(node *pH, int data)
{
node *p = pH;
node *pPrev = NULL;
while(NULL != p->pNext){
pPrev = p;
p = p->pNext;
if (p->data == data){
if(NULL == p->pNext){
pPrev->pNext = NULL;
free(p);
}
else{
pPrev->pNext = p->pNext;
free(p);
}
return 0;
}
}
printf("没有要删除的节点\n");
return -1;
}
//双向链表
typedef struct node{
int data;
struct node *pNext;
struct node *pPrev;
}node;
node * create_node(int data)
{
node * p = (node *)malloc(sizeof(node));
if(NULL == p){
printf("malloc error\n");
return NULL;
}
p->data = data;
p->pPrev = NULL;
p->pNext = NULL;
return p;
}
void insert_tail(node * pH, node * new)
{
struct node *p = pH;
while(NULL != p->pNext){
p = p->pNext;
}
p->pNext = new;
new->pPrev = p;
}
void insert_head(node * pH, node * new)
{
new->pNext = pH->pNext;
if(NULL != pH->pNext)
pH->pNext->pPrev = new;
pH->pNext = new;
new->pPrev = pH;
}
//正向遍历
struct node * list_for_each(node * pH)
{
node * p = pH;
if(NULL == p){
return NUll;
}
while(NULL != p->pNext){
p = p->pNext;
printf("data = %d.\n",p->data);
}
return p;
}
//逆向遍历
#inlcude<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
node * pPrev;
node * pNext;
}node;
node * list_for_each(node * pH);
void list_for_reverse(node * pTail)
{
node * p = pTail;
while(NULL != p->pPrev){
printf("data = %d.\n",p->data);
p = p->pPrev;
}
}
int main()
{
node * pHeader = create_node(0);
insert_tail(pHeader,create_node(0));
insert_tail(pHeader,create_node(1));
insert_tail(pHeader,create_node(2));
printf("正向遍历:\n");
node * pTail = list_for_each(pHeader);
printf("逆向遍历:\n");
list_for_reverse(pTail);
return 0;
}
//删除节点
int delete_node(node * pH, int data)
{
node * p = pH;
if(NULL == p)
return -1;
while(NULL != p->pNext){
p = p->pNext;
if(p->data = data){
if(NULL == p->pNext){
p->pPrev->pNext = NULL;
}
else{
p->pPrev->pNext = p->pNext;
p->pNext->pPrev = p->pPrev;
}
free(p);
return 0;
}
}
printf("未找到要删除的节点.\n");
return -1;
}
双向链表的删除与实际应用
链表的实际应用
struct list_head{
struct list_head *next,*prev;
}
#define LIST_HEAD_INIT(name) {&(name),&(name)}
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list){ //inline内联函数,解决频繁调用的小函数大量消耗栈空间(栈内存)的问题,使用时函数体和函数声明必须结合在一起。
list->next = list;
list->prev = list;
}//define的使用