首页 技术 正文
技术 2022年11月17日
0 收藏 942 点赞 2,203 浏览 9997 个字

这里实现的单链表,可以存储任意数据类型,支持增、删、改、查找、插入等基本操作。(本文提供的是完整代码,可能有些长。)

下面是头文件:

 #ifndef SLIST_H
#define SLIST_H #ifdef __cplusplus
extern "C" {
#endif #define NODE_T(ptr, type) ((type*)ptr) struct slist_node {
struct slist_node * next;
}; typedef void (*list_op_free_node)(struct slist_node *node);
/*
* return 0 on hit key, else return none zero
*/
typedef int (*list_op_key_hit_test)(struct slist_node *node, void *key); struct single_list {
/* all the members must not be changed manually by callee */
struct slist_node * head;
struct slist_node * tail;
int size; /* length of the list, do not change it manually*/ /* free method to delete the node
*/
void (*free_node)(struct slist_node *node);
/*
* should be set by callee, used to locate node by key(*_by_key() method)
* return 0 on hit key, else return none zero
*/
int (*key_hit_test)(struct slist_node *node, void *key); struct single_list *(*add)(struct single_list * list, struct slist_node * node);
struct single_list *(*insert)(struct single_list * list, int pos, struct slist_node *node);
/* NOTE: the original node at the pos will be freed by free_node */
struct single_list *(*replace)(struct single_list *list, int pos, struct slist_node *node);
struct slist_node *(*find_by_key)(struct single_list *, void * key);
struct slist_node *(*first)(struct single_list* list);
struct slist_node *(*last)(struct single_list* list);
struct slist_node *(*at)(struct single_list * list, int pos);
struct slist_node *(*take_at)(struct single_list * list, int pos);
struct slist_node *(*take_by_key)(struct single_list * list, void *key);
struct single_list *(*remove)(struct single_list * list, struct slist_node * node);
struct single_list *(*remove_at)(struct single_list *list, int pos);
struct single_list *(*remove_by_key)(struct single_list *list, void *key);
int (*length)(struct single_list * list);
void (*clear)(struct single_list * list);
void (*deletor)(struct single_list *list);
}; struct single_list * new_single_list(list_op_free_node op_free, list_op_key_hit_test op_cmp); #ifdef __cplusplus
}
#endif #endif // SLIST_H

struct single_list 这个类,遵循我们前面介绍的基本原则,不再一一细说。有几点需要提一下:

  • 我们定义了 slist_node 作为链表节点的基类,用户自定义的节点,都必须从 slist_node 继承
  • 为了支持节点( node )的释放,我们引入一个回调函数 list_op_free_node ,这个回调需要在创建链表时传入
  • 为了支持查找,引入另外一个回调函数 list_op_key_hit_test

好了,下面看实现文件:

 #include "slist.h"
