-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.cpp
More file actions
292 lines (272 loc) · 4.91 KB
/
Link.cpp
File metadata and controls
292 lines (272 loc) · 4.91 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define DEBUG_PRINT printf("%s %d:head is NULL\n", __FILE__, __LINE__)
#define FILEPATH "data.bin"
using namespace std;
struct link
{
int data;
struct link* next;
};
//创建头节点
struct link* Create(void)
{
struct link* p = (struct link*)malloc(sizeof(struct link));
if (NULL == p)
DEBUG_PRINT;
memset(p, 0, sizeof(struct link));
return p;
}
//创建节点
struct link* CreateNode(int data)
{
struct link* p = (struct link*)malloc(sizeof(struct link));
if (NULL == p)
{
DEBUG_PRINT;
return NULL;
}
memset(p, 0, sizeof(struct link));
p->data = data;
return p;
}
//插入一个节点(头插法)
void InsertHeadNode(struct link* head, int data)
{
struct link* node = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return;
}
node = CreateNode(data);
node->next = head->next;
head->next = node;
}
//插入一个节点(尾插法)
void InsertTailNode(struct link* head, int data)
{
struct link* p = NULL, * node = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return;
}
node = CreateNode(data);
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = node;
node->next = NULL;
}
//查找节点
struct link* FindNode(struct link* head, int data)
{
struct link* p = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return NULL;
}
p = head;
while (p->next != NULL)
{
if (p->next->data == data)
{
return p;
}
p = p->next;
}
return NULL;
}
//删除节点
void DeleteNode(struct link* head, int data)
{
struct link* pdel = NULL, * p = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return;
}
p = FindNode(head, data);
if (NULL == p)
{
printf("没有%d这个数据!\n", data);
return;
}
pdel = p->next;
p->next = pdel->next;
free(pdel);
}
//链表排序
void SortLink(struct link* head)
{
struct link* p = NULL, * q = NULL, * pos = NULL;
int temp = 0;
if (NULL == head || head->next == NULL)
{
printf("%s %d:head or head->next is NULL\n", __FILE__, __LINE__);
return;
}
for (p = head->next; p->next != NULL; p = p->next)
{
for (pos = p, q = p->next; q != NULL; q = q->next)
{
if (q->data < pos->data) //if(pos->data > q->data)
{
pos = q;
}
}
if (pos != p)
{
temp = p->data;
p->data = pos->data;
pos->data = temp;
}
}
}
//遍历打印
void Display(struct link* head)
{
struct link* p = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return;
}
p = head->next;
while (p != NULL)
{
printf("p:%p,p->data:%d,p->next:%p\n", p, p->data, p->next);
p = p->next;
}
}
//释放所有链表节点
void FreeLinkNode(struct link* head)
{
struct link* p = NULL, * q = NULL;
if (NULL == head)
{
DEBUG_PRINT;
return;
}
p = head->next;
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
free(head);
//head = NULL;
//p = NULL;
//q = NULL;
}
//将链表数据写入文件
void SaveToFile(const char* const path, struct link* head)
{
FILE* fp = NULL;
struct link* p = NULL;
if (NULL == head || NULL == path)
{
printf("%s %d:head or file is NULL!\n", __FILE__, __LINE__);
return;
}
fopen_s(&fp, FILEPATH, "wb");
//if (NULL == fp)
//{
// perror("file open failed in load");
// return;
//}
p = head->next;
while (NULL != p)
{
fwrite(&p->data, sizeof(p->data), 1, fp);
p = p->next;
}
if (ferror(fp))
{
perror("Load file error!\n");
}
fclose(fp);
fp = NULL;
}
//从文件加载数据
void LoadFromFile(const char* path, struct link* head)
{
int data = 0;
FILE* fp = NULL;
if (NULL == head || NULL == path)
{
printf("%s %d:head or file is NULL!\n", __FILE__, __LINE__);
return;
}
//fp = fopen(path, "rb");
//if (NULL == fp)
//{
// perror("file open failed in load");
// return;
//}
fopen_s(&fp, FILEPATH, "rb");
if (NULL == fp)
{
printf("文件为空,没有加载到内容!\n");
return;
}
while (fread(&data, sizeof(data), 1, fp) > 0)
{
InsertTailNode(head, data);
}
if (ferror(fp)) {
perror("Load file error!\n");
}
fclose(fp);
fp = NULL;
}
int main(void)
{
int i = 0, data = 0;
struct link* p = NULL;
struct link* head = Create();
LoadFromFile(FILEPATH, head);
for (i = 0; i < 10; i++)
{
//data = i + 1; //rand()%100;
data = rand() % 100; //随机数
InsertTailNode(head, data);
//InsertHeadNode(head, data);
}
Display(head);
//删除
printf("请输入要删除的数:");
scanf_s(" %d", &data);
DeleteNode(head, data);
printf("删除后:\n");
Display(head);
//查找
printf("请输入要查找的数:");
scanf_s(" %d", &data);
p = FindNode(head, data);
if (p != NULL)
{
printf("已查找到数据%d!\n", data);
}
else {
printf("没有找到%d的数据!\n", data);
}
//排序
SortLink(head);
printf("排序后:\n");
Display(head);
SaveToFile(FILEPATH, head);
FreeLinkNode(head);
}