数据结构 -- 单链表

链表

链表的概念

链表是一种物理存储上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

  1. 单向、双向
  2. 带头、不带头
  3. 循环、非循环

实际中最常用的是两种结构:

  • 无头单项非循环链表
  • 带头双向循环链表

单链表

在这里插入图片描述

单链表的增删改查操作示例

  1. list.h
#pragma once
#include<stdio.h>
#include<stdlib.h>

typedef int LDataType;

//定义节点:数据 + 指针
typedef struct listNode
{
	LDataType _data;
	struct listNode* _next;
}listNode;

//链表
typedef struct list
{
	//保存第一个节点的地址
	listNode* _head;
}list;

//初始化链表
void listInit(list* lst);

//创建节点
listNode* creatListNode(LDataType val);

//展示链表数据
void listPrint(list* lst);

//尾插
void listPushBack(list* lst, LDataType val);

//尾删
void listPopBack(list* lst);

//头插
void listPushFront(list* lst, LDataType val);

//头删
void listPopFront(list* lst);

//某一位置下一节点插入
void listInsertAfter(listNode* node, LDataType val);

//某一位置下一节点删除
void listDelAfter(listNode* node);

//销毁每一个动态开辟的空间
void listDestory(list* lst);
  1. list.c
#include"list.h"

//初始化链表
void listInit(list* lst)
{
	if (lst == NULL)
		return;
	lst->_head = NULL;
}

//创建节点
listNode* creatListNode(LDataType val)
{
	listNode* node = (listNode*)malloc(sizeof(listNode));
	if (node == NULL)
		return NULL;
	node->_data = val;
	node->_next = NULL;

	return node;
}

//展示链表数据
void listPrint(list* lst)
{
	if (lst == NULL)
		return;
	if (lst->_head == NULL)
	{
		printf("空\n");
		return;
	}

	else
	{
		listNode* node = lst->_head;
		while (node != NULL)
		{
			printf("%d ", node->_data);
			node = node->_next;
		}
		printf("\n");
	}
}

//尾插
void listPushBack(list* lst, LDataType val)
{
	if (lst == NULL)
		return;
	if (lst->_head == NULL)
	{
		lst->_head = creatListNode(val);
	}
	else
	{
		//遍历找到最后一个节点,找到最后一个节点
		listNode* tail = lst->_head;
		while (tail->_next != NULL)
		{
			tail = tail->_next;
		}

		tail->_next = creatListNode(val);
	}
}

//尾删
void listPopBack(list* lst)
{
	if (lst == NULL)
		return;

	if (lst->_head == NULL)
	{
		return;
	}
	else
	{
		listNode* tail = lst->_head;
		listNode* prve = tail;
		if (tail->_next != NULL)
		{

			while (tail->_next != NULL)
			{
				prve = tail;
				tail = tail->_next;
			}
			prve->_next = NULL;
			free(tail);
		}
		else
		{
			lst->_head = NULL;
			free(tail);
		}
	}

}

//头插
void listPushFront(list* lst, LDataType val)
{
	if (lst == NULL)
		return;
	if (lst->_head == NULL)
	{
		lst->_head = creatListNode(val);
	}
	else
	{
		listNode* node = creatListNode(val);
		node->_next = lst->_head;
		lst->_head = node;
	}
}

//头删
void listPopFront(list* lst)
{
	if (lst == NULL)
		return;
	if (lst->_head == NULL)
		return;
	listNode* node = lst->_head;
	lst->_head = node->_next;
	free(node);
}

//某一位置下一节点插入
void listInsertAfter(listNode* node, LDataType val)
{
	if (node == NULL)
		return;
	listNode* nodeInsert = creatListNode(val);

	listNode* next = node->_next;
	node->_next = nodeInsert;
	nodeInsert->_next = next;
}

//某一位置下一节点删除
void listDelAfter(listNode* node)
{
	if (node == NULL || node->_next == NULL)
		return;
	listNode* next = node->_next;
	listNode* nextnext = next->_next;
	node->_next = nextnext;
	free(next);
}

//查找接口
listNode* listFind(list* lst, LDataType val)
{
	if (lst == NULL || lst->_head == NULL)
		return NULL;
	listNode* node = lst->_head;
	while (node->_data != val)
	{
		if (node->_next != NULL)
			node = node->_next;
		else
		{
			return NULL;
		}
	}
	return node;
}

//销毁每一个动态开辟的空间
void listDestory(list* lst)
{
	if (lst == NULL || lst->_head == NULL)
		return;
	listNode* node = lst->_head;
	while (lst->_head != NULL)
	{
		node = lst->_head;
		lst->_head = node->_next;
		free(node);
	}
}


void test()
{
	list lst;
	listInit(&lst);
	listPushBack(&lst, 1);
	listPushBack(&lst, 2);
	listPushBack(&lst, 3);
	listPrint(&lst);
	listNode* node = listFind(&lst, 2);
	listInsertAfter(node, 22);
	listPrint(&lst);
	node = listFind(&lst, 1);
	listDelAfter(node);
	listPrint(&lst);

	listDestory(&lst);
}

void main()
{
	test();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值