-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinklist.cpp
executable file
·143 lines (120 loc) · 3.06 KB
/
Linklist.cpp
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
/*************************************************************************
> File Name: Linklist.cpp
> Author: sunowsir
> Mail: [email protected]
> Created Time: 2018年10月11日 星期四 21时48分09秒
************************************************************************/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode {
int data;
struct LinkNode *next;
} LinkNode;
typedef struct LinkList {
LinkNode head;
int length;
} LinkList;
LinkList *init() {
LinkList *p = (LinkList *)malloc(sizeof(LinkList));
p->head.next = NULL;
p->length = 0;
return p;
}
LinkNode *getNewNode(int value) {
LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
p->data = value;
p->next = NULL;
return p;
}
void insert(LinkList *l, int value, int ind) {
// 创建虚拟节点
LinkNode *p = &(l->head);
// 找到需要插入的位置的前一个节点
while (ind--) {
p = p->next;
if (p == NULL) {
return ;
}
}
// 创建一个新的节点;
LinkNode *insert_node = getNewNode(value);
// 让新节点的next指向虚拟节点的下一个节点,更新头结点,让头结点的next指向链表头
insert_node->next = p->next;
p->next = insert_node;
//链表长度加1
l->length += 1;
return ;
}
// delete
void erase(LinkList *l, int ind) {
LinkNode *p = &(l->head);
while(ind--) {
p = p->next;
if (p == NULL) {
return ;
}
}
if (p->next == NULL) {
return ;
}
LinkNode *delete_node = p->next;
p->next = p->next->next;
free(delete_node);
l->length -= 1;
return ;
}
void clear(LinkList *l) {
if (l->head.next == NULL) {
return ;
}
LinkNode *now_node = l->head.next, *free_node;
while (now_node) {
free_node = now_node;
now_node = now_node->next;
free(free_node);
}
free(l);
return ;
}
void output(LinkList *l) {
printf("[%.2d] ", l->length);
LinkNode *output_node = l->head.next;
while (output_node) {
printf(" %d -> ", output_node->data);
output_node = output_node->next;
}
printf(" NULL\n");
return ;
}
int main() {
srand(time(0));
LinkList *l = init();
#define MAX_OP 20
for (int i = 0; i < MAX_OP; i++) {
int op = rand() % 4, value, ind;
switch (op) {
case 0 :
case 1 :
case 2 : {
ind = rand() % (l->length + 1);
value = rand() % 100;
printf("insert(%d, %d) to LinkList\n", value, ind);
insert(l, value, ind);
output(l);
} break;
case 3 : {
if (l->length == 0) {
break;
}
ind = rand() % l->length;
printf("erase(%d) from LinkList\n", ind);
erase(l, ind);
output(l);
} break;
}
}
#undef MAX_OP
clear(l);
return 0;
}