Skip to content

Commit d6aa5b8

Browse files
Merge pull request wangzheng0822#94 from bkcarlos/master
通用单链表实现提交
2 parents 0a35243 + 834abd6 commit d6aa5b8

File tree

11 files changed

+550
-101
lines changed

11 files changed

+550
-101
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
#include "singleList.h"
2+
3+
#include <string.h>
4+
5+
linkedList * listCreate()
6+
{
7+
linkedList *list = NULL;
8+
list = malloc(sizeof(*list));
9+
if (NULL == list)
10+
{
11+
return NULL;
12+
}
13+
14+
list->dup = NULL;
15+
list->free = NULL;
16+
list->match = NULL;
17+
18+
list->head = NULL;
19+
list->len = 0;
20+
21+
return list;
22+
}
23+
24+
// 释放
25+
void listRelease(linkedList *list)
26+
{
27+
if (NULL == list)
28+
{
29+
return;
30+
}
31+
32+
listEmpty(list);
33+
34+
free(list);
35+
list = NULL;
36+
}
37+
38+
void listEmpty(linkedList *list)
39+
{
40+
if (NULL == list)
41+
{
42+
return;
43+
}
44+
45+
while (NULL != list->head)
46+
{
47+
listNode *pNode = list->head;
48+
list->head = pNode->next;
49+
if (NULL != list->free)
50+
{
51+
list->free(pNode->value);
52+
}
53+
else
54+
{
55+
free(pNode->value);
56+
}
57+
58+
pNode->next = NULL;
59+
free(pNode);
60+
pNode = NULL;
61+
}
62+
}
63+
64+
linkedList * listAddNodeHead(linkedList *list, void * value)
65+
{
66+
if (NULL == list || NULL == value)
67+
{
68+
return list;
69+
}
70+
71+
listNode *node = NULL;
72+
node = malloc(sizeof(*node));
73+
if (NULL == node)
74+
{
75+
return list;
76+
}
77+
78+
node->value = value;
79+
node->next = list->head;
80+
list->head = node;
81+
82+
++list->len;
83+
return list;
84+
}
85+
86+
linkedList * listAddNodeTail(linkedList *list, void *value)
87+
{
88+
if (NULL == list || NULL == value)
89+
{
90+
return list;
91+
}
92+
93+
listNode *node = NULL;
94+
node = malloc(sizeof(*node));
95+
if (NULL == node)
96+
{
97+
return list;
98+
}
99+
100+
node->value = value;
101+
node->next = NULL;
102+
103+
if (NULL == list->head
104+
&& list->len == 0)
105+
{
106+
list->head = node;
107+
}
108+
else
109+
{
110+
listNode *tail = list->head;
111+
listNode *pre = list->head;
112+
while (NULL != tail)
113+
{
114+
pre = tail;
115+
tail = tail->next;
116+
}
117+
118+
pre->next = node;
119+
}
120+
121+
++list->len;
122+
return list;
123+
}
124+
125+
linkedList * listInsertNode(linkedList *list, listNode *old_node, void *value, bool after)
126+
{
127+
if (NULL == list || NULL == old_node)
128+
{
129+
return list;
130+
}
131+
132+
listNode *pNode = NULL;
133+
pNode = malloc(sizeof(*pNode));
134+
if (NULL == pNode)
135+
{
136+
return list;
137+
}
138+
139+
pNode->value = value;
140+
if (after)
141+
{
142+
pNode->next = old_node->next;
143+
old_node->next = pNode;
144+
}
145+
else
146+
{
147+
listNode *pre = list->head;
148+
while (pre->next != old_node)
149+
{
150+
pre = pre->next;
151+
}
152+
153+
if (NULL != pre)
154+
{
155+
pre->next = pNode;
156+
pNode->next = old_node;
157+
}
158+
}
159+
160+
++list->len;
161+
return list;
162+
}
163+
164+
// 没设置释放函数时不做释放处理
165+
void listDelNode(linkedList *list, listNode *node)
166+
{
167+
if (NULL == list || NULL == node)
168+
{
169+
return;
170+
}
171+
172+
listNode *pre = list->head;
173+
listNode *cur = list->head;
174+
while (NULL != cur && cur != node)
175+
{
176+
pre = cur;
177+
cur = cur->next;
178+
}
179+
180+
// 不在该链表中
181+
if (NULL == pre)
182+
{
183+
return;
184+
}
185+
186+
pre->next = node->next;
187+
node->next = NULL;
188+
--list->len;
189+
190+
if (NULL != list->free)
191+
{
192+
list->free(node->value);
193+
free(node);
194+
node = NULL;
195+
}
196+
}
197+
198+
listNode * listSearchKey(linkedList *list, void *key)
199+
{
200+
if (NULL == list)
201+
{
202+
return NULL;
203+
}
204+
205+
listNode *node = list->head;
206+
while (NULL != node)
207+
{
208+
if (NULL != list->match)
209+
{
210+
if (list->match(key, node->value) == 0)
211+
{
212+
return node;
213+
}
214+
}
215+
else
216+
{
217+
if (key == node->value)
218+
{
219+
return node;
220+
}
221+
}
222+
223+
node = node->next;
224+
}
225+
226+
return NULL;
227+
}
228+
229+
listNode * listIndex(linkedList *list, long index)
230+
{
231+
if (NULL == list)
232+
{
233+
return NULL;
234+
}
235+
236+
if (index <= 0
237+
|| index > list->len)
238+
{
239+
return NULL;
240+
}
241+
242+
listNode *pNode = list->head;
243+
for (long i = 0; i < index; ++i)
244+
{
245+
pNode = pNode->next;
246+
}
247+
248+
return pNode;
249+
}
250+
251+
linkedList* listRewind(linkedList *list)
252+
{
253+
if (NULL == list)
254+
{
255+
return NULL;
256+
}
257+
258+
listNode *head = list->head;
259+
listNode *pre = NULL;
260+
listNode *next = NULL;
261+
while (NULL != head)
262+
{
263+
next = head->next;
264+
head->next = pre;
265+
pre = head;
266+
head = next;
267+
}
268+
269+
list->head = pre;
270+
return list;
271+
}
272+
273+
size_t listLength(linkedList *list)
274+
{
275+
if (NULL == list)
276+
{
277+
return 0;
278+
}
279+
280+
return list->len;
281+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef __SINGLELIST_H__
2+
#define __SINGLELIST_H__
3+
4+
#include <stdlib.h>
5+
#include <stdbool.h>
6+
7+
typedef struct listNode
8+
{
9+
struct listNode *next;
10+
void *value;
11+
}listNode;
12+
13+
typedef struct linkedList
14+
{
15+
listNode *head;
16+
size_t len;
17+
size_t typesize;
18+
19+
void(*dup)(void*, void*);
20+
int(*match)(void*, void*);
21+
void(*free)(void*);
22+
}linkedList;
23+
24+
#define listSetDupMethod(l,m) ((l)->dup = (m))
25+
#define listSetFreeMethod(l,m) ((l)->free = (m))
26+
#define listSetMatchMethod(l,m) ((l)->match = (m))
27+
28+
#define listGetDupMethod(l) ((l)->dup)
29+
#define listGetFree(l) ((l)->free)
30+
#define listGetMatchMethod(l) ((l)->match)
31+
32+
linkedList *listCreate();
33+
void listRelease(linkedList *list);
34+
void listEmpty(linkedList *list);
35+
linkedList *listAddNodeHead(linkedList *list, void *value);
36+
linkedList *listAddNodeTail(linkedList *list, void *value);
37+
linkedList *listInsertNode(linkedList *list, listNode *old_node, void *value, bool after);
38+
void listDelNode(linkedList *list, listNode *node);
39+
40+
listNode *listSearchKey(linkedList *list, void *key);
41+
listNode *listIndex(linkedList *list, long index);
42+
linkedList* listRewind(linkedList *list);
43+
44+
size_t listLength(linkedList *list);
45+
46+
#endif // !__SINGLELIST_H__

java/11_sorts/Sorts.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,31 @@ public static void insertionSort(int[] a, int n) {
4949
// 选择排序,a表示数组,n表示数组大小
5050
public static void selectionSort(int[] a, int n) {
5151
if (n <= 1) return;
52+
<<<<<<< HEAD
53+
for (int i = 0; i < n; ++i) {
54+
// 查找最小值
55+
int minIndex = i;
56+
int minValue = a[i];
57+
for (int j = i; j < n; ++j) {
58+
if (a[j] < minValue) {
59+
minValue = a[j];
60+
=======
5261
for (int i = 0; i < n - 1; ++i) {
5362
// 查找最小值
5463
int minIndex = i;
5564
for (int j = i + 1; j < n; ++j) {
5665
if (a[j] < a[minIndex]) {
66+
>>>>>>> upstream/master
5767
minIndex = j;
5868
}
5969
}
6070

71+
<<<<<<< HEAD
72+
=======
6173
if (minIndex == i)
6274
continue;
6375

76+
>>>>>>> upstream/master
6477
// 交换
6578
int tmp = a[i];
6679
a[i] = a[minIndex];

0 commit comments

Comments
 (0)