这是用C语言实现的一个简单的带头结点的单链表,可以进行基本的遍历、插入、删除以及销毁等操作,有需要的可以参考。代码如下:

//==========================================										   	
//	Filename : C语言实现简单的单链表						   	
//	Time     : 2019年5月2日						   
//	Authonr  : 柚子树					   	
//	Email    : [email protected]		   										   
//==========================================

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 1、创建链表
typedef struct _LINKNODE		// 节点结构体
{
	int data;					// 数据域
	struct _LINKNODE *next;		// 指针域
}link_node;

link_node* init_linklist()
{
	int len;	// 节点个数
	int val;	// 节点的值

	// 创建头结点指针并分配内存
	link_node* head = (link_node*)malloc(sizeof(link_node));
	if (NULL == head)
	{
		printf("malloc error!");
		return NULL;
	}
	head->next = NULL;

	// 保存当前节点
	link_node* p_current = head;
	printf("请输入节点个数:");
	scanf("%d", &len);
	
	// 循环向链表中插入节点
	for (int i = 0; i < len; ++i)
	{
		// 给新节点分配内存
		link_node* newNode = (link_node*)malloc(sizeof(link_node));
		if (NULL == newNode)
		{
			printf("malloc error!");
			exit(-1);
		}

		printf("请输入第 %d 个节点的值: ", i + 1);
		scanf("%d", &val);
		// 给新节点赋值
		newNode->data = val;
		newNode->next = NULL;
		// 将新节点插入链表中最后一个节点的下一个位置
		p_current->next = newNode;
		// 更新 p_current 指针
		p_current = newNode;
	}

	printf("链表创建成功!\n");
	return head;
}

// 2、遍历链表
void foreach_linklist(link_node* head)
{
	printf("链表内容: ");
	if (NULL == head)		// 判断参数有效性
	{
		return NULL;
	}

	// 定义辅助指针
	link_node* p_current = head->next;		// 不需要打印头结点
	while (p_current != NULL)
	{
		printf("%d  ", p_current->data);
		p_current = p_current->next;
	}
	printf("\n");
}

// 3、在 val 值前插入节点,如果值 val 不存在,则在尾部插入
void insert_linklist(link_node* head, int val, const int data)
{
	if (NULL == head)
	{
		return NULL;
	}

	// 两个辅助指针
	link_node* p_prev = head;
	link_node* p_current = p_prev->next;

	while (p_current != NULL)		// 寻找值 val
	{
		if (p_current->data == val)
		{
			break;
		}
		p_prev = p_current;
		p_current = p_prev->next;
	}
	if (p_current == NULL)			
	{
		printf("不存在值为%d的节点\n", val);
		// return NULL;
	}

	// 创建新节点
	link_node* newNode = (link_node*)malloc(sizeof(link_node));
	if (NULL == newNode)
	{
		return NULL;
	}
	newNode->data = data;
	newNode->next = NULL;

	// 将新节点插入链表中
	newNode->next = p_current;
	p_prev->next = newNode;

	printf("值为 %d 节点插入\n", data);
}

// 4、删除节点
void remove_linklist(link_node* head, const int val)
{
	if (NULL == head)
	{
		return NULL;
	}

	// 定义辅助指针
	link_node* p_prev = head;
	link_node* p_current = p_prev->next;

	while (p_current != NULL)		// 寻找值 val
	{
		if (p_current->data == val)
		{
			break;
		}
		p_prev = p_current;
		p_current = p_prev->next;
	}
	if (p_current == NULL)
	{
		printf("不存在值为%d的节点\n", val);
		return NULL;
	}

	// 删除值为 val 的节点,并释放其内存
	p_prev->next = p_current->next;
	free(p_current);

	printf("值为 %d 节点删除\n", val);
}

// 5、销毁链表
void destroy_linklist(link_node* head)
{
	if (NULL == head)
	{
		return NULL;
	}

	// 辅助指针
	link_node* p_current = head;
	while (p_current != NULL)
	{
		// 缓存当前节点下一个节点
		link_node* p_next = p_current->next;
		free(p_current);
		p_current = p_next;
	}
	
	printf("链表销毁成功!\n");
}

int main()
{
	link_node* head = init_linklist();
	printf("======================================\n");
	foreach_linklist(head);

	printf("======================================\n");
	insert_linklist(head, 4, 3);
	printf("======================================\n");

	foreach_linklist(head);
	
	printf("======================================\n");
	remove_linklist(head, 5);
	printf("======================================\n");

	foreach_linklist(head);

	printf("======================================\n");
	destroy_linklist(head);
	printf("======================================\n");

	system("pause");
	return EXIT_SUCCESS;
}

运行结果:
C语言实现简单的单链表

相关文章: