-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList.c
More file actions
233 lines (205 loc) · 4.52 KB
/
LinkList.c
File metadata and controls
233 lines (205 loc) · 4.52 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "define.h"
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
/*
|-------------------------------------------------------------------------------------------------
|线性表的链式存储结构
|-------------------------------------------------------------------------------------------------
|查找某一位置的元素的时间复杂度为O(n)
|查找某一元素的位置的时间复杂度为O(n)
|
|插入一个元素的时间复杂度为O(n),同时插入多个元素的时间复杂度
|也是O(n)
|
|删除一个元素的时间复杂度为O(n)
|
|插入和删除都要先找到被操作位置的前一个位置
|
*/
/**
* 申请新节点
*/
static PListNode applyNewNode()
{
PListNode new;
new = (ListNode *)malloc(sizeof(ListNode));
if(new == NULL){
printf("Apply new node failed\n");
exit(-1);
}
else return new;
}
/**
* 判断空
*/
static int isEmpty(PLinkList p)
{
if(p->length == 0) return 1;
else return 0;
}
/**
* 初始化
*/
static int initial(PLinkList p)
{
//头指针指向头结点
p->head = applyNewNode();
p->length = 0;
//头结点的下一个为NULL
p->head->next = NULL;
}
/**
* 插入
* 时间复杂度为O(n)
*/
static int insert(PLinkList p)
{
int value, position, i;
//第一个节点
PListNode node = p->head;
PListNode new = applyNewNode();
printf("Input the value you want to insert\n");
scanf("%d", &value);
printf("Input the position that you want to insert\n");
scanf("%d", &position);
new->data = value;
if(isEmpty(p)){
if(position != 1){
printf("The list is empty.The value must insert into the first position\n");
}
new->next = p->head->next;
p->head->next = new;
p->length++;
}
else{
if(position > p->length +1 || position < 0){
printf("Invalid position\n");
exit(0);
}
//找到要插入的位置前一个节点
for(i = 1; i < position; i++) node = node->next;
new->next = node->next;
node->next = new;
p->length++;
}
}
/**
* 删除
* 时间复杂度为O(n)
*/
static int delete(PLinkList p)
{
int position, i;
PListNode preNode = p->head, currentNode;
if(isEmpty(p)){
printf("Empty list\n");
exit(0);
}
printf("Input the position\n");
scanf("%d", &position);
if(position > p->length){
printf("Invalid position.The length of list is %d\n",p->length);
exit(0);
}
for(i = 1; i < position; i++) preNode = preNode->next;
currentNode = preNode->next;
preNode->next = currentNode->next;
currentNode->next = NULL;
p->length--;
free(currentNode);
}
/**
* 寻找某一个位置的值
* 时间复杂度为O(n)
*/
static int get(PLinkList p)
{
int position, i;
PListNode node = p->head;
if(isEmpty(p)){
printf("Empty list");
exit(0);
}
printf("Input the position you want to find\n");
scanf("%d", &position);
if(position > p->length){
printf("Invalid position\n");
exit(-1);
}
for(i = 0; i < position; i++){
node = node->next;
}
printf("Position is %d and value is %d\n", position, node->data);
}
/**
* 寻找某一值得位置
* 时间弄复杂为O(n)
*/
static int getPosition(PLinkList p)
{
int value, i = 0;
PListNode node = p->head;
if(isEmpty(p)){
printf("Empty list\n");
exit(0);
}
printf("Input the value you want to find\n");
scanf("%d", &value);
while((node = node->next)){
if(node->data == value){
i++;
break;
}
i++;
}
if(node == NULL || i == 0){
printf("Do not have this value\n");
exit(0);
}
else{
printf("The value is %d and the position is %d",value,i);
}
}
/**
* 遍历
*/
static int travesal(PLinkList p)
{
//头结点的下一个节点也就是第一个节点
PListNode node = p->head->next;
if(p->head == NULL){
printf("Invalid list\n");
exit(-1);
}
if(isEmpty(p)){
printf("Empty list\n");
exit(0);
}
while (node != NULL){
printf("%d\n",node->data);
node = node->next;
}
}
/**
* 释放资源
*/
static int clear(PLinkList p)
{
free(p);
}
int linkList()
{
LinkList p;
initial(&p);
insert(&p);
insert(&p);
insert(&p);
insert(&p);
insert(&p);
insert(&p);
travesal(&p);
delete(&p);
get(&p);
getPosition(&p);
}