当前位置: 首页 > news >正文

单链表及其相关函数

实现功能

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下设置&#xff0c;否则权限不通过 echo 1>/proc/sys/kernel/core_uses_pid echo "/tmp/corefile-%e-%p-%t" >/proc/sys/kernel/core_pattern 3、编译…...

Gopro卡无法打开视频恢复方法

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

vmware虚拟机与树莓派4B安装ubuntu1804 + ros遇到的问题

如题所示&#xff0c;本人在虚拟机上安装ubuntu1804&#xff0c;可以很容易安装&#xff0c;并且更换系统apt源和ros源&#xff0c;然后安装ros&#xff0c;非常顺利&#xff0c;但是在树莓派4B上安装raspiberry系统就遇到了好多问题。 树莓派我烧录的是这个镜像&#xff1a;ub…...

JS逆向hook通用脚本合集

1. cookie 通用hook Cookie Hook 用于定位 Cookie 中关键参数生成位置&#xff0c;以下代码演示了当 Cookie 中匹配到了 v 关键字&#xff0c; 则插入断点 (function () {var cookieTemp ;Object.defineproperty(document, cookie, {set: function (val) {if (val.indexOf(v…...

nacos的介绍和下载安装(详细)

目录 一、介绍 1.什么是nacos&#xff08;含有官方文档&#xff09;&#xff1f; 2.nacos的作用是什么&#xff1f; 3.什么是nacos注册中心&#xff1f; 4.核心功能 二、下载安装 一、介绍 1.什么是nacos&#xff08;含有官方文档&#xff09;&#xff1f; 一个更易于…...

【算法经典题集】前缀和与数学(持续更新~~~)

&#x1f63d;PREFACE&#x1f381;欢迎各位→点赞&#x1f44d; 收藏⭐ 评论&#x1f4dd;&#x1f4e2;系列专栏&#xff1a;算法经典题集&#x1f50a;本专栏涉及到的知识点或者题目是算法专栏的补充与应用&#x1f4aa;种一棵树最好是十年前其次是现在前缀和一维前缀和k倍…...

寻找时空中的引力波:科学家控制量子运动至量子基态

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

第六讲:ambari-web 模块二次开发

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

echarts--提示框显示不全问题记录

最近接手一个同事之前做的网页&#xff0c;发现里面使用echarts来绘制各类图表&#xff1b;有2个问题一个是提示框显示不全&#xff0c;另一个就是绘制总是有部分数据显示不全。后者就是div宽度问题。。。无语&#xff0c;说下前面一个问题吧&#xff0c;记录一下。 tooltip组…...

LeetCode 1653. 使字符串平衡的最少删除次数

LeetCode 1653. 使字符串平衡的最少删除次数 难度&#xff1a;middle\color{orange}{middle}middle Rating&#xff1a;1794\color{orange}{1794}1794 题目描述 给你一个字符串 sss &#xff0c;它仅包含字符 ′a′a′a′ 和 ′b′b′b′​​​​ 。 你可以删除 sss 中任意…...

聊一聊代码重构——程序方法和类上的代码实践

使用工厂方法取代构造方法 构造方法的问题 我们使用构造方法来初始化对象时候&#xff0c;我们得到的只能是当前对象。而使用工厂方法替换构造方法&#xff0c;我们可以返回其子类型或者代理类型。这让我们可以通过不同的实现类来进行逻辑实现的变化。 更重要的一点是&#…...

嵌入式学习笔记——寄存器开发STM32 GPIO口

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

[ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限

&#x1f36c; 博主介绍 &#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 _PowerShell &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【数据通信】 【通讯安全】 【web安全】【面试分析】 &#x1f389;点赞➕评论➕收藏 养成习…...

程序设计与 C 语言期末复习

程序设计与 C 语言 1.计算机语言与编译 机器语言&#xff1a;一串仅由 0 和 1 序列表示的语言。计算机只能识别和接受 0 和 1 组成的指令。 符号语言&#xff08;汇编语言&#xff09;&#xff1a;用一些英文字母和数字表示一个指令。 符号语言&#xff08;汇编语言&#xf…...

05-思维导图Xmind快速入门

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

使用去中心化存储构建网站

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

L - Let‘s Swap(哈希 + 规律)

2023河南省赛组队训练赛&#xff08;四&#xff09; - Virtual Judge (vjudge.net) 约瑟夫最近开发了一款名为Pandote的编辑软件&#xff0c;现在他正在测试&#xff0c;以确保它能正常工作&#xff0c;否则&#xff0c;他可能会被解雇!Joseph通过实现对Pandote上字符串的复制和…...

c语言自动内存回收(RAII实现)

简述 什么是RAII RAII&#xff08;Resource Acquisition Is Initialization&#xff09;是c之父Bjarne Stroustrup提出的概念。资源一般分三个步骤&#xff1a;获取、使用和销毁&#xff0c;而在自由使用内存的c语言中&#xff0c;资源的销毁常常是程序员容易遗漏的事情&…...

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 代码终端中的快…...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

MongoDB学习和应用(高效的非关系型数据库)

一丶 MongoDB简介 对于社交类软件的功能&#xff0c;我们需要对它的功能特点进行分析&#xff1a; 数据量会随着用户数增大而增大读多写少价值较低非好友看不到其动态信息地理位置的查询… 针对以上特点进行分析各大存储工具&#xff1a; mysql&#xff1a;关系型数据库&am…...

【Linux】C语言执行shell指令

在C语言中执行Shell指令 在C语言中&#xff0c;有几种方法可以执行Shell指令&#xff1a; 1. 使用system()函数 这是最简单的方法&#xff0c;包含在stdlib.h头文件中&#xff1a; #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...

【Zephyr 系列 10】实战项目:打造一个蓝牙传感器终端 + 网关系统(完整架构与全栈实现)

🧠关键词:Zephyr、BLE、终端、网关、广播、连接、传感器、数据采集、低功耗、系统集成 📌目标读者:希望基于 Zephyr 构建 BLE 系统架构、实现终端与网关协作、具备产品交付能力的开发者 📊篇幅字数:约 5200 字 ✨ 项目总览 在物联网实际项目中,**“终端 + 网关”**是…...

【Java_EE】Spring MVC

目录 Spring Web MVC ​编辑注解 RestController RequestMapping RequestParam RequestParam RequestBody PathVariable RequestPart 参数传递 注意事项 ​编辑参数重命名 RequestParam ​编辑​编辑传递集合 RequestParam 传递JSON数据 ​编辑RequestBody ​…...

(转)什么是DockerCompose?它有什么作用?

一、什么是DockerCompose? DockerCompose可以基于Compose文件帮我们快速的部署分布式应用&#xff0c;而无需手动一个个创建和运行容器。 Compose文件是一个文本文件&#xff0c;通过指令定义集群中的每个容器如何运行。 DockerCompose就是把DockerFile转换成指令去运行。 …...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

CSS设置元素的宽度根据其内容自动调整

width: fit-content 是 CSS 中的一个属性值&#xff0c;用于设置元素的宽度根据其内容自动调整&#xff0c;确保宽度刚好容纳内容而不会超出。 效果对比 默认情况&#xff08;width: auto&#xff09;&#xff1a; 块级元素&#xff08;如 <div>&#xff09;会占满父容器…...

JVM虚拟机:内存结构、垃圾回收、性能优化

1、JVM虚拟机的简介 Java 虚拟机(Java Virtual Machine 简称:JVM)是运行所有 Java 程序的抽象计算机,是 Java 语言的运行环境,实现了 Java 程序的跨平台特性。JVM 屏蔽了与具体操作系统平台相关的信息,使得 Java 程序只需生成在 JVM 上运行的目标代码(字节码),就可以…...