【数据结构 04】单链表

一、链表简介

链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

链表在结构上的分类

1. 带头结点或无头结点
2. 单向或双向
3. 循环或非循环

虽然链表有多种结构类型,但是我么在实际开发中常用的只有两种结构:

  1. 无头单向非循环链表:结构简单,通常不单独使用,而是作为其他数据结构的子结构,如哈希桶、图的邻接表……
  2. 带头双向循环链表:结构最复杂,功能最全面,使用效率高

下例代码是无头单向非循环链表的实现,设计思路:

  1. 每个ListNode节点都包含一个数据和一个next指针,next指针指向下一个节点
  2. 当pList == NULL 的时候,代表这个链表为空,没有任何数据
  3. 链表最后一个节点的next指针一定是NULL
  4. 当函数涉及数据增删时,传入的参数为二级指针 ListNode** ppList

二、SingleList.h

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>typedef int DataType;typedef struct ListNode
{DataType data;struct ListNode* next;
}ListNode;bool Empty(ListNode* plist)
{return plist == NULL;
}void Print(ListNode* plist)
{ListNode* cur = plist;while (cur != NULL){printf("%2d -> ", cur->data);cur = cur->next;}printf("NULL\n");
}// 动态申请一个节点
ListNode* BuyNode(DataType x)
{ListNode* node = (ListNode*)malloc(sizeof(ListNode));node->data = x;node->next = NULL;return node;
}// 尾插
void PushBack(ListNode** pplist, DataType x)
{assert(pplist);ListNode* node = BuyNode(x);// 插入链表的第一个节点if (*pplist == NULL){*pplist = node;return;}// cur指针通过循环遍历找到链表的尾结点ListNode* cur = *pplist;while (cur->next != NULL){cur = cur->next;}cur->next = node;
}// 头插
void PushFront(ListNode** pplist, DataType x)
{assert(pplist);ListNode* node = BuyNode(x);if (*pplist == NULL){*pplist = node;return;}node->next = *pplist;*pplist = node;
}// 尾删
void PopBack(ListNode** pplist)
{if (Empty(*pplist)){printf("链表为空,尾删失败\n");return;}ListNode* cur = *pplist;ListNode* prev = cur;while (cur->next != NULL){prev = cur;cur = cur->next;}free(cur);cur = NULL;prev->next = NULL;
}// 头删
void PopFront(ListNode** pplist)
{if (Empty(*pplist)){printf("链表为空,尾删失败\n");return;}ListNode* cur = *pplist;*pplist = cur->next;free(cur);cur = NULL;
}// 查找,返回第一个元素x的节点
ListNode* Find(ListNode* plist, DataType x)
{ListNode* cur = plist;while (cur != NULL){if (cur->data == x)return cur;cur = cur->next;}return NULL;
}// 在pos后面插入新节点,pos节点由Find函数获得
void InsertAfter(ListNode* pos, DataType x)
{if (pos == NULL){printf("pos为空,数据插入失败\n");return;}ListNode* node = BuyNode(x);node->next = pos->next;pos->next = node;
}// 删除pos节点
void Delete(ListNode** pplist, ListNode* pos)
{if (pos == NULL){printf("pos为空,数据删除失败\n");return;}if (Empty(*pplist)){printf("单链表已为空,Delete失败\n");return;}ListNode* cur = *pplist;ListNode* prev = NULL;while (cur){if (cur->data == pos->data && prev == NULL){// 删除第一个节点*pplist = pos->next;free(pos);pos = NULL;return;}if (cur->data == pos->data){prev->next = pos->next;free(pos);pos = NULL;return;}prev = cur;cur = cur->next;}
}// 销毁链表
void Destroy(ListNode** pplist)
{while (*pplist){ListNode* cur = *pplist;*pplist = cur->next;free(cur);cur = NULL;}printf("链表销毁成功\n");
}

三、test.c

#define _CRT_SECURE_NO_WARNINGS 1#include "SingleList.h"int main()
{ListNode* plist = NULL;// 尾插数据PushBack(&plist, 1);PushBack(&plist, 3);PushBack(&plist, 5);PushBack(&plist, 7);Print(plist);// 头插数据PushFront(&plist, 2);PushFront(&plist, 4);PushFront(&plist, 6);PushFront(&plist, 8);Print(plist);// 尾删数据PopBack(&plist);PopBack(&plist);PopBack(&plist);Print(plist);// 头删数据PopFront(&plist);PopFront(&plist);PopFront(&plist);Print(plist);// 在查找的元素后面插入节点InsertAfter(Find(plist, 1), -1);InsertAfter(Find(plist, 2), -2);InsertAfter(Find(plist, -2), 22);Print(plist);// 删除查找到的节点Delete(&plist, Find(plist, -2));Delete(&plist, Find(plist, -1));Delete(&plist, Find(plist, 22));Delete(&plist, Find(plist, 100)); // pos为空,数据删除失败!Print(plist);// Delete删空链表Delete(&plist, Find(plist, 2));Delete(&plist, Find(plist, 1));Delete(&plist, Find(plist, 1)); // pos为空,数据删除失败!Print(plist);// 销毁链表,先插入数据测试PushBack(&plist, 1);PushBack(&plist, 2);PushBack(&plist, 3);Print(plist); // 1 -> 2 -> 3 -> NULLDestroy(&plist); // 链表销毁成功Print(plist);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/442419.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java时间段合并,去除重复时间

java实现多个时间段合并 如题&#xff0c;将多个时间段进行合并 参考力扣合并区间写法 private static List<ProductStartWork> mergeOverlappingTimePeriods(List<ProductStartWork> timePeriods) {List<ProductStartWork> mergedIntervals new ArrayL…

###C语言程序设计-----C语言学习(8)## 斐波那契数列问题,素数问题,人数分配问题。

前言&#xff1a;感谢您的关注哦&#xff0c;我会持续更新编程相关知识&#xff0c;愿您在这里有所收获。如果有任何问题&#xff0c;欢迎沟通交流&#xff01;期待与您在学习编程的道路上共同进步。 今天&#xff0c;我们主要分享三个问题如何用C语言去求解&#xff0c;1.斐波…

基于深度学习的鸟类识别系统matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1 卷积神经网络基础 4.2 GoogLeNet模型 4.3 鸟类识别系统 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 ............................…

RX-4045SA实时时钟模块规格书

.内置 32.768 kHz 晶体单元 (频率高精度调整完毕) (510-6/ Ta25 oC) .接口类型 &#xff1a;4 线串行接口 .工作电压范围 &#xff1a;1.7 V ~ 5.5 V .计时&#xff08;保持&#xff09; 电压范围 &#xff1a;1.15 V ~ 5.5 V .多种检测功能 &#xff1a;振荡停止…

Mysql-索引创建,索引失效案例

索引创建建议 1 什么情况下需要创建索引&#xff1f; 频繁出现在where 条件字段&#xff0c;order排序&#xff0c;group by分组字段select 频繁查询的列&#xff0c;考虑是否需要创建联合索引&#xff08;覆盖索引&#xff0c;不回表&#xff09;多表join关联查询&#xff0…

融资800万欧元!量子传导公司QphoX具备光量子通道连接优势

​编辑丨慕一 编译/排版丨沛贤 深度好文&#xff1a;900字丨6分钟阅读 荷兰量子计算公司QphoX获得了一轮 800 万欧元&#xff08;约合人民币6263万元&#xff09;的融资&#xff0c;这是目前荷兰的量子公司获得的最大笔融资&#xff0c;也是荷兰量子产业的一项重大进展。 本轮…

pytorch学习笔记(十二)

以下代码是以CIFAR10这个10分类的图片数据集训练过程的完整的代码。 训练部分 train.py主要包含以下几个部件&#xff1a; 准备训练、测试数据集用DateLoader加载两个数据集&#xff0c;要设置好batchsize创建网络模型&#xff08;具体模型在model.py中&#xff09;设置损失函…

C++ :类的简单介绍(二) ——this指针

目录 this指针&#xff1a; this的特性&#xff1a; 相关面试题型&#xff1a; this指针&#xff1a; class Date { public:void Init(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout <<_year<< "-" <&l…

数据库安全的重要性和防范要求

数据库作为信息系统的核心&#xff0c;不仅承载着海量的关键数据&#xff0c;还负责向各类用户提供高效、可靠的信息服务。在网络技术高度发展的今天&#xff0c;数据库的安全性显得尤为关键。为了防范不法分子的攻击&#xff0c;维护数据完整性和可靠性&#xff0c;数据库安全…

详解SpringCloud微服务技术栈:深入ElasticSearch(1)——数据聚合

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位大四、研0学生&#xff0c;正在努力准备大四暑假的实习 &#x1f30c;上期文章&#xff1a;详解SpringCloud微服务技术栈&#xff1a;ElasticSearch实战&#xff08;旅游类项目&#xff09; &#x1f4da;订阅专栏&#x…

怎么查询鸿蒙真机支持的API版本

1、打开设备的开发者模式与USB调试并通过USB连接上电脑。 2、管理员身份运行cmd。 3、进入hdc.exe所在目录。(鸿蒙OS IDE的SDK下载目录中) 4、输入hdc shell&#xff0c;进入特殊模式 5、输入 getprop hw_sc.build.os.apiversion 查看API版本 6、输入 getprop hw_sc.build…

在Mixamo网站上,下载的动画导入unity给自己的模型添加后出错怎么解决

在Mixamo网站上&#xff0c;下载的动画导入unity给自己的模型添加后出错 一、在Mixamo下载的模型可以正常使用二、在自己的模型和unity自带模型上就出错1.解决方法2.解决成功 注意 一、在Mixamo下载的模型可以正常使用 二、在自己的模型和unity自带模型上就出错 1.解决方法 选…