单链表及其相关函数
实现功能
BuySListNode ————————————申请一个新节点并赋值
SListLength —————————————计算链表的长度
SListPushBack————————————尾插
SListPushFront————————————头插
SListPopBack—————————————尾删
SListPopFront————————————头删
SListFindByVal————————————按值查找链表
SListFindByPos————————————按位置查找链表
SListInsertAfter————————————任意位置插入
SListEraseAfter————————————任意位置删除
SListPrint——————————————打印链表
SList.h
#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>typedef int SLTDataType;typedef struct SLTNode
{SLTDataType data;struct SLTNode* next;
}SLTNode;//申请一个新节点并赋值
extern SLTNode* BuySListNode(SLTDataType x);//计算链表的长度
extern int SListLength(SLTNode* phead);//尾插
extern void SListPushBack(SLTNode** pphead, SLTDataType x);//头插
extern void SListPushFront(SLTNode** pphead, SLTDataType x);//尾删
extern void SListPopBack(SLTNode** pphead);//头删
extern void SListPopFront(SLTNode** pphead);//按值查找链表
extern SLTNode* FindByVal(SLTNode* phead, SLTDataType x);
extern void SListFindByVal(SLTNode* phead, SLTDataType x);//按位置查找链表
void FindByPos(SLTNode* phead, int pos, SLTNode** pp_aim1, int* p_aim2);
extern void SListFindByPos(SLTNode* phead, int pos);//任意位置插入
extern void SListInsertAfter(SLTNode** pphead, int pos, SLTDataType x);//任意位置删除
extern void SListEraseAfter(SLTNode** pphead, int pos);//打印链表
extern void SListPrint(SLTNode* phead);
SList.c
#include "SList.h"SLTNode* BuySListNode(SLTDataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (!newnode){perror("create newnode");exit(-1);}newnode->next = NULL;newnode->data = x;return newnode;
}int SListLength(SLTNode* phead)
{SLTNode* tail = phead;int count = 0;for (count = 1; tail->next != NULL; ++count){tail = tail->next;}return count;
}bool CheckEmpty(SLTNode* phead)
{return (phead) ? false : true;
}void SListPushBack(SLTNode** pphead, SLTDataType x)
{if (CheckEmpty(*pphead)){*pphead = BuySListNode(x);}else{SLTNode* tail = *pphead;while (tail->next){tail = tail->next;}tail->next = BuySListNode(x);}
}void SListPushFront(SLTNode** pphead, SLTDataType x)
{SLTNode* newnode = BuySListNode(x);newnode->next = *pphead;*pphead = newnode;
}void SListPopBack(SLTNode** pphead)
{if (CheckEmpty(*pphead)){printf("SList is empty!\n");return;}//注意仅存有一个节点的情况if (!(*pphead)->next){free(*pphead);*pphead = NULL;}//寻找前一个节点else{SLTNode* tail = *pphead;while (tail->next->next){tail = tail->next;}free(tail->next);tail->next = NULL;}
}void SListPopFront(SLTNode** pphead)
{if (CheckEmpty(*pphead)){printf("SList is empty!\n");return;}SLTNode* ret = *pphead;*pphead = (*pphead)->next;free(ret);
}SLTNode* FindByVal(SLTNode* phead, SLTDataType x)
{SLTNode* cur = phead;while (cur){if (cur->data == x){return cur;}elsecur = cur->next;}return NULL;
}
void SListFindByVal(SLTNode* phead, SLTDataType x)
{SLTNode* pos = FindByVal(phead, x);if (!pos){printf("Don't find it!\n");return;}//进行多次查找int i = 1;while (pos){printf("第%d个pos节点:%p->%d\n", i++, pos, pos->data);pos = FindByVal(pos->next, x);}
}void FindByPos(SLTNode* phead, int pos, SLTNode** pp_aim1, int* p_aim2)
{if (pos < 0 || pos > SListLength(phead)){printf("The position is illegal!\n");}else{SLTNode* tail = phead;//注意循环只需进行pos-1,所以使用--poswhile (--pos){tail = tail->next;}*pp_aim1 = tail;*p_aim2 = tail->data;}
}
void SListFindByPos(SLTNode* phead, int pos)
{SLTNode* aim1;int aim2 = 0;FindByPos(phead, pos, &aim1, &aim2);printf("%d号节点:%p->%d\n", pos, aim1, aim2);
}void SListInsertAfter(SLTNode** pphead, int pos, SLTDataType x)
{if (pos < 0 || pos > SListLength(*pphead)){printf("The position is illegal!\n");}else{if (*pphead == NULL){*pphead = BuySListNode(x);}else if ((*pphead)->next == NULL){(*pphead)->next = BuySListNode(x);}else{SLTNode* dest = *pphead;while (--pos){dest = dest->next;}SLTNode* newnode = BuySListNode(x);newnode->next = dest->next;dest->next = newnode;}}
}void SListEraseAfter(SLTNode** pphead, int pos)
{if (pos <= 0 || pos >= SListLength(*pphead)){printf("The position is illegal!\n");}else{SLTNode* prev = *pphead;while (--pos){prev = prev->next;}SLTNode* afet = prev->next->next;free(prev->next);prev->next = afet;}
}void SListPrint(SLTNode* phead)
{if (CheckEmpty(phead)){printf("SList is empty!\n");return;}SLTNode* tail = phead;while (!tail == NULL){printf("%d->", tail->data);tail = tail->next;}printf("NULL\n");
}
test.c
#include "SList.h"void test1()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);
}void test2()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPrint(pList);
}void test3()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPopBack(&pList);SListPrint(pList);
}void test4()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);SListPopFront(&pList);SListPopFront(&pList);SListPopFront(&pList);SListPopFront(&pList);SListPrint(pList);SListPopFront(&pList);SListPrint(pList);
}void test5()
{SLTNode* pList = NULL;SListPushFront(&pList, 3);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 3);SListPushFront(&pList, 1);SListPrint(pList);SListFindByVal(pList, 3);
}void test6()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);SListFindByPos(pList, 5);}void test7()
{SLTNode* pList = NULL;SListPushFront(&pList, 1);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPrint(pList);SListInsertAfter(&pList, 1, 2);SListPrint(pList);
}void test8()
{SLTNode* pList = NULL;SListPushFront(&pList, 2);SListPushBack(&pList, 3);SListPushBack(&pList, 4);SListPushBack(&pList, 5);SListPushFront(&pList, 1);SListPrint(pList);SListEraseAfter(&pList, 1);SListEraseAfter(&pList, 1);SListEraseAfter(&pList, 1);SListEraseAfter(&pList, 1);SListEraseAfter(&pList, 0);SListPrint(pList);
}void main()
{test1();printf("-------------------------------\n");test2();printf("-------------------------------\n");test3();printf("-------------------------------\n");test4();printf("-------------------------------\n");test5();printf("-------------------------------\n");test6();printf("-------------------------------\n");test7();printf("-------------------------------\n");test8();printf("-------------------------------\n");
}
相关文章:
单链表及其相关函数
实现功能BuySListNode ————————————申请一个新节点并赋值SListLength —————————————计算链表的长度SListPushBack————————————尾插SListPushFront————————————头插SListPopBack—————————————尾删SListPopFront—…...
Linux段错误调试
1、设置ulimit ulimit -a 查看 ulimit -c 2048 设置core大小 2、设置core文件信息 下面两个设置需要在root下设置,否则权限不通过 echo 1>/proc/sys/kernel/core_uses_pid echo "/tmp/corefile-%e-%p-%t" >/proc/sys/kernel/core_pattern 3、编译…...

