Skip to content

Commit

Permalink
Merge pull request wangzheng0822#94 from bkcarlos/master
Browse files Browse the repository at this point in the history
 通用单链表实现提交
  • Loading branch information
wangzheng0822 authored Oct 25, 2018
2 parents 0a35243 + 834abd6 commit d6aa5b8
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 101 deletions.
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__
13 changes: 13 additions & 0 deletions java/11_sorts/Sorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,31 @@ public static void insertionSort(int[] a, int n) {
// 选择排序,a表示数组,n表示数组大小
public static void selectionSort(int[] a, int n) {
if (n <= 1) return;
<<<<<<< HEAD
for (int i = 0; i < n; ++i) {
// 查找最小值
int minIndex = i;
int minValue = a[i];
for (int j = i; j < n; ++j) {
if (a[j] < minValue) {
minValue = a[j];
=======
for (int i = 0; i < n - 1; ++i) {
// 查找最小值
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (a[j] < a[minIndex]) {
>>>>>>> upstream/master
minIndex = j;
}
}

<<<<<<< HEAD
=======
if (minIndex == i)
continue;

>>>>>>> upstream/master
// 交换
int tmp = a[i];
a[i] = a[minIndex];
Expand Down
Loading

0 comments on commit d6aa5b8

Please sign in to comment.