Skip to content

Commit

Permalink
Merge branch 'master' into notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam0205 committed Oct 26, 2018
2 parents 72dc277 + c2e136f commit 7ef7e8b
Show file tree
Hide file tree
Showing 30 changed files with 1,263 additions and 106 deletions.
2 changes: 1 addition & 1 deletion c-cpp/05_array/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int insert(struct array *array, int elem)
}

if (idx < array->used)
memmove(&array->arr[array->used], &array->arr[idx],
memmove(&array->arr[idx+1], &array->arr[idx],
(array->used - idx) * sizeof(int));

array->arr[idx] = elem;
Expand Down
281 changes: 281 additions & 0 deletions c-cpp/06_linkedlist/singlelist_gc/singleList.c
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;
}
46 changes: 46 additions & 0 deletions c-cpp/06_linkedlist/singlelist_gc/singleList.h
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__
Loading

0 comments on commit 7ef7e8b

Please sign in to comment.