#include <malloc.h> static struct single_list * _add_node(struct single_list *list, struct slist_node *node)
{ if(list->tail)
{
list->tail->next = node;
node->next = ;
list->tail = node;
list->size++;
}
else
{
list->head = node;
list->tail = node;
node->next = ;
list->size = ;
} return list;
} static struct single_list * _insert_node(struct single_list * list, int pos, struct slist_node *node)
{
if(pos < list->size)
{
int i = ;
struct slist_node * p = list->head;
struct slist_node * prev = list->head;
for(; i < pos; i++)
{
prev = p;
p = p->next;
}
if(p == list->head)
{
/* insert at head */
node->next = list->head;
list->head = node;
}
else
{
prev->next = node;
node->next = p;
} if(node->next == ) list->tail = node;
list->size++;
}
else
{
list->add(list, node);
} return list;
} static struct single_list * _replace(struct single_list * list, int pos, struct slist_node *node)
{
if(pos < list->size)
{
int i = ;
struct slist_node * p = list->head;
struct slist_node * prev = list->head;
for(; i < pos; i++)
{
prev = p;
p = p->next;
}
if(p == list->head)
{
/* replace at head */
node->next = list->head->next;
list->head = node;
}
else
{
prev->next = node;
node->next = p->next;
} if(node->next == ) list->tail = node; if(list->free_node) list->free_node(p);
else free(p);
} return list;
} static struct slist_node * _find_by_key(struct single_list *list, void * key)
{
if(list->key_hit_test)
{
struct slist_node * p = list->head;
while(p)
{
if(list->key_hit_test(p, key) == ) return p;
p = p->next;
}
}
return ;
} static struct slist_node *_first_of(struct single_list* list)
{
return list->head;
} static struct slist_node *_last_of(struct single_list* list)
{
return list->tail;
} static struct slist_node *_node_at(struct single_list * list, int pos)
{
if(pos < list->size)
{
int i = ;
struct slist_node * p = list->head;
for(; i < pos; i++)
{
p = p->next;
}
return p;
} return ;
} static struct slist_node * _take_at(struct single_list * list, int pos)
{
if(pos < list->size)
{
int i = ;
struct slist_node * p = list->head;
struct slist_node * prev = p;
for(; i < pos ; i++)
{
prev = p;
p = p->next;
}
if(p == list->head)
{
list->head = p->next;
if(list->head == ) list->tail = ;
}
else if(p == list->tail)
{
list->tail = prev;
prev->next = ;
}
else
{
prev->next = p->next;
} list->size--; p->next = ;
return p;
} return ;
} static struct slist_node * _take_by_key(struct single_list * list, void *key)
{
if(list->key_hit_test)
{
struct slist_node * p = list->head;
struct slist_node * prev = p;
while(p)
{
if(list->key_hit_test(p, key) == ) break;
prev = p;
p = p->next;
} if(p)
{
if(p == list->head)
{
list->head = p->next;
if(list->head == ) list->tail = ;
}
else if(p == list->tail)
{
list->tail = prev;
prev->next = ;
}
else
{
prev->next = p->next;
} list->size--; p->next = ;
return p;
}
}
return ;
} static struct single_list *_remove_node(struct single_list * list, struct slist_node * node)
{
struct slist_node * p = list->head;
struct slist_node * prev = p;
while(p)
{
if(p == node) break;
prev = p;
p = p->next;
} if(p)
{
if(p == list->head)
{
list->head = list->head->next;
if(list->head == ) list->tail = ;
}
else if(p == list->tail)
{
prev->next = ;
list->tail = prev;
}
else
{
prev->next = p->next;
} if(list->free_node) list->free_node(p);
else free(p); list->size--;
}
return list;
} static struct single_list *_remove_at(struct single_list *list, int pos)
{
if(pos < list->size)
{
int i = ;
struct slist_node * p = list->head;
struct slist_node * prev = p;
for(; i < pos ; i++)
{
prev = p;
p = p->next;
}
if(p == list->head)
{
list->head = p->next;
if(list->head == ) list->tail = ;
}
else if(p == list->tail)
{
list->tail = prev;
prev->next = ;
}
else
{
prev->next = p->next;
} if(list->free_node) list->free_node(p);
else free(p); list->size--;
} return list;
} static struct single_list *_remove_by_key(struct single_list *list, void *key)
{
if(list->key_hit_test)
{
struct slist_node * p = list->head;
struct slist_node * prev = p;
while(p)
{
if(list->key_hit_test(p, key) == ) break;
prev = p;
p = p->next;
} if(p)
{
if(p == list->head)
{
list->head = list->head->next;
if(list->head == ) list->tail = ;
}
else if(p == list->tail)
{
prev->next = ;
list->tail = prev;
}
else
{
prev->next = p->next;
} if(list->free_node) list->free_node(p);
else free(p); list->size--;
}
} return list;
} static int _length_of(struct single_list * list)
{
return list->size;
} static void _clear_list(struct single_list * list)
{
struct slist_node * p = list->head;
struct slist_node * p2;
while(p)
{
p2 = p;
p = p->next; if(list->free_node) list->free_node(p2);
else free(p2);
} list->head = ;
list->tail = ;
list->size = ;
} static void _delete_single_list(struct single_list *list)
{
list->clear(list);
free(list);
} struct single_list * new_single_list(list_op_free_node op_free, list_op_key_hit_test op_cmp)
{
struct single_list *list = (struct single_list *)malloc(sizeof(struct single_list));
list->head = ;
list->tail = ;
list->size = ;
list->free_node = op_free;
list->key_hit_test = op_cmp; list->add = _add_node;
list->insert = _insert_node;
list->replace = _replace;
list->find_by_key = _find_by_key;
list->first = _first_of;
list->last = _last_of;
list->at = _node_at;
list->take_at = _take_at;
list->take_by_key = _take_by_key;
list->remove = _remove_node;
list->remove_at = _remove_at;
list->remove_by_key = _remove_by_key;
list->length = _length_of;
list->clear = _clear_list;
list->deletor = _delete_single_list; return list;
}

上面的代码就不一一细说了,下面是测试代码:

 /* call 1 or N arguments function of struct */
