首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
热门关键字: .net应用  操作系统  Dreamweaver  WinRAR  网络推广
高级搜索
您当前的位置: 主页>C/C++>C语言>用C语言实现一个简单实用的单向链表list
用C语言实现一个简单实用的单向链表list
来源: 发布时间:2007-11-20 发布人: 浏览: 人次   字体: [ ]  

C语言实现一个简单实用的单向链表list,具有一定的实际意义。尤其我们不想使用STL里面的list<...>类的时候。我实现的这个list,结点存储任何调用者分配的任意类型的数据(void*)。这个list适用于一些简单的场合,消耗极少的资源。 

头文件:

 

/*
 * list.h
 *        Generic sequential linked list node structure -- can hold any type data.
 *        cheungmine 
 *      Sep. 22, 2007.  All rights reserved.
 
*/
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#include 
"unistd.h"

typedef 
struct _listnode_t
{
    
struct _listnode_t    *next;
    union{
        
void*            data;
        
struct _list_t    *list;
        
const char        *str;
        
long            key;
    };
}listnode_t;

typedef 
struct _list_t
{
    size_t        size;    
/* count of nodes */
    listnode_t    
*head;
    listnode_t  
*tail;
}list_t, 
*list_p;

/* A prototype of callbacked function called by list_destroy(), NULL for no use. */
typedef 
void(*pfcb_list_node_free)(listnode_t* node);    

/* An example of free node data function implemented by callee:
void my_list_node_free(listnode_t *node)
{
    free(node->data);
}
*/

/* Appends a node to a list */
extern void 
list_append_node(list_t 
*in_list, listnode_t *in_node);

/* Removes the first node from a list and returns it */
extern listnode_t* 
list_remove_head(list_t 
*in_list);

/* Removes all nodes but for list itself */
extern void
list_remove_all(list_t 
*in_list, pfcb_list_node_free pfunc /* NULL for no use or a key node */);

/* Returns a copy of a list_t from heap */
extern list_t* 
list_copy(list_t in_list);

/* Concatenates two lists into first list. NOT freeing the second */
extern void 
list_concat(list_t 
*first, list_t *second);

/* Allocates a new listnode_t from heap. NO memory allocated for input node_data */
extern listnode_t* 
list_node_create(
void* node_data);

/* Allocates a new listnode_t with a key node type */
extern listnode_t* 
list_key_create(
long node_key);

/* Allocates a empty list_t from heap */
extern list_t* 
list_create();

/* Frees in_list's all nodes and destroys in_list from heap.
 * the callee is responsible for freeing node data.
 * the node freed-function(pfunc) is called by list_destroy.
 
*/
extern void 
list_destroy(list_t 
*in_list, pfcb_list_node_free pfunc /* NULL for no use or a key node */);

/* Gets count of nodes in the list */
extern size_t 
list_size(
const list_t* in_list);

/* Gets node by index 0-based. 0 is head */
extern listnode_t* 
list_node_at(
const list_t* in_list, int index);


#endif  /* LIST_H_INCLUDED */

 

实现文件:

 

/*
 * list.c
 *        Generic linked list implementation.
 *        cheungmine 
 *      Sep. 22, 2007.  All rights reserved.
 
*/

#include 
"list.h"

/* Appends a node to a list */
void 
list_append_node(list_t 
*in_list, listnode_t *node)
{
    node
->next = NULL;

    
if (in_list->head)
    {
        in_list
->tail->next = node;
        in_list
->tail = node;
    }
    
else
        in_list
->head = in_list->tail = node;

    in_list
->size++;
}

/* Removes the first node from a list and returns it */
listnode_t
* 
list_remove_head(list_t 
*in_list)
{
    listnode_t    
*node = NULL;
    
if (in_list->head)
    {
        node 
= in_list->head;
        in_list
->head = in_list->head->next;
        
if (in_list->head == NULL)
            in_list
->tail = NULL;
        node
->next = NULL;

        in_list
->size--;        
    }
    assert(in_list
->size >= 0);
    
return node;
}

/* Removes all nodes but for list itself */
void
list_remove_all(list_t 
*in_list, pfcb_list_node_free pf)
{
    listnode_t    
*node;
    
while((node = list_remove_head(in_list))){
        
if (pf) (*pf)(node);
        free(node);
    }
    assert (in_list
->size==0);
}

/* Returns a copy of a list_t from heap */
list_t
* 
list_copy(list_t list)
{
    list_t    
*newlist = (list_t*)malloc (sizeof(list_t));
    
*newlist = list;
    
return newlist;
}

共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·字母全排列快速算法C代码
·C语言的无符号数据类型int,short,byt...
·C语言宏定义使用技巧
·linux下的c语言的随机数算法代码
·Linux下实时定时器在C语言中的实现和...
·怎样用C语言得到一个进程的全路径
·实例讲解C语言OPEN函数语法及其应用
·不定参数在C语言中的应用实例
·C语言开发实现的虚拟桌面程序代码
·C语言高手总结的新手容易犯的错误
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·C语言高手总结的新手容易犯的
·C语言开发实现的虚拟桌面程序
·不定参数在C语言中的应用实例
·实例讲解C语言OPEN函数语法及
·怎样用C语言得到一个进程的全
·Linux下实时定时器在C语言中...
·linux下的c语言的随机数算法...
·C语言宏定义使用技巧
·C语言的无符号数据类型int,sh...
·字母全排列快速算法C代码
·如何用C语言编写Windows服务...
·C语言程序设计基础之预处理
热 门 文 章
·C语言的无符号数据类型int,sh...
·Linux下实时定时器在C语言中...
·C语言宏定义使用技巧
·C语言获得整数类型和浮点类型...
·字母全排列快速算法C代码
·实例讲解C语言OPEN函数语法及...
·linux下的c语言的随机数算法...
·如何用C语言编写Windows服务...
·C语言开发实现的虚拟桌面程序...
·C语言程序设计基础之预处理
·怎样用C语言得到一个进程的全...
·利用c语言编制cgi实现搜索
·C语言数组排序小结
·深入理解C语言指针的奥秘
·不定参数在C语言中的应用实例
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .