只实现了几个简单的操作。在Linux内核中链表的使用非常多,不过使用方法与本例完全不一样。后续有时间再详述。
ChainNode.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA_LEN 128
#define CN_TRUE 0
#define CN_FALSE 1
#define CN_ERROR -1
typedef struct ChainNode ChainNode;
struct ChainNode {
char data[MAX_DATA_LEN+1];
ChainNode *next;
};
static int get_chain_len(ChainNode *head)
{
int len = 0;
ChainNode *node;
node = head;
while(node) {
len++;
node = node->next;
}
return len;
}
/*
* Create a new node, and append it to the end of chain.
*/
static int chain_append(ChainNode *head, char *content, int content_len)
{
ChainNode *node;
ChainNode *new_node;
if(NULL == head || NULL == content || content_len < 1 || content_len > MAX_DATA_LEN)
return CN_ERROR;
new_node = (ChainNode *)malloc(sizeof(ChainNode));
if(NULL == new_node)
return CN_ERROR;
st