首页 / 软件开发 / C语言 / list.c - A linked list by C --- C语言实现的单向链表
list.c - A linked list by C --- C语言实现的单向链表2010-06-17 csdn博客 张亮这是我实现的单向链表。单向链表很简单,可以存储任意类型的数据:整型、字符串或指针类型。但是,不要混存。除整型外,链表节点数据由调用者分配和负责释放,即调用者负责提供一个回调函数,当链表释放时,自动调用你提供的这个函数。记住:链表不分配任何用户的数据,仅分配和管理自己的私有数据,因此,分配和释放链表所存放的数据的工作必须由用户来完成。读者可以在测试代码中看到用例。链表需要的文件为:unistd.h,list.h,list.c。测试文件为test.c。以下是代码:/**************************************************************************************** * list.h * * Generic sequential linked list node structure -- can hold any type data. * * cheungmine * * Mar. 22, 2008. All rights reserved. * ****************************************************************************************/ #ifndef LIST_H_INCLUDED__ #define LIST_H_INCLUDED__
/** * A prototype of callbacked function called by: * - list_destroy() * - list_traverse() * - list_node_free() * NULL for no use */ typedef void(*pfunc_list_callback)(listnode_t* node);
/** * An prototype example of free node data function implemented by caller: */ static void my_listnode_data_free(listnode_t *node) { free(node->data); DBG_TRACE("my_listnode_data_free
"); }
/** * A prototype example of traverse implemented by caller: */ static void my_listnode_key_traverse(listnode_t *node) { printf(" key=%ld
", node->key); }
/** * Traverses a list, applied callback functionn for each node */ void list_traverse(list_t *in_list, pfunc_list_callback pfcb_traversenode);
/** * Allocates a empty list from heap, this creates a new list */ list_t* list_create();
/** * Clears a list and free memory, the list cannot be used later */ void list_destroy(list_t *in_list, pfunc_list_callback pfcb_freedata);
/** * Creates a new node assigned with data, not allocates for data */ listnode_t* list_node_create(void* data);
/** * Free a list node and it's associated nodes, the freed node cannot be used later */ void list_node_free(listnode_t* node, pfunc_list_callback pfcb_freedata);
/** * Creates a new node assigned with a key, not allocates for key */ listnode_t* list_key_create(long key);
/** * Finds prev node of given node */ listnode_t* list_find_prev(const list_t *in_list, const listnode_t *node);
/** * Appends a node to a list at back */ void list_push_back(list_t *in_list, listnode_t *node);
/** * Inserts a node in front of head into a list */ void list_push_front(list_t *in_list, listnode_t *in_node);
/** * Inserts a node after pos node into a list */ void list_insert_after(list_t *in_list, listnode_t *pos_node, listnode_t *in_node);
/** * Inserts a node before pos node into a list */ void list_insert_before(list_t *in_list, listnode_t *pos_node, listnode_t *in_node);
/** * Removes the first node from a list and returns it */ listnode_t* list_pop_front(list_t *in_list);
/** * Removes the last node from a list and returns it */ listnode_t* list_pop_back(list_t *in_list);
/** * Removes all nodes but for list itself */ void list_clear(list_t *in_list, pfunc_list_callback pfcb);
/** * Returns a copy of a list_t from heap */ list_t* list_copy(list_t list);
/** * Concatenates two lists into first list */ void list_concat(list_t *first, list_t *second);
/** * Gets count of nodes in the list */ size_t list_size(const list_t* in_list);
/** * Gets node by index: 0-based. 0 is head */ listnode_t* list_node_at(const list_t* in_list, size_t index);
/** * Slices list off from begin to end, returns begin node * Caller should free returned list nodes * begin and end can be same one */ listnode_t* list_slice(list_t* in_list, listnode_t* begin, listnode_t* end);
#ifdef __cplusplus } #endif
#endif /* LIST_H_INCLUDED__ */
/**************************************************************************************** * list.c * * Generic sequential linked list node structure -- can hold any type data. * * cheungmine * * Mar. 22, 2008. All rights reserved. * ****************************************************************************************/ #include "list.h"
/**************************************************************************************** * unistd.h * * 2008-03-15 Last created by cheungmine. * * All rights reserved by cheungmine. * ****************************************************************************************/ #ifndef UNISTD_H__ #define UNISTD_H__
/* Standard C header files included */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h>