用C语言实现一个简单实用的单向链表list,具有一定的实际意义。尤其我们不想使用STL里面的list<……>类的时候。我实现的这个list,结点存储任何调用者分配的任意类型的数据(void*)。这个list适用于一些简单的场合,消耗极少的资源。
头文件:
/* #include "unistd.h" typedef struct _listnode_t typedef struct _list_t /* A prototype of callbacked function called by list_destroy(), NULL for no use. */ /* An example of free node data function implemented by callee: /* Appends a node to a list */ /* Removes the first node from a list and returns it */ /* Removes all nodes but for list itself */ /* Returns a copy of a list_t from heap */ /* Concatenates two lists into first list. NOT freeing the second */ /* Allocates a new listnode_t from heap. NO memory allocated for input node_data */ /* Allocates a new listnode_t with a key node type */ /* Allocates a empty list_t from heap */ /* Frees in_list's all nodes and destroys in_list from heap. /* Gets count of nodes in the list */ /* Gets node by index 0-based. 0 is head */
|
/* #include "list.h" /* Appends a node to a list */ if (in_list->head) in_list->size++; /* Removes the first node from a list and returns it */ in_list->size--; /* Removes all nodes but for list itself */ /* Returns a copy of a list_t from heap */ first->size += second->size; /* Allocates a new listnode_t from heap */ listnode_t* /* Allocates a empty list_t from heap */ /* Frees a empty list_t from heap */ /* Gets count of nodes in the list */ /* Gets node by index 0-based. 0 is head */ assert(index >=0 && index < (int)in_list->size); while (i < index) return node;
|
/* unistd.h /* Standard C header files included */ /*============================================================================*/ typedef unsigned char uchar, byte, BYTE; typedef unsigned short uint16, word_t, ushort; typedef unsigned __int32 uint, uint32, dword_t, size_t; typedef unsigned long ulong; typedef __int16 int16; typedef long lresult; typedef unsigned __int64 uint64, qword_t, ulonglong; #ifndef BOOL #ifndef RESULT #ifndef IN #ifndef OUT #ifndef INOUT #ifndef OPTIONAL #define SIZE_BYTE 1
|