-
Notifications
You must be signed in to change notification settings - Fork 3
/
singly-linked-list.c
87 lines (78 loc) · 2.16 KB
/
singly-linked-list.c
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
//导入 stdbool.h 来使用布尔类型
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
bool insert(struct ListNode *L, int X, int i) { /* 这里默认L有头结点 */
struct ListNode *t, *pre;
int idx = -1;
/* 查找位序为i - 1的结点 */
pre = L; /* pre指向表头 */
while (pre && idx < i - 1) {
pre = pre->next;
idx++;
}
if (pre == NULL) { /* 所找结点不在L中 */
printf("插入位置参数错误\n");
return false;
} else { /* 找到了待插结点的前一个结点pre;若i为0,pre就指向表头 */
/* 插入新结点 */
t = (struct ListNode *)malloc(
sizeof(struct ListNode)); /*申请、填装结点*/
t->val = X;
t->next = pre->next;
pre->next = t;
return true;
}
}
bool delete (struct ListNode *L, int i) { /* 这里默认L有头结点 */
struct ListNode *t, *pre;
int idx = -1;
/* 查找位序为i-1的结点 */
pre = L; /* pre指向表头 */
while (pre && idx < i - 1) {
pre = pre->next;
idx++;
}
if (pre == NULL || idx != i - 1 || pre->next == NULL) {
/* 所找结点或位序为i的结点不在L中 */
printf("删除位置参数错误\n");
return false;
} else { /* 找到了待删结点的前一个结点pre */
/* 将结点删除 */
t = pre->next;
pre->next = t->next;
free(t);
return true;
}
}
void printOut(struct ListNode *L) {
struct ListNode *p = L->next;
while (p) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
void create(struct ListNode *L, int X) {
struct ListNode *t = (struct ListNode *)malloc(sizeof(struct ListNode));
t->val = X;
t->next = L->next;
L->next = t;
}
int main(void) {
struct ListNode *L = (struct ListNode *)malloc(sizeof(struct ListNode));
L->next = NULL;
int a[] = {5, 10, 21, 35, 11};
int k;
for (k = 0; k < 5; k++) create(L, a[4 - k]);
printOut(L);
/*insert(L, 4, 3);
printOut(L);
delete(L, 5);
printOut(L);*/
return 0;
}