链表
链表的概念
链表是一种物理存储上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
- 单向、双向
- 带头、不带头
- 循环、非循环
实际中最常用的是两种结构:
- 无头单项非循环链表
- 带头双向循环链表
单链表
单链表的增删改查操作示例
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);
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();
}