C语言复习-链表
链表:
特点:
通过 next 指针 把内存上不连续 的几段数据 联系起来
set nu -- 打印行号
概念: 一种数据结构 -- 数据存放的思想
比如 -- 数组 -- 内存连续的一段空间,存放相同类型的一堆数据
缺点 -- 增删元素很 难 -- 不灵活 --> 引入链表
next指针的初步认识:
#include<stdio.h>struct chain
{
int num;
struct chain* next;
};
int main()
{
int arr[3]={1,2,3};
puts("use arr to output:");for(int i=0;i<3;++i)
{
printf("%d ",arr[i]);
}
puts("");
struct chain a={1,NULL};
struct chain b={2,NULL};
struct chain c={3,NULL};a.next=&b;
b.next=&c;
puts("use chain to output:");
printf("%d %d %d\n",a.num,a.next->num,b.next->num);return 0;
}
遍历:
遍历条件 -- 尾部元素的next 指针为NULL -- 结束条件
遍历 只需要 传入首元素的地址就-ok -> 后续元素直接 next去获取
#include<stdio.h>struct chain
{
int num;
struct chain* next;
};
void printChain(struct chain* head)
{
struct chain* p=head;
while(p!=NULL)
{
printf("%d ",p->num);
p=p->next;
}puts("");
}
int main()
{
int arr[3]={1,2,3};
puts("use arr to output:");for(int i=0;i<3;++i)
{
printf("%d ",arr[i]);
}
puts("");
struct chain a={1,NULL};
struct chain b={2,NULL};
struct chain c={3,NULL};a.next=&b;
b.next=&c;
puts("use chain to output:");
//printf("%d %d %d\n",a.num,a.next->num,b.next->num);printChain(&a);
return 0;
}//p-> next 下一个元素的地址
==========================================
列表的查找
int getTotal(struct chain* head) //获得链表长度
{
int cnt=0;
while(head!=NULL)
{
cnt++;
head=head->next;
}
return cnt;
}int check(struct chain* head,int n) //查看n是否在链表内
{
while(head!=NULL)
{
if(head->num==n) return 1;head=head->next;
return 0;
}
=================================
插入新节点
后面插入
int insert(struct chain* head,int data,struct chain*new)
{
struct chain* p=head;
while(p!=NULL)
{
if(p->num==data) //找到目标元素位置
{
new->next=p->next; //插入到目标元素后面
p->next=new; //目标元素的下一个数改为插入值
return 1;
}p=p->next;
}
return 0;}
==============================================
前面插入
struct chain* frontInsert(struct chain* head,int data,struct chain*new)
{
struct chain* p=head;
if(p->num==data) //在头部插入
{
new->next=head;//换头
puts("insert succeed");
return new;
}while(p->next!=NULL)
{
if(p->next->num==data) // 找到目标元素的上一个元素位置
{
new->next=p->next; //差到目标元素上一个元素的后面就是目标元素的前面
p->next=new; //目标元素的上一个元素指向我们的插入值
puts("insert succeed");
return head;
}
p=p->next;}
puts("insert failed!!");
return head;}
==================================
删除指定节点:
分情况:
1.改头 -- 记得把之前的头free掉(malloc 创建的才能free -- 一般也是malloc去创建),避免浪费系统空间
2.跳过
struct chain* myRemove(struct chain* head,int data)
{
struct chain *p =head;if(p->num==data) //第一个就是目标
{
head=head->next;
return head;
}while(p->next!=NULL)
{
if(p->next->num==data)
{
p->next=p->next->next; //跳过中间值 == 删除//注-- if 是malloc 动态创建的内存空间这里要free释放掉--一般都是动态内存空间
return head;
}
p=p->next;
}
return head;
}
================================================
链表的动态创建:
头插法
struct chain* frontCreate(struct chain* head)
{
struct chain*new=NULL;
while(1)
{
new=(struct chain*)malloc(sizeof(struct chain)); //拿到一块新的内存空间
puts("清输入一个数字, 0--退出!");
scanf("%d",&new->num);if(new->num==0)
{
puts("0 -- quit");
return head;
}if(head==NULL)
{
head=new; //给链表头赋值
}
else
{
new->next=head; //头插,新元素插到头后面
head=new; //然后新的节点变成头 -- 类似栈 --先进后出
}
}return head;
}
========================================
优化-- 循环 和 头插 分开写
struct chain *frontCreate(struct chain *head, struct chain *new)
{
if (head == NULL)
{
head = new;
}
else
{
new->next = head;
head = new;
}
return head;
}
struct chain *myCreate(struct chain *head)
{
while (1)
{
struct chain *new = NULL;
new = (struct chain *)malloc(sizeof(struct chain));
puts("清输入一个数字, 0--退出!");
scanf("%d", &new->num);
if (new->num == 0)
{
puts("0 -- quit");
free(new);
return head;
}
head= frontCreate(head, new);
}
return head;
}
========================================
尾插法
struct chain *behindCreate(struct chain *head, struct chain *new)
{
struct chain *p = head;
if (head == NULL)
{
head = new;
return head;
}
while (p->next != NULL) //拿到尾部位置 p==NULL
{
p = p->next;
}
p->next = new; //直接在当前链表的尾部添加return head;
}
===================================
整个程序:
#include <stdio.h>struct chain
{
int num;
struct chain *next;
};void printChain(struct chain *head)
{
struct chain *p = head;
while (p != NULL)
{
printf("%d ", p->num);
p = p->next;
}puts("");
}int getTotal(struct chain *head)
{
int cnt = 0;
while (head != NULL)
{
cnt++;
head = head->next;
}
return cnt;
}int check(struct chain *head, int n)
{
while (head != NULL)
{
if (head->num == n)
return 1;head = head->next;
return 0;
}
}void modif(struct chain *head, int n, int new)
{
struct chain *p = head;
while (p != NULL)
{
if (p->num == n)
{
p->num = new;
puts("修改成功!");
}p = p->next;
}
}int insert(struct chain *head, int data, struct chain *new)
{
struct chain *p = head;
while (p != NULL)
{
if (p->num == data)
{
new->next = p->next;
p->next = new;
return 1;
}p = p->next;
}
return 0;
}struct chain *frontInsert(struct chain *head, int data, struct chain *new)
{
struct chain *p = head;
if (p->num == data) // 在头部插入
{
new->next = head; // 换头
puts("insert succeed");
return new;
}while (p->next != NULL)
{
if (p->next->num == data)
{new->next = p->next;
p->next = new;
puts("insert succeed");
return head;
}
p = p->next;
}
puts("insert failed!!");
return head;
}struct chain *myRemove(struct chain *head, int data)
{
struct chain *p = head;if (p->num == data) // 第一个就是目标
{
head = head->next;
return head;
}while (p->next != NULL)
{
if (p->next->num == data)
{
p->next = p->next->next;
return head;
}
p = p->next;
}
return head;
}struct chain *frontCreate(struct chain *head, struct chain *new)
{if (head == NULL)
{
head = new;
}
else
{
new->next = head;
head = new;
}return head;
}struct chain *behindCreate(struct chain *head, struct chain *new)
{
struct chain *p = head;
if (head == NULL)
{
head = new;
return head;
}
while (p->next != NULL)
{
p = p->next;
}
p->next = new;return head;
}struct chain *myCreate(struct chain *head,int func)
{
while (1)
{
struct chain *new = NULL;new = (struct chain *)malloc(sizeof(struct chain));
puts("清输入一个数字, 0--退出!");
scanf("%d", &new->num);if (new->num == 0)
{
puts("0 -- quit");
free(new);
return head;
}
if(func)
head = frontCreate(head, new);
else
head = behindCreate(head, new);
}
return head;
}int main()
{puts("use chain to output:");
// printf("%d %d %d\n",a.num,a.next->num,b.next->num);
struct chain *head = NULL;
int func;
puts("清选择插入方式:1--头插 0--尾插");
scanf("%d",&func);
head = myCreate(head,func);printChain(head);
// insert(&a,1,&new);
// head=frontInsert(head,4,&new);
// head=myRemove(head,4);
// modif(head,4,255);
// printChain(head);return 0;
}
相关文章:
C语言复习-链表
链表: 特点: 通过 next 指针 把内存上不连续 的几段数据 联系起来 set nu -- 打印行号 概念: 一种数据结构 -- 数据存放的思想 比如 -- 数组 -- 内存连续的一段空间,存放相同类型的一堆数据 缺点 -- 增删元素很 难 -- 不灵活 --> 引入链表 next指针的初步认识…...
Redis面试题-缓存雪崩、缓存穿透、缓存击穿问题
1 穿透: 两边都不存在(皇帝的新装) (黑名单) (布隆过滤器) 2 击穿:一个热点的key失效了,这时大量的并发请求直接到达数据库. (提前预热) 3 雪崩:…...
【Node.js】npx
概述 npx 可以使用户在不安装全局包的情况下,运行已安装在本地项目中的包或者远程仓库中的包。 高版本npm会自带npx命令。 它可以直接运行 node_modules/.bin 下的 exe 可执行文件。而不像之前,我们需要在 scripts 里面配置,然后 npm run …...
hive授予指定用户特定权限及beeline使用
背景:因业务需要,需要使用beeline对hive数据进行查询,但是又不希望该用户可以查询所有的数据,希望有一个新用户bb给他指定的库表权限。 解决方案: 1.赋权语句,使用hive管理员用户在终端输入hive进入命令控…...
Vmware虚拟机无法用root直连说明
Vmware虚拟机无法用root直连说明 背景目的SSH服务介绍无法连接检查配置 背景 今天在VM上新装了一套Centos-stream-9系统,网络适配器的连接方式采用的是桥接,安装好虚拟机后,在本地用ssh工具进行远程连接,ip、用户、密码均是成功的…...
Visio中存在问题的解决方法
公式缩放 mathtype公式在visio缩放之后,出现了变形。 解决方法:每次输入公式都通过 插入->对象->mathType Equation 新建一个公式。可以避免 注:网上有的说在word中使用mathtype编写公式,之后复制到visio中。 插入波形 选择…...
taro之Swiper的使用
图样: 往往我们需要轮播图去显示我们想要的图片之类的 这是工作的代码 <View classNametop-title><SwiperclassNamebanner-swiperinterval{3000}circularautoplay>{homeBannerList.map((item) > {return (<SwiperItem key{item.id}><View…...
正大国际:金融行业发展趋势
2024金融科技趋势研究报告 大模型生态揭秘!金融行业迎来变革,中控成生态核心,大模型在金融行业的应用 随着大模型的不断发展,越来越多的金融机构开始尝试在一些业务场景中引入大模型和生成式A能力,预计2024年,领先的金…...
vue中实现超出一行 展开和收起的功能
html中: <divclass="txttype"ref="txttype"style="margin-bottom: 6px":class="hidetext == true ? hidetext : "><div style="width: 96%"><el-tagtype="info"style="margin-right: 10px&…...
记录一次使用cert-manager-颁发CA证书
一、官网 SelfSigned - cert-manager Documentation 二、例子 apiVersion: v1 kind: Namespace metadata:name: sandbox --- apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata:name: selfsigned-issuer spec:selfSigned: {} --- apiVersion: cert-manager.io/v…...
生成式AI的风险与挑战
生成式AI,即通过训练数据生成新的文本、图像或音频等内容的人工智能技术,具有很多潜在的风险与挑战。 1. 信息可信度:生成式AI往往是基于大量训练数据,但这些数据可能存在偏见、错误或虚假信息。生成的内容可能会引入不准确或误导…...
让IIS支持.NET Web Api PUT和DELETE请求
前言 有很长一段时间没有使用过IIS来托管应用了,今天用IIS来托管一个比较老的.NET Fx4.6的项目。发布到线上后居然一直调用不同本地却一直是正常的,关键是POST和GET请求都是正常的,只有PUT和DELETE请求是有问题的。经过一番思考忽然想起来了I…...
运维小技能:IP多号段配置、重置Mac电脑密码、修改系统级别的文件
文章目录 I 清除last_run_metadata_path数据。1.1 删除文件1.2 清空一个目录下所有文件的内容1.3 定期重启Logstash,并清除last_run_metadata_path数据。II 配置IP2.1 CentOS系统的IP参数2.2 shell脚本-静态网络配置III 电脑的IP多号段配置3.1 Mac电脑3.2 windows系统IV mac Ro…...
Docker的Ubuntu上的安装教程及相关命令
一、简介 Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,这个容器是完全使用沙箱机制(限制容器内部对系统资源的访问),更重要的是容器性能开销极低。 正是因为…...
一些常见的nacos问题和答案
什么是Nacos?它的作用是什么? Nacos是一个动态服务发现、配置管理和服务管理平台。它的作用是帮助应用程序实现服务注册与发现、动态配置管理和服务健康管理等功能。 Nacos的核心功能包括哪些: 服务注册与发现:Nacos支持基于DN…...
华为OD机22道试题
华为OD机试题 2.查找小朋友的好朋友位置 在学校中,N 个小朋友站成一队,第 i 个小朋友的身高为 height[i],第 i 个小朋友可以看到第一个比自己身高更高的小朋友j,那么 j 是 i 的好朋友 (要求:j>i) 。 请重新生成一个…...
什么是Prompt Tuning?
本文是观看视频What is Prompt Tuning?后的笔记。 大语言模型(如ChatGPT )是基础模型,是经过互联网上大量知识训练的大型可重用模型。 他们非常灵活,同样的模型可以分析法律文书或撰写文章。 但是,如果我们需要用其解…...
正则表达式篇
文章目录 1. 导入re模块2. 正则表达式的基本模式3. re模块的主要函数和方法4. 示例 正则表达式(Regular Expression,常简写为regex或regexp)是一种强大的文本处理工具,它使用一种特殊的字符序列来帮助用户检查一个字符串是否与某种…...
CAST(columnA AS VARCHAR(255)) AS fieldA报错的问题
列类型转换,不能使用VARCHAR,是能使用CHAR 应该改为: CAST(columnA AS CHAR(255)) AS fieldA报错的问题...
github加速神器!解决github巨慢的问题,并且能够加速下载!另外推荐GitKraken -- 超好用的 Git 可视化工具
FastGithub github加速神器,解决github打不开、用户头像无法加载、releases无法上传下载、git-clone、git-pull、git-push失败等问题。 下载地址: 清华云盘 2 部署方式 2.1 windows-x64桌面 双击运行FastGithub.UI.exe 2.2 windows-x64服务 fastgi…...
深度学习基石:从卷积神经网络理解 Stable Yogi 的图像生成能力
深度学习基石:从卷积神经网络理解 Stable Yogi 的图像生成能力 你是不是也好奇,像 Stable Yogi 这样能“凭空”画出精美图片的模型,它的“眼睛”和“大脑”究竟是怎么工作的?为什么给它一段文字描述,它就能理解并生成…...
本地部署开源推送通知系统 ntfy 并实现外部访问
ntfy 是一款简单、轻量级且功能强大的开源推送通知系统,它的核心目标是让用户或开发者能够轻松地从任何设备、任何地方向自己的手机或桌面发送通知。本文将详细介绍如何在 Linux 系统局域网内部署 ntfy 并结合路由侠实现外网访问局域网内部署的 ntfy 。 第一步&…...
VS2019调试配置报错解析:Designtime生成失败与IntelliSense不可用的深度排查指南
1. 问题现象与初步诊断 当你打开VS2019项目时突然弹出"配置Debug|Win32的Designtime生成失败,IntelliSense可能不可用"的红色错误提示,代码编辑窗口里的智能提示全部消失,连最基本的语法高亮都失效了——这种场景我遇到过不下20次。…...
Balena Etcher:三步完成系统镜像烧录,告别复杂命令的困扰
Balena Etcher:三步完成系统镜像烧录,告别复杂命令的困扰 【免费下载链接】etcher Flash OS images to SD cards & USB drives, safely and easily. 项目地址: https://gitcode.com/GitHub_Trending/et/etcher 你是否曾经因为需要制作系统启动…...
双冗余链路实现(2/2期)
目录 拓扑: 基础需求: 出口路由器(双路): 静态路由: 防火墙配置: 全区域互通透传: 静态路由: 冗余备份: 核心交换机: 静态路由ÿ…...
别再被‘万向死锁’吓到了!一个拧瓶盖的日常例子,5分钟搞懂欧拉角和四元数的区别
从拧瓶盖到游戏开发:用生活常识破解万向死锁之谜 想象一下,你正试图拧开一瓶顽固的矿泉水瓶盖。第一次尝试,你顺时针旋转瓶盖——没动静;于是你调整手腕角度再次尝试,这次瓶盖却意外滑脱了方向。这种日常挫败感&#x…...
Halcon图像高效转换:HObject到Bitmap的优化实践(20ms内完成)
1. 为什么需要HObject到Bitmap的高效转换 在工业视觉和深度学习应用中,Halcon的HObject图像格式和Windows平台的Bitmap格式就像两个说着不同语言的人。我遇到过太多这样的场景:当我们需要把Halcon处理后的图像交给TensorFlow做推理,或者要在…...
【OFDM通信】基于matlab室内NOMA-OFDM-VLC系统仿真【含Matlab源码 15240期】
💥💥💥💥💥💥💞💞💞💞💞💞💞💞欢迎来到海神之光博客之家💞💞💞Ὁ…...
华硕笔记本CPU过热?G-Helper降压调优终极指南帮你降温10℃
华硕笔记本CPU过热?G-Helper降压调优终极指南帮你降温10℃ 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops. Control tool for ROG Zephyrus G14, G15, G16, M16, Flow X13, Flow X16, TUF, Strix, Scar and other models 项目…...
MusePublic Art Studio参数详解:随机种子锁定与艺术风格复现方法
MusePublic Art Studio参数详解:随机种子锁定与艺术风格复现方法 1. 理解随机种子:艺术创作的"基因密码" 在AI图像生成领域,随机种子就像是每幅作品的DNA序列。它决定了生成过程中的随机性因素,是控制输出结果一致性的…...
