c语言面向对象表情包,C语言面向对象编程(五):单链表实现

前面我们介绍了如何在 C 语言中引入面向对象语言的一些特性来进行面向对象编程,从本篇开始,我们使用前面提到的技巧,陆续实现几个例子,最后呢,会提供一个基本的 http server 实现(使用 libevent )。在这篇文章里,我们实现一个通用的数据结构:单链表。

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

下面是头文件:

#ifndef SLIST_H

#define SLIST_H

#ifdef __cplusplus

extern"C"{

#endif

#define NODE_T(ptr, type) ((type*)ptr)

structslist_node {

structslist_node * next;

};

typedefvoid(*list_op_free_node)(structslist_node *node);

/*

* return 0 on hit key, else return none zero

*/

typedefint(*list_op_key_hit_test)(structslist_node *node,void*key);

structsingle_list {

/* all the members must not be changed manually by callee */

structslist_node * head;

structslist_node * tail;

intsize;/* length of the list, do not change it manually*/

/* free method to delete the node

*/

void(*free_node)(structslist_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)(structslist_node *node,void*key);

structsingle_list *(*add)(structsingle_list * list,structslist_node * node);

structsingle_list *(*insert)(structsingle_list * list,intpos,structslist_node *node);

/* NOTE: the original node at the pos will be freed by free_node */

structsingle_list *(*replace)(structsingle_list *list,intpos,structslist_node *node);

structslist_node *(*find_by_key)(structsingle_list *,void* key);

structslist_node *(*first)(structsingle_list* list);

structslist_node *(*last)(structsingle_list* list);

structslist_node *(*at)(structsingle_list * list,intpos);

structslist_node *(*take_at)(structsingle_list * list,intpos);

structslist_node *(*take_by_key)(structsingle_list * list,void*key);

structsingle_list *(*remove)(structsingle_list * list,structslist_node * node);

structsingle_list *(*remove_at)(structsingle_list *list,intpos);

structsingle_list *(*remove_by_key)(structsingle_list *list,void*key);

int(*length)(structsingle_list * list);

void(*clear)(structsingle_list * list);

void(*deletor)(structsingle_list *list);

};

structsingle_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 

staticstructsingle_list * _add_node(structsingle_list *list,structslist_node *node)

{

if(list->tail)

{

list->tail->next = node;

node->next = 0;

list->tail = node;

list->size++;

}

else

{

list->head = node;

list->tail = node;

node->next = 0;

list->size = 1;

}

returnlist;

}

staticstructsingle_list * _insert_node(structsingle_list * list,intpos,structslist_node *node)

{

if(pos size)

{

inti = 0;

structslist_node * p = list->head;

structslist_node * prev = list->head;

for(; 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 == 0) list->tail = node;

list->size++;

}

else

{

list->add(list, node);

}

returnlist;

}

staticstructsingle_list * _replace(structsingle_list * list,intpos,structslist_node *node)

{

if(pos size)

{

inti = 0;

structslist_node * p = list->head;

structslist_node * prev = list->head;

for(; 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 == 0) list->tail = node;

if(list->free_node) list->free_node(p);

elsefree(p);

}

returnlist;

}

staticstructslist_node * _find_by_key(structsingle_list *list,void* key)

{

if(list->key_hit_test)

{

structslist_node * p = list->head;

while(p)

{

if(list->key_hit_test(p, key) == 0)returnp;

p = p->next;

}

}

return0;

}

staticstructslist_node *_first_of(structsingle_list* list)

{

returnlist->head;

}

staticstructslist_node *_last_of(structsingle_list* list)

{

returnlist->tail;

}

staticstructslist_node *_node_at(structsingle_list * list,intpos)

{

if(pos size)

{

inti = 0;

structslist_node * p = list->head;

for(; i 

{

p = p->next;

}

returnp;

}

return0;

}

staticstructslist_node * _take_at(structsingle_list * list,intpos)

{

if(pos size)

{

inti = 0;

structslist_node * p = list->head;

structslist_node * prev = p;

for(; i 

{

prev = p;

p = p->next;

}

if(p == list->head)

{

list->head = p->next;

if(list->head == 0) list->tail = 0;

}

elseif(p == list->tail)

{

list->tail = prev;

prev->next = 0;

}

else

{

prev->next = p->next;

}

list->size--;

p->next = 0;

returnp;

}

return0;

}

staticstructslist_node * _take_by_key(structsingle_list * list,void*key)

{

if(list->key_hit_test)

{

structslist_node * p = list->head;

structslist_node * prev = p;

while(p)

{

if(list->key_hit_test(p, key) == 0)break;

prev = p;

p = p->next;

}

if(p)

{

if(p == list->head)

{

list->head = p->next;

if(list->head == 0) list->tail = 0;

}

elseif(p == list->tail)

{

list->tail = prev;

prev->next = 0;

}

else

{

prev->next = p->next;

}

list->size--;

p->next = 0;

returnp;

}

}

return0;

}

staticstructsingle_list *_remove_node(structsingle_list * list,structslist_node * node)

{

structslist_node * p = list->head;

structslist_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 == 0) list->tail = 0;

}

elseif(p == list->tail)

{

prev->next = 0;

list->tail = prev;

}

else

{

prev->next = p->next;

}

if(list->free_node) list->free_node(p);

elsefree(p);

list->size--;

}

returnlist;

}

staticstructsingle_list *_remove_at(structsingle_list *list,intpos)