Gopro卡无法打开视频恢复方法
下边来看一个文件系统严重受损的Gopro恢复案例故障存储: 120G SD卡故障现象:客户正常使用,备份数据时发现卡无法打开,多次插拔后故障依旧。故障分析:Winhex查看发现0号分区表扇区正常,这应该是一个exfat格式的文件系统,但是逻辑盘…...

vmware虚拟机与树莓派4B安装ubuntu1804 + ros遇到的问题
如题所示,本人在虚拟机上安装ubuntu1804,可以很容易安装,并且更换系统apt源和ros源,然后安装ros,非常顺利,但是在树莓派4B上安装raspiberry系统就遇到了好多问题。 树莓派我烧录的是这个镜像:ub…...
JS逆向hook通用脚本合集
1. cookie 通用hook Cookie Hook 用于定位 Cookie 中关键参数生成位置,以下代码演示了当 Cookie 中匹配到了 v 关键字, 则插入断点 (function () {var cookieTemp ;Object.defineproperty(document, cookie, {set: function (val) {if (val.indexOf(v…...

nacos的介绍和下载安装(详细)
目录 一、介绍 1.什么是nacos(含有官方文档)? 2.nacos的作用是什么? 3.什么是nacos注册中心? 4.核心功能 二、下载安装 一、介绍 1.什么是nacos(含有官方文档)? 一个更易于…...

【算法经典题集】前缀和与数学(持续更新~~~)
😽PREFACE🎁欢迎各位→点赞👍 收藏⭐ 评论📝📢系列专栏:算法经典题集🔊本专栏涉及到的知识点或者题目是算法专栏的补充与应用💪种一棵树最好是十年前其次是现在前缀和一维前缀和k倍…...

寻找时空中的引力波:科学家控制量子运动至量子基态
据英国每日邮报报道,时空织布里的涟漪或可以揭示宇宙在140亿年前是如何产生的,然而寻找这些名为“引力波”的涟漪却一直难以捉摸。现在美国科学家们声称他们发现了改善用于检测宇宙大爆炸的引力波的探测器的方法。 宇宙大爆炸残留的引力波 美国加州理…...

第六讲:ambari-web 模块二次开发
上述图片为 Ambari 部署及操作 hdp 集群相关的部分界面截图。这些页面如果想调整的话,比如汉化,二次开发等,则可以修改 ambari-web 模块的源码来实现。 一、介绍 ambari-web 模块涉及到的界面有: HDP 集群部署向导已安装服务的仪表板、配置界面等主机列表及详细信息告警列…...

echarts--提示框显示不全问题记录
最近接手一个同事之前做的网页,发现里面使用echarts来绘制各类图表;有2个问题一个是提示框显示不全,另一个就是绘制总是有部分数据显示不全。后者就是div宽度问题。。。无语,说下前面一个问题吧,记录一下。 tooltip组…...
LeetCode 1653. 使字符串平衡的最少删除次数
LeetCode 1653. 使字符串平衡的最少删除次数 难度:middle\color{orange}{middle}middle Rating:1794\color{orange}{1794}1794 题目描述 给你一个字符串 sss ,它仅包含字符 ′a′a′a′ 和 ′b′b′b′ 。 你可以删除 sss 中任意…...
聊一聊代码重构——程序方法和类上的代码实践
使用工厂方法取代构造方法 构造方法的问题 我们使用构造方法来初始化对象时候,我们得到的只能是当前对象。而使用工厂方法替换构造方法,我们可以返回其子类型或者代理类型。这让我们可以通过不同的实现类来进行逻辑实现的变化。 更重要的一点是&#…...

嵌入式学习笔记——寄存器开发STM32 GPIO口
寄存器开发STM32GPIO口前言认识GPIOGPIO是什么GPIO有什么用GPIO怎么用STM32上GPIO的命名以及数量GPIO口的框图(重点)输入框图解析三种输入模式GPIO输入时内部器件及其作用1.保护二极管2.上下拉电阻(可配置)3.施密特触发器4.输入数…...

[ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
🍬 博主介绍 👨🎓 博主介绍:大家好,我是 _PowerShell ,很高兴认识大家~ ✨主攻领域:【渗透领域】【数据通信】 【通讯安全】 【web安全】【面试分析】 🎉点赞➕评论➕收藏 养成习…...
程序设计与 C 语言期末复习
程序设计与 C 语言 1.计算机语言与编译 机器语言:一串仅由 0 和 1 序列表示的语言。计算机只能识别和接受 0 和 1 组成的指令。 符号语言(汇编语言):用一些英文字母和数字表示一个指令。 符号语言(汇编语言…...

05-思维导图Xmind快速入门
文章目录5.1 认识思维导图5.2 Xmind的主要结构及主题元素5.2.1 Xmind的多种结构5.2.2 主题分类5.2.3 Xmind的主题元素章节总结5.1 认识思维导图 什么是思维导图? 思维导图是一种将思维进行可视化的实用工具。 具体实现方法是用一个关键词去引发相关想法࿰…...

使用去中心化存储构建网站
今天的大多数网站都遵循后端服务器到前端代码的架构。但在 Web3 应用程序中,前端代码不具有与受智能合约保护的后端代码相同的去中心化性和弹性。那么如何使网站像智能合约一样具有弹性呢? 该体系结构似乎很简单: 创建一个没有服务器的静态…...

L - Let‘s Swap(哈希 + 规律)
2023河南省赛组队训练赛(四) - Virtual Judge (vjudge.net) 约瑟夫最近开发了一款名为Pandote的编辑软件,现在他正在测试,以确保它能正常工作,否则,他可能会被解雇!Joseph通过实现对Pandote上字符串的复制和…...
c语言自动内存回收(RAII实现)
简述 什么是RAII RAII(Resource Acquisition Is Initialization)是c之父Bjarne Stroustrup提出的概念。资源一般分三个步骤:获取、使用和销毁,而在自由使用内存的c语言中,资源的销毁常常是程序员容易遗漏的事情&…...

Node.js的简单学习一-----未完待续
文章目录前言学习目标一、初识Node.js1.1 回顾与思考1.1.1 需要掌握那些技术1.1.2 浏览器中的JavaScript的组成部分1.2 Node.js简介1 什么是Node.js2 Node.js中的JavaScript运行环境3 Node.js 可以做什么1.3 Node.js环境的安装1.4 在Node.js环境中执行JavaScript 代码终端中的快…...
Java如何权衡是使用无序的数组还是有序的数组
在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

2025季度云服务器排行榜
在全球云服务器市场,各厂商的排名和地位并非一成不变,而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势,对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析: 一、全球“三巨头”…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合
在汽车智能化的汹涌浪潮中,车辆不再仅仅是传统的交通工具,而是逐步演变为高度智能的移动终端。这一转变的核心支撑,来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒(T-Box)方案:NXP S32K146 与…...
IP如何挑?2025年海外专线IP如何购买?
你花了时间和预算买了IP,结果IP质量不佳,项目效率低下不说,还可能带来莫名的网络问题,是不是太闹心了?尤其是在面对海外专线IP时,到底怎么才能买到适合自己的呢?所以,挑IP绝对是个技…...
A2A JS SDK 完整教程:快速入门指南
目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库ÿ…...

【Redis】笔记|第8节|大厂高并发缓存架构实战与优化
缓存架构 代码结构 代码详情 功能点: 多级缓存,先查本地缓存,再查Redis,最后才查数据库热点数据重建逻辑使用分布式锁,二次查询更新缓存采用读写锁提升性能采用Redis的发布订阅机制通知所有实例更新本地缓存适用读多…...

Linux nano命令的基本使用
参考资料 GNU nanoを使いこなすnano基础 目录 一. 简介二. 文件打开2.1 普通方式打开文件2.2 只读方式打开文件 三. 文件查看3.1 打开文件时,显示行号3.2 翻页查看 四. 文件编辑4.1 Ctrl K 复制 和 Ctrl U 粘贴4.2 Alt/Esc U 撤回 五. 文件保存与退出5.1 Ctrl …...
SpringAI实战:ChatModel智能对话全解
一、引言:Spring AI 与 Chat Model 的核心价值 🚀 在 Java 生态中集成大模型能力,Spring AI 提供了高效的解决方案 🤖。其中 Chat Model 作为核心交互组件,通过标准化接口简化了与大语言模型(LLM࿰…...
前端高频面试题2:浏览器/计算机网络
本专栏相关链接 前端高频面试题1:HTML/CSS 前端高频面试题2:浏览器/计算机网络 前端高频面试题3:JavaScript 1.什么是强缓存、协商缓存? 强缓存: 当浏览器请求资源时,首先检查本地缓存是否命中。如果命…...

云原生安全实战:API网关Envoy的鉴权与限流详解
🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关 作为微服务架构的统一入口,负责路由转发、安全控制、流量管理等核心功能。 2. Envoy 由Lyft开源的高性能云原生…...