文章目录

  • 一、思维导图
  • 二、单链表代码
    • head.h
    • text.c
    • main.c
    • 现象

一、思维导图

在这里插入图片描述

二、单链表代码

head.h

#ifndef __HEAD_H__
#define __HEAD_H__#include <stdlib.h>
#include <stdio.h>
#include <string.h>enum A
{FAULSE=-1,//失败返回SUCCESS//成功返回};//给普通节点的数据域类型起别名
typedef int datatype;//定义节点的结构体
typedef struct Node
{//数据域union{datatype data;//普通元素的数据域int len;//头结点的数据元素:数据长度};//指针域:存储下一个节点的地址struct Node *next;
}*linklist;//单链表的节点创建
linklist creat_node(int flag);
//头插
int insert_head(linklist head,datatype element);
//输出
int output(linklist head);
//尾插
int insert_end(linklist head,datatype element);
//头删
int delate_head(linklist head);
//尾删
int delate_end(linklist head);
//按位置查找
int search_place(linklist head,int place);
//按位置删除
int delate_place(linklist head,int place);
//按位置插入
int insert_place(linklist head,int place,datatype element);
//按位置修改
int change_place(linklist head,int place,datatype element);
//按元素查找
int search_number(linklist head,datatype element);
//按元素修改
int change_number(linklist head,datatype element,datatype number);
//按元素删除
int delate_number(linklist head,datatype element);
//逆置
int reverse(linklist head);
//找倒数第n个节点
int find_n(linklist head,int n);
//单链表释放内存
int my_free(linklist head);
//单链表排序
int bubble(linklist head);#endif

text.c

#include "head.h"//单链表的节点创建
linklist creat_node(int flag)
{linklist s=(linklist)malloc(sizeof(struct Node));if(s==NULL){return NULL;}//内存申请成功//flag==1给头结点初始化if(flag==1){s->len=0;}//flag==0给普通节点初始化else if(flag==0){s->data==0;}//初始化指针域s->next=NULL;return s;
}//头插
int insert_head(linklist head,datatype element)
{//判断头结点是否存在if(head==NULL){return FAULSE;}//创建节点linklist s=creat_node(0);if(s==NULL){return FAULSE;}//节点创建成功s->data=element;//对新节点的数据域赋值//插入新节点s->next=head->next;head->next=s;//链表长度加1head->len++;return SUCCESS;
}//输出
int output(linklist head)
{//判断head是否为null//判断内容是否为空if(head==NULL||head->len==0){return FAULSE;}linklist L=head;/*for(int i=0;i<head->len;i++){printf("%d  ",L->next->data);L=L->next;}*/while(L->next!=NULL){printf("%d  ",L->next->data);L=L->next;}putchar(10);return SUCCESS;
}//尾插
int insert_end(linklist head,datatype element)
{//判断头结点是否存在if(head==NULL){return FAULSE;}linklist end=head;while(end->next!=NULL)//寻找尾节点{end=end->next;}//创建尾节点linklist s=creat_node(0);//输入尾节点的值s->data=element;//让尾节点指向新的尾节点end->next=s;head->len++;return SUCCESS;}//头删
int delate_head(linklist head)
{//判断头结点是否存在//判断头结点是否为空if(head==NULL||head->next==NULL){return FAULSE;}//标记要删除的节点linklist index=head->next;//让头结点指向要删除节点的下一个节点head->next=index->next;//释放删除节点的内存free(index);//防止野指针index=NULL;head->len--;return SUCCESS;
}//尾删
int delate_end(linklist head)
{if(head==NULL||head->next==NULL){return FAULSE;}//定义尾节点的上一个节点linklist index=head;while(index->next->next!=NULL){index=index->next;}//删除尾节点free(index->next);index->next=NULL;head->len--;return SUCCESS;
}//按位置查找
int search_place(linklist head,int place)
{//判断头结点是否存在//判断位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}printf("第%d个元素的数值为%d\n",place,p->data);return SUCCESS;
}//按位置删除
int delate_place(linklist head,int place)
{//判断头结点是否存在//判断位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}linklist L=p->next;p->next=L->next;free(L);L->next=NULL;head->len--;return SUCCESS;}//按位置插入
int insert_place(linklist head,int place,datatype element)
{//判断头结点是否存在//判断位置是否合法if(head==NULL||place<1||place>head->len+1){return FAULSE;}//寻找插入节点前一个节点linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}//创建节点linklist L=creat_node(0);if(L==NULL)return FAULSE;//传值L->data=element;L->next=p->next;p->next=L;head->len++;return SUCCESS;}//按位置修改
int change_place(linklist head,int place,datatype element)
{//判断头结点是否存在//判断位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}p->data=element;return SUCCESS;
}//按元素查找
int search_number(linklist head,datatype element)
{//判断节点是否存在if(head==NULL){return FAULSE;}linklist p=head;int pos=0;while(p!=NULL){if(p->data==element){return pos;}pos++;p=p->next;}return FAULSE;
}//按元素修改
int change_number(linklist head,datatype element,datatype number)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;change_place(head,pos,number);return SUCCESS;
}//按元素删除
int delate_number(linklist head,datatype element)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;delate_place(head,pos);return SUCCESS;}
//逆置
int reverse(linklist head)
{//判断头结点是否存在//判断其他节点是0个或1个if(NULL==head||head->len<=1){return FAULSE;}//逆置linklist p=head->next;head->next=NULL;for(int i=0;i<head->len;i++){linklist temp=p;p=p->next;//把temp头插temp->next=head->next;head->next=temp;}return SUCCESS;
}//找倒数第n个节点
int find_n(linklist head,int n)
{if(head==NULL||n<1||n>head->len){return FAULSE;}linklist p=head;linklist q=head;for(int i=0;i<n;i++){q=q->next;}while(q!=NULL){p=p->next;q=q->next;}printf("倒数第%d个节点的值为%d\n",n,p->data);return SUCCESS;
}//单链表释放内存
int my_free(linklist head)
{if(head==NULL){return FAULSE;}int len=head->len;for(int i=0;i<len;i++){delate_end(head);}free(head);
}//单链表排序
int bubble(linklist head)
{if(NULL==head||head->len<=1){return FAULSE;}for(int i=0;i<head->len-1;i++){int flag=0;linklist p=head->next;for(int j=0;j<head->len-1-i;j++){if(p->data>p->next->data){datatype temp=p->data;p->data=p->next->data;p->next->data=temp;flag=1;}p=p->next;}if(flag==0){break;}}return SUCCESS;
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{//创建头结点linklist head=creat_node(1);//头插insert_head(head,15);insert_head(head,13);insert_head(head,12);insert_head(head,21);insert_head(head,19);insert_head(head,12);insert_head(head,54);insert_head(head,14);printf("头插后的结果:\n");output(head);//尾插insert_end(head,11);printf("尾插后的结果:\n");output(head);//按位置删除delate_place(head,3);printf("按位置删除后的结果:\n");output(head);//按位置查找search_place(head,2);//头删delate_head(head);printf("头删后的结果:\n");output(head);//按位置插入insert_place(head,2,14);printf("按位置插入的结果:\n");output(head);//按位置修改change_place(head,2,98);printf("按位置修改后的结果:\n");output(head);//尾删delate_end(head);printf("按尾删后的结果:\n");output(head);//逆置reverse(head);printf("逆置后的结果:\n");output(head);//按元素查找int pos=search_number(head,13);printf("查找到元素为第%d个\n",pos);//按元素修改change_number(head,15,99);printf("按元素修改后的结果:\n");output(head);//按元素删除delate_number(head,54);printf("按元素删除后的结果:\n");output(head);//找倒数第n个节点find_n(head,2);//单链表排序bubble(head);printf("排序之后的结果:\n");//输出output(head);//单链表释放内存my_free(head);head=NULL;return 0;
}

现象

头插后的结果:
14 54 12 19 21 12 13 15
尾插后的结果:
14 54 12 19 21 12 13 15 11
按位置删除后的结果:
14 54 19 21 12 13 15 11
第2个元素的数值为54
头删后的结果:
54 19 21 12 13 15 11
按位置插入的结果:
54 14 19 21 12 13 15 11
按位置修改后的结果:
54 98 19 21 12 13 15 11
按尾删后的结果:
54 98 19 21 12 13 15
逆置后的结果:
15 13 12 21 19 98 54
查找到元素为第2个
按元素修改后的结果:
99 13 12 21 19 98 54
按元素删除后的结果:
99 13 12 21 19 98
倒数第2个节点的值为19
排序之后的结果:
12 13 19 21 98 99

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

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

相关文章

某种物联网SIM卡流量查询方法

说起流量卡,很多人可能还停留在营业厅办理的常规套餐里。但其实在 2016 年,三大运营商就推出了一种资费更为划算的正规流量卡 —— 物联卡。当年,当不少人还在用 50 元 1G 的流量时,第一批体验物联卡的用户已经享受到了 53 元 6G 的全国流量,彻底摆脱了流量焦虑。不过,至…

XTTS实现语音克隆:精确控制音频格式与生成流程【TTS的实战指南】

言简意赅的讲解XTTS解决的痛点 &#x1f4ce; 前置操作&#xff1a;如何使用 OBS Studio 录制高质量 WAV 语音&#xff08;建议先阅读并准备录音样本&#xff09; 本教程介绍如何使用 Coqui TTS 的 XTTS v2 模型 实现中文语音克隆&#xff0c;支持直接传入 .wav 文件&#xff0…

C/C++中常量放置在比较操作符左侧

目录 介绍 原因详解 避免误用赋值运算符 示例对比 结论 介绍 在编程中&#xff0c;将常量放在比较操作符&#xff08;如 或 !&#xff09;的左侧&#xff08;例如 if (42 value)&#xff09;&#xff0c;是一种被称为 "Yoda 条件"&#xff08;Yoda Conditions…

Node.js 模拟 Linux 环境

&#x1f9e9; 项目介绍 该项目使用 Node.js 实现了一个模拟的 Linux 终端环境&#xff0c;支持多种常见的 Linux 命令&#xff08;如 ls, cd, cat, mkdir, rm 等&#xff09;&#xff0c;所有文件操作都在内存中进行&#xff0c;并持久化到本地文件系统中。适合用于学习 Shel…

HAProxy 实验指南:从零开始搭建高可用负载均衡系统

引言HAProxy&#xff08;High Availability Proxy&#xff09;是一款高性能的TCP/HTTP负载均衡器和代理服务器&#xff0c;广泛用于构建高可用、可扩展的Web架构。它由法国开发者Willy Tarreau于2000年开发&#xff0c;如今已成为开源社区和企业级应用中不可或缺的工具。HAProx…

2.10DOM和BOM插入/移除/克隆

1.DOM创建/插入/移除/克隆1.1创建元素前面我们使用过 document.write 方法写入一个元素&#xff1a;这种方式写起来非常便捷&#xff0c;但是对于复杂的内容、元素关系拼接并不方便&#xff1b;它是在早期没有 DOM 的时候使用的方案&#xff0c;目前依然被保留了下来&#xff1…

华为仓颉编程语言的表达式及其特点

华为仓颉编程语言的表达式及其特点 仓颉&#xff08;Cangjie&#xff09;语言的表达式有一个明显的特点&#xff0c;范围不再局限于传统算术运算&#xff0c;而是扩展到条件表达式、循环表达式等多种类型&#xff0c;每种表达式均有确定的类型和值。 传统基本表达式&#xff0…

【linux】keepalived

一.高可用集群1.1 集群类型LB&#xff1a;Load Balance 负载均衡 LVS/HAProxy/nginx&#xff08;http/upstream, stream/upstream&#xff09; HA&#xff1a;High Availability 高可用集群 数据库、Redis SPoF: Single Point of Failure&#xff0c;解决单点故障 HPC&#xff…

Webpack配置原理

一、Loader&#xff1a; 1、定义&#xff1a;将不同类型的文件转换为 webpack 可识别的模块2、分类&#xff1a; ① pre&#xff1a; 前置 loader &#xff08;1&#xff09;配置&#xff1a;在 webpack 配置文件中通过enforce进行指定 loader的优先级配置&#xff08;2&#x…

对比JS“上下文”与“作用域”

下面从定义、特性、示例&#xff0c;以及在代码分析中何时侧重“上下文”&#xff08;Execution Context/this&#xff09;和何时侧重“作用域”&#xff08;Scope/变量查找&#xff09;&#xff0c;以及二者结合的场景来做对比和指导。一、概念对比 | 维度 | 上下文&#xff0…

如何做数据增强?

目录 1、为什么要做数据增强&#xff1f; 2、图像数据增强&#xff1f; 3、文本与音频数据增强&#xff1f; 4、高级数据增强&#xff1f; 数据增强技术就像是一种“造数据”的魔法&#xff0c;通过对原始数据进行各种变换&#xff0c;生成新的样本&#xff0c;从而提高模型…

Go by Example

网页地址Go by Example 中文版 Github仓库地址mmcgrana/gobyexample&#xff1a;按示例进行 HelloWorld package mainimport ("fmt" )func main() {fmt.Println("Hello World") } Hello World 值 package mainimport ("fmt" )func main() {…

ClickHouse高性能实时分析数据库-消费实时数据流(消费kafka)

告别等待&#xff0c;秒级响应&#xff01;这不只是教程&#xff0c;这是你驾驭PB级数据的超能力&#xff01;我的ClickHouse视频课&#xff0c;凝练十年实战精华&#xff0c;从入门到精通&#xff0c;从单机到集群。点开它&#xff0c;让数据处理速度快到飞起&#xff0c;让你…

电子电气架构 --- 车载软件与样件产品交付的方法

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 简单,单纯,喜欢独处,独来独往,不易合同频过着接地气的生活,除了生存温饱问题之外,没有什么过多的欲望,表面看起来很高冷,内心热情,如果你身…

C++:STL中vector的使用和模拟实现

在上一篇中讲到了string类&#xff0c;string并不属于STL中因为string出现的比STL早&#xff0c;但是在使用方法上两者有相似之处&#xff0c;学习完string后再来看vector会容易的多&#xff0c;接着往下阅读&#xff0c;一定会有收获滴&#xff01; 目录 vector的介绍 vect…

仓库管理的流程、绩效和解决方案?

什么是仓库管理&#xff1f; 仓库管理涉及对所有仓库运营的日常监督。一个全面、集成的仓库管理解决方案采用行业最佳实践&#xff0c;并涵盖使高效运营得以实现的所有基本要素。这些要素包括分销和库存管理、仓库劳动力管理以及业务支持服务。此外&#xff0c;由内部提供或与服…

TIM 实现定时中断【STM32L4】【实操】

使用定时器实现定时中断的功能&#xff1a;比如每1ms进入中断处理函数使用STM32CubeMX配置TIM初始化先了解每个参数的含义&#xff0c;在进行配置Counter Settings: 计数器基本设置Prescaler(PSC): 预分频器&#xff0c;设置预分频器系数Counter Mode: 技术模式&#xff0c;…

Elasticsearch 的聚合(Aggregations)操作详解

目录 1. 概述 2. 聚合类型分类详解 2.1 桶聚合&#xff08;Bucket Aggregations&#xff09; 2.1.1 基础桶聚合 2.1.2 特殊桶聚合 2.1.3 高级桶聚合 2.2 指标聚合&#xff08;Metric Aggregations&#xff09; 2.2.1 单值指标聚合&#xff08;Single-value Metrics&am…

电子电气架构 --- 高阶智能驾驶对E/E架构的新要求

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 做到欲望极简,了解自己的真实欲望,不受外在潮流的影响,不盲从,不跟风。把自己的精力全部用在自己。一是去掉多余,凡事找规律,基础是诚信;二是…

0.深度学习环境配置步骤

0.深度学习环境配置步骤 这里介绍深度学习环境配置详细步骤&#xff0c;包括安装软件&#xff0c;每一步都有安装时的截图&#xff08;后续持续更新&#xff0c;敬请关注&#xff09; 目录如下&#xff1a; 1.安装anaconda 2.安装CUDA 3.安装CU_DNN 4.安装pytorch