{

if(pos size)

{

inti = 0;

structslist_node * p = list->head;

structslist_node * prev = p;

for(; i 

{

prev = p;

p = p->next;

}

if(p == list->head)

{

list->head = p->next;

if(list->head == 0) list->tail = 0;

}

elseif(p == list->tail)

{

list->tail = prev;

prev->next = 0;

}

else

{

prev->next = p->next;

}

if(list->free_node) list->free_node(p);

elsefree(p);

list->size--;

}

returnlist;

}

staticstructsingle_list *_remove_by_key(structsingle_list *list,void*key)

{

if(list->key_hit_test)

{

structslist_node * p = list->head;

structslist_node * prev = p;

while(p)

{

if(list->key_hit_test(p, key) == 0)break;

prev = p;

p = p->next;

}

if(p)

{

if(p == list->head)

{

list->head = list->head->next;

if(list->head == 0) list->tail = 0;

}

elseif(p == list->tail)

{

prev->next = 0;

list->tail = prev;

}

else

{

prev->next = p->next;

}

if(list->free_node) list->free_node(p);

elsefree(p);

list->size--;

}

}

returnlist;

}

staticint_length_of(structsingle_list * list)

{

returnlist->size;

}

staticvoid_clear_list(structsingle_list * list)

{

structslist_node * p = list->head;

structslist_node * p2;

while(p)

{

p2 = p;

p = p->next;

if(list->free_node) list->free_node(p2);

elsefree(p2);

}

list->head = 0;

list->tail = 0;

list->size = 0;

}

staticvoid_delete_single_list(structsingle_list *list)

{

list->clear(list);

free(list);

}

structsingle_list * new_single_list(list_op_free_node op_free, list_op_key_hit_test op_cmp)

{

structsingle_list *list = (structsingle_list *)malloc(sizeof(structsingle_list));

list->head = 0;

list->tail = 0;

list->size = 0;

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;

returnlist;

}

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

/* 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))

structint_node {

structslist_node node;

intid;

};

structstring_node {

structslist_node node;

charname[16];

};

staticintint_free_flag = 0;

staticvoid_int_child_free(structslist_node *node)

{

free(node);

if(!int_free_flag)

{

int_free_flag = 1;

printf("int node free\n");

}

}

staticint_int_slist_hittest(structslist_node * node,void*key)

{

structint_node * inode = NODE_T(node,structint_node);

intikey = (int)key;

return(inode->id == ikey ? 0 : 1);

}

staticintstring_free_flag = 0;

staticvoid_string_child_free(structslist_node *node)

{

free(node);

if(!string_free_flag)

{

string_free_flag = 1;

printf("string node free\n");

}

}

staticint_string_slist_hittest(structslist_node * node,void*key)

{

structstring_node * sn = (structstring_node*)node;

returnstrcmp(sn->name, (char*)key);

}

voidint_slist_test()

{

structsingle_list * list = new_single_list(_int_child_free, _int_slist_hittest);

structint_node * node = 0;

structslist_node * bn = 0;

inti = 0;

printf("create list && nodes:\n");

for(; i 

{

node = (structint_node*)malloc(sizeof(structint_node));

node->id = i;

if(i%10)

{

list->add(list, node);

}

else

{

list->insert(list, 1, node);

}

}

printf("create 100 nodes end\n----\n");

printf("first is : %d, last is: %d\n----\n",

NODE_T( ST_CALL_0(list, first), structint_node )->id,

NODE_T( ST_CALL_0(list, last ), structint_node )->id);

assert(list->size == 100);

printf("list traverse:\n");

for(i = 0; i 

{

if(i%10 == 0) printf("\n");

bn = list->at(list, i);

node = NODE_T(bn, structint_node);

printf(" %d", node->id);

}

printf("\n-----\n");

printf("find by key test, key=42:\n");

bn = list->find_by_key(list, (void*)42);

assert(bn != 0);

node = NODE_T(bn, structint_node);

printf("find node(key=42), %d\n------\n", node->id);

printf("remove node test, remove the 10th node:\n");

bn = list->at(list, 10);

node = NODE_T(bn, structint_node);

printf("  node 10 is: %d\n", node->id);

printf("  now remove node 10\n");

list->remove_at(list, 10);

printf(" node 10 was removed, check node 10 again:\n");

bn = list->at(list, 10);

node = NODE_T(bn, structint_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, 12);

node = NODE_T(bn, structint_node);

printf("  now node 12 is : %d\n", node->id);

node = (structint_node*)malloc(sizeof(structint_node));

node->id = 1200;

list->replace(list, 12, node);

bn = list->at(list, 12);

node = NODE_T(bn, structint_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*)1200);

assert(bn == 0);

printf("test remove ok\n----\n");

printf("test remove_by_key(90):\n");

ST_CALL(list, remove_by_key, (void*)90);

bn = ST_CALL(list, find_by_key, (void*)90);

assert(bn == 0);

printf("test remove_by_key(90) end\n----\n");

printf("test take_at(80):\n");

bn = ST_CALL(list, take_at, 80);

printf("  node 80 is: %d\n", NODE_T(bn,structint_node)->id);

free(bn);

printf("test take_at(80) end\n");

int_free_flag = 0;

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");

}

voidstring_slist_test()

{

structsingle_list * list = new_single_list(_string_child_free, _string_slist_hittest);

}

voidslist_test()

{

int_slist_test();

string_slist_test();

}

测试代码里主要演示了:

自定义链表节点类型

定义释放回调

定义用于查找的 hit test 回调

如何创建链表

如何使用( add 、remove 、 take 、find 、 insert 等)

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值