单向链表-C语言实现

本文介绍了如何使用C语言实现单向链表,虽然仅实现了基础操作,但提及在Linux内核中链表应用广泛,暗示了其重要性。未来计划深入探讨。
摘要由CSDN通过智能技术生成

只实现了几个简单的操作。在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值