#define ST_CALL(THIS,func,args...) ((THIS)->func(THIS,args)) /* call none-arguments function of struct */
#define ST_CALL_0(THIS,func) ((THIS)->func(THIS)) struct int_node {
struct slist_node node;
int id;
}; struct string_node {
struct slist_node node;
char name[];
}; static int int_free_flag = ;
static void _int_child_free(struct slist_node *node)
{
free(node);
if(!int_free_flag)
{
int_free_flag = ;
printf("int node free\n");
}
} static int _int_slist_hittest(struct slist_node * node, void *key)
{
struct int_node * inode = NODE_T(node, struct int_node);
int ikey = (int)key;
return (inode->id == ikey ? : );
} static int string_free_flag = ;
static void _string_child_free(struct slist_node *node)
{
free(node);
if(!string_free_flag)
{
string_free_flag = ;
printf("string node free\n");
}
} static int _string_slist_hittest(struct slist_node * node, void *key)
{
struct string_node * sn = (struct string_node*)node;
return strcmp(sn->name, (char*)key);
} void int_slist_test()
{
struct single_list * list = new_single_list(_int_child_free, _int_slist_hittest);
struct int_node * node = ;
struct slist_node * bn = ;
int i = ; printf("create list && nodes:\n");
for(; i < ; i++)
{
node = (struct int_node*)malloc(sizeof(struct int_node));
node->id = i;
if(i%)
{
list->add(list, node);
}
else
{
list->insert(list, , node);
}
}
printf("create 100 nodes end\n----\n");
printf("first is : %d, last is: %d\n----\n",
NODE_T( ST_CALL_0(list, first), struct int_node )->id,
NODE_T( ST_CALL_0(list, last ), struct int_node )->id); assert(list->size == ); printf("list traverse:\n");
for(i = ; i < ; i++)
{
if(i% == ) printf("\n");
bn = list->at(list, i);
node = NODE_T(bn, struct int_node);
printf(" %d", node->id);
}
printf("\n-----\n"); printf("find by key test, key=42:\n");
bn = list->find_by_key(list, (void*));
assert(bn != );
node = NODE_T(bn, struct int_node);
printf("find node(key=42), %d\n------\n", node->id); printf("remove node test, remove the 10th node:\n");
bn = list->at(list, );
node = NODE_T(bn, struct int_node);
printf(" node 10 is: %d\n", node->id);
printf(" now remove node 10\n");
list->remove_at(list, );
printf(" node 10 was removed, check node 10 again:\n");
bn = list->at(list, );
node = NODE_T(bn, struct int_node);
printf(" now node 10 is: %d\n------\n", node->id); printf("replace test, replace node 12 with id 1200:\n");
bn = list->at(list, );
node = NODE_T(bn, struct int_node);
printf(" now node 12 is : %d\n", node->id);
node = (struct int_node*)malloc(sizeof(struct int_node));
node->id = ;
list->replace(list, , node);
bn = list->at(list, );
node = NODE_T(bn, struct int_node);
printf(" replaced, now node 12 is : %d\n----\n", node->id); printf("test remove:\n");
ST_CALL(list, remove, bn);
bn = ST_CALL(list, find_by_key, (void*));
assert(bn == );
printf("test remove ok\n----\n");
printf("test remove_by_key(90):\n");
ST_CALL(list, remove_by_key, (void*));
bn = ST_CALL(list, find_by_key, (void*));
assert(bn == );
printf("test remove_by_key(90) end\n----\n");
printf("test take_at(80):\n");
bn = ST_CALL(list, take_at, );
printf(" node 80 is: %d\n", NODE_T(bn, struct int_node)->id);
free(bn);
printf("test take_at(80) end\n"); int_free_flag = ;
printf("delete list && nodes:\n");
list->deletor(list);
printf("delete list && nodes end\n");
printf("\n test add/insert/remove/delete/find_by_key/replace...\n");
} void string_slist_test()
{
struct single_list * list = new_single_list(_string_child_free, _string_slist_hittest);
} void slist_test()
{
int_slist_test();
string_slist_test();
}

测试代码里主要演示了:

  • 自定义链表节点类型
  • 定义释放回调
  • 定义用于查找的 hit test 回调
  • 如何创建链表
  • 如何使用( add 、remove 、 take 、find 、 insert 等)

相信到这里,单链表的使用已经不成问题了。

以单链表为基础,可以进一步实现很多数据结构,比如树(兄弟孩子表示法),比如 key-value 链表等等。接下来根据例子的需要,会择机进行展示。

转自:http://blog.csdn.net/foruok/article/details/18594177

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918