forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
通用单链表实现提交(注意自己管理内存)
- Loading branch information
Showing
2 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
#include "singleList.h" | ||
|
||
#include <string.h> | ||
|
||
linkedList * listCreate() | ||
{ | ||
linkedList *list = NULL; | ||
list = malloc(sizeof(*list)); | ||
if (NULL == list) | ||
{ | ||
return NULL; | ||
} | ||
|
||
list->dup = NULL; | ||
list->free = NULL; | ||
list->match = NULL; | ||
|
||
list->head = NULL; | ||
list->len = 0; | ||
|
||
return list; | ||
} | ||
|
||
// 释放 | ||
void listRelease(linkedList *list) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return; | ||
} | ||
|
||
listEmpty(list); | ||
|
||
free(list); | ||
list = NULL; | ||
} | ||
|
||
void listEmpty(linkedList *list) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return; | ||
} | ||
|
||
while (NULL != list->head) | ||
{ | ||
listNode *pNode = list->head; | ||
list->head = pNode->next; | ||
if (NULL != list->free) | ||
{ | ||
list->free(pNode->value); | ||
} | ||
else | ||
{ | ||
free(pNode->value); | ||
} | ||
|
||
pNode->next = NULL; | ||
free(pNode); | ||
pNode = NULL; | ||
} | ||
} | ||
|
||
linkedList * listAddNodeHead(linkedList *list, void * value) | ||
{ | ||
if (NULL == list || NULL == value) | ||
{ | ||
return list; | ||
} | ||
|
||
listNode *node = NULL; | ||
node = malloc(sizeof(*node)); | ||
if (NULL == node) | ||
{ | ||
return list; | ||
} | ||
|
||
node->value = value; | ||
node->next = list->head; | ||
list->head = node; | ||
|
||
++list->len; | ||
return list; | ||
} | ||
|
||
linkedList * listAddNodeTail(linkedList *list, void *value) | ||
{ | ||
if (NULL == list || NULL == value) | ||
{ | ||
return list; | ||
} | ||
|
||
listNode *node = NULL; | ||
node = malloc(sizeof(*node)); | ||
if (NULL == node) | ||
{ | ||
return list; | ||
} | ||
|
||
node->value = value; | ||
node->next = NULL; | ||
|
||
if (NULL == list->head | ||
&& list->len == 0) | ||
{ | ||
list->head = node; | ||
} | ||
else | ||
{ | ||
listNode *tail = list->head; | ||
listNode *pre = list->head; | ||
while (NULL != tail) | ||
{ | ||
pre = tail; | ||
tail = tail->next; | ||
} | ||
|
||
pre->next = node; | ||
} | ||
|
||
++list->len; | ||
return list; | ||
} | ||
|
||
linkedList * listInsertNode(linkedList *list, listNode *old_node, void *value, bool after) | ||
{ | ||
if (NULL == list || NULL == old_node) | ||
{ | ||
return list; | ||
} | ||
|
||
listNode *pNode = NULL; | ||
pNode = malloc(sizeof(*pNode)); | ||
if (NULL == pNode) | ||
{ | ||
return list; | ||
} | ||
|
||
pNode->value = value; | ||
if (after) | ||
{ | ||
pNode->next = old_node->next; | ||
old_node->next = pNode; | ||
} | ||
else | ||
{ | ||
listNode *pre = list->head; | ||
while (pre->next != old_node) | ||
{ | ||
pre = pre->next; | ||
} | ||
|
||
if (NULL != pre) | ||
{ | ||
pre->next = pNode; | ||
pNode->next = old_node; | ||
} | ||
} | ||
|
||
++list->len; | ||
return list; | ||
} | ||
|
||
// 没设置释放函数时不做释放处理 | ||
void listDelNode(linkedList *list, listNode *node) | ||
{ | ||
if (NULL == list || NULL == node) | ||
{ | ||
return; | ||
} | ||
|
||
listNode *pre = list->head; | ||
listNode *cur = list->head; | ||
while (NULL != cur && cur != node) | ||
{ | ||
pre = cur; | ||
cur = cur->next; | ||
} | ||
|
||
// 不在该链表中 | ||
if (NULL == pre) | ||
{ | ||
return; | ||
} | ||
|
||
pre->next = node->next; | ||
node->next = NULL; | ||
--list->len; | ||
|
||
if (NULL != list->free) | ||
{ | ||
list->free(node->value); | ||
free(node); | ||
node = NULL; | ||
} | ||
} | ||
|
||
listNode * listSearchKey(linkedList *list, void *key) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return NULL; | ||
} | ||
|
||
listNode *node = list->head; | ||
while (NULL != node) | ||
{ | ||
if (NULL != list->match) | ||
{ | ||
if (list->match(key, node->value) == 0) | ||
{ | ||
return node; | ||
} | ||
} | ||
else | ||
{ | ||
if (key == node->value) | ||
{ | ||
return node; | ||
} | ||
} | ||
|
||
node = node->next; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
listNode * listIndex(linkedList *list, long index) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return NULL; | ||
} | ||
|
||
if (index <= 0 | ||
|| index > list->len) | ||
{ | ||
return NULL; | ||
} | ||
|
||
listNode *pNode = list->head; | ||
for (long i = 0; i < index; ++i) | ||
{ | ||
pNode = pNode->next; | ||
} | ||
|
||
return pNode; | ||
} | ||
|
||
linkedList* listRewind(linkedList *list) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return NULL; | ||
} | ||
|
||
listNode *head = list->head; | ||
listNode *pre = NULL; | ||
listNode *next = NULL; | ||
while (NULL != head) | ||
{ | ||
next = head->next; | ||
head->next = pre; | ||
pre = head; | ||
head = next; | ||
} | ||
|
||
list->head = pre; | ||
return list; | ||
} | ||
|
||
size_t listLength(linkedList *list) | ||
{ | ||
if (NULL == list) | ||
{ | ||
return 0; | ||
} | ||
|
||
return list->len; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef __SINGLELIST_H__ | ||
#define __SINGLELIST_H__ | ||
|
||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
typedef struct listNode | ||
{ | ||
struct listNode *next; | ||
void *value; | ||
}listNode; | ||
|
||
typedef struct linkedList | ||
{ | ||
listNode *head; | ||
size_t len; | ||
size_t typesize; | ||
|
||
void(*dup)(void*, void*); | ||
int(*match)(void*, void*); | ||
void(*free)(void*); | ||
}linkedList; | ||
|
||
#define listSetDupMethod(l,m) ((l)->dup = (m)) | ||
#define listSetFreeMethod(l,m) ((l)->free = (m)) | ||
#define listSetMatchMethod(l,m) ((l)->match = (m)) | ||
|
||
#define listGetDupMethod(l) ((l)->dup) | ||
#define listGetFree(l) ((l)->free) | ||
#define listGetMatchMethod(l) ((l)->match) | ||
|
||
linkedList *listCreate(); | ||
void listRelease(linkedList *list); | ||
void listEmpty(linkedList *list); | ||
linkedList *listAddNodeHead(linkedList *list, void *value); | ||
linkedList *listAddNodeTail(linkedList *list, void *value); | ||
linkedList *listInsertNode(linkedList *list, listNode *old_node, void *value, bool after); | ||
void listDelNode(linkedList *list, listNode *node); | ||
|
||
listNode *listSearchKey(linkedList *list, void *key); | ||
listNode *listIndex(linkedList *list, long index); | ||
linkedList* listRewind(linkedList *list); | ||
|
||
size_t listLength(linkedList *list); | ||
|
||
#endif // !__SINGLELIST_H__ |