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

数据结构---链表的基本操作

  1. 头插法
  2. 遍历链表
  3. 尾插法
  4. 头删法
  5. 尾删法
  6. 按位置插入数据
  7. 按位置删除数据
  8. 直接插入排序
  9.  链表翻转
  10. 快慢指针

linklist.c

#include <stdio.h>
#include <stdlib.h>
#include "./linklist.h"linklist* create_linklist(void)
{linklist* head = (linklist*)malloc(sizeof(linklist));	if(NULL == head){printf("头结点申请失败!\n");return NULL;}head->text.len = 0;head->next = NULL;return head;
}
//头插法
int  insertHead_linlist(linklist *head,dataType num)
{//创建一个新结点linklist* temp = (linklist*)malloc(sizeof(linklist));if(NULL == temp){printf("创建失败!\n");return 0;}temp->text.data = num;temp->next = NULL;//头插法插入数据temp->next = head->next;head->next = temp;return 0;
}//遍历链表void show_linklist(linklist* head)
{linklist *p = head;while(p->next != NULL){p = p->next;printf("%d ",p->text.data);}printf("\n");//更新头结点中记录的链表长度head->text.len++;return;
}//尾插法
int insertTail_linlist(linklist* head,dataType num)
{linklist* temp = (linklist*)malloc(sizeof(linklist));if(NULL == temp){printf("创建失败!\n");return 0;}//初始化新结点temp->next = NULL;temp->text.data = num;linklist* p = head;while(p->next != NULL){p = p->next;}p->next = temp;temp->next = NULL;head->text.len++;return 0;
}
//判空
int isEmpty_linlist(linklist* head)
{return head->next == NULL?1:0;
}//头删
dataType delHead_linlist(linklist* head)
{if(isEmpty_linlist(head)){printf("链表为空!");return (dataType)0;}linklist* temp = head->next;head->next = head->next->next;dataType num = temp->text.data;free(temp);temp = NULL;head->text.len--;return num;
}//尾删
dataType delTail_linlist(linklist* head)
{if(isEmpty_linlist(head)){printf("链表为空!");return (dataType)0;}linklist* p = head;linklist* temp;while(p->next != NULL)   //p->next->next != NULL{temp = p;p = p->next;}temp->next=temp->next->next;free(p);    //free(temp->next)p = NULL;   //temp->next = NULLhead->text.len--;return 0;}//按位置插入
void insert_location_linlist(linklist *head,dataType index,dataType num)
{if(index<1 || index >head->text.len+1){printf("位置非法!");return;}int i;linklist* temp=head;linklist* p = (linklist*)malloc(sizeof(linklist));if(NULL == p){printf("插入失败!\n");return ;}//初始化新结点p->text.data = num;p->next = NULL;for(i=0;i<index-1;i++){temp = temp->next;}p->next = temp->next;temp->next = p;head->text.len++;return;}//按位置删除
void del_location_linlist(linklist* head,dataType index)
{if(isEmpty_linlist(head)){printf("链表为空!");return;}linklist* temp = head;for(int i=0;i<index-1;i++){temp = temp->next;}linklist* p = temp->next;temp->next = temp->next->next;free(p);p = NULL;head->text.len--;return;}//按位置查找
void find_location_linlist(linklist* head,dataType index)
{linklist* temp = head;for(int i=0;i<index;i++){temp = temp->next;}printf("%d\n",temp->text.data);head->text.len++;return;}//直接插入排序
void insert_sort_linlist(linklist* head,dataType num)
{linklist* p = head;int sum=0;while(p != NULL){sum++;p = p->next;}linklist* temp = head;for(int i=0;i<sum;i++){linklist* q = temp;while(q->next != NULL){if(q->text.data > q->next->text.data){int tem = q->text.data;q->text.data = q->next->text.data;q->next->text.data = tem;}q = q->next;}}int t=0;linklist* temp2 = head;for(int j=0;j<sum;j++){if(num > temp2->text.data){t++;}temp2 = temp2->next;}//按位置插入insert_location_linlist(head,t,num);}//链表翻转
void  reversal_linlist(linklist* head)
{linklist *p;linklist *q;p=head->next;head->next=NULL;while(p!=NULL){q=p;p=p->next;q->next=head->next;head->next=q;}
}
//快慢指针查找中间节点位置                    
void middleNode_linklist(linklist* head)      
{if(isEmpty_linlist(head)){printf("链表为空!");return;}                                         linklist *low,*fast;low = head->next;fast = head->next;                        while(fast != NULL && fast->next != NULL){low = low->next;                      fast = fast->next->next;}printf("low = %d\n",low->text.data);return;
} 

linklist.h

#ifndef __LINKLIDT_H__
#define __LINKLIDT_H__typedef int dataType;union msg{dataType data;int len;
};
typedef struct node
{struct node* next;union msg text; 
}linklist;linklist* create_linklist(void);
int  insertHead_linlist(linklist *head,dataType num);
void show_linklist(linklist* head);
int insertTail_linlist(linklist* head,dataType num);
dataType delHead_linlist(linklist* head);
dataType delTail_linlist(linklist* head);
void insert_location_linlist(linklist *head,dataType index,dataType num);
void del_location_linlist(linklist* head,dataType index);
void find_location_linlist(linklist* head,dataType index);
void insert_sort_linlist(linklist* head,dataType num);
void reversal_linlist(linklist* head);
void middleNode_linklist(linklist* head)
#endif

main.c

#include <stdio.h>
#include "./linklist.h"
int main(int argc, const char *argv[])
{linklist* head =  create_linklist();insertHead_linlist(head,11);insertHead_linlist(head,24);insertHead_linlist(head,46);insertHead_linlist(head,28);insertHead_linlist(head,18);
//	show_linklist(head);insertTail_linlist(head,36);insertTail_linlist(head,27);insertTail_linlist(head,30);show_linklist(head);/*	dataType num = 0;num = delHead_linlist(head);show_linklist(head);num = delHead_linlist(head);show_linklist(head);num = delTail_linlist(head);show_linklist(head);num = delTail_linlist(head);show_linklist(head);
*/	/*	insert_location_linlist(head,1,111);insert_location_linlist(head,5,555);insert_location_linlist(head,8,888);show_linklist(head);
*/	
/*	del_location_linlist(head,2);show_linklist(head);
*/
/*	find_location_linlist(head,1);find_location_linlist(head,3);find_location_linlist(head,8);
*/insert_sort_linlist(head,22);show_linklist(head);reversal_linlist(head);show_linklist(head);middleNode_linklist(head)return 0;
}  

相关文章:

数据结构---链表的基本操作

头插法遍历链表尾插法头删法尾删法按位置插入数据按位置删除数据直接插入排序 链表翻转快慢指针 linklist.c #include <stdio.h> #include <stdlib.h> #include "./linklist.h"linklist* create_linklist(void) {linklist* head (linklist*)malloc(siz…...

异步框架Celery在Django中的运用

参考博客&#xff1a;https://www.cnblogs.com/pyedu/p/12461819.html 参考视频&#xff1a;01 celery的工作机制_哔哩哔哩_bilibili 定义&#xff1a;简单灵活、处理大量消息的分布式系统&#xff0c;专注于实时处理异步队列&#xff0c;支持任务调度 主要架构&#xff1a; …...

YOLOv5代码解读[02] models/yolov5l.yaml文件解析

文章目录 YOLOv5代码解读[02] models/yolov5l.yaml文件解析yolov5l.yaml文件检测头1--->耦合头检测头2--->解耦头检测头3--->ASFF检测头Model类解析parse_model函数 YOLOv5代码解读[02] models/yolov5l.yaml文件解析 yolov5l.yaml文件 # YOLOv5 &#x1f680; by Ult…...

智能搬运机器人|海格里斯将如何持续推进工业和物流的智能化升级与发展?

存取、搬运、分拣是物流行业中的通用功能&#xff0c;但具体到每个行业又十分不同&#xff0c;例如&#xff1a;新能源电池领域&#xff0c;它所搬运的东西是电池&#xff0c;50KG~200KG&#xff1b;快递行业领域&#xff0c;所要处理的物料是那种扁平件和信封等等&#xff0c;…...

linux之前后端项目部署与发布

目录 前言 简介 一、安装Nginx 二、后端部署 2.1多个tomcat负载均衡 2.2 负载均衡 2.3 后端项目部署 三、前端部署 1.解压前端 2.Nginx配置文件修改 3.IP域名映射 4.重启Nginx服务 前言 上篇博主已经讲解过了单机项目的部署linux之JAVA环境配置JDK&Tomcat&a…...

Python 高级语法:一切皆对象

1 “一切皆对象”是一种核心设计哲学 在编程领域&#xff0c;特别是面向对象编程&#xff08;OOP&#xff09;中&#xff0c;“一切皆对象”是一种核心设计哲学。这种哲学主张&#xff0c;无论是数据、函数、还是更复杂的结构&#xff0c;都可以被视为对象&#xff0c;并赋予…...

python jupyter notebook打开页面方便使用

如果没安装jupyter, 请安装&#xff1a; pip install jupyter notebook 运行jupyter notebook jupyter-notebook...

音视频开发之旅(69)-SD图生图

目录 1. 效果展示 2. ControlNet介绍 3. 图生图流程浅析 4. SDWebui图生图代码流程 5. 参考资料 一、效果展示 图生图的应用场景非常多&#xff0c;比较典型的应用场景有风格转化&#xff08;真人与二次元&#xff09;、线稿上色、换装和对图片进行扩图等&#xff0c;下面…...

數據集成平台:datax將hive數據步到mysql(全部列和指定列)

數據集成平台&#xff1a;datax將hive數據步到mysql&#xff08;全部列和指定列&#xff09; 1.py腳本 傳入參數&#xff1a; target_database&#xff1a;數據庫 target_table&#xff1a;表 target_columns&#xff1a;列 target_positions&#xff1a;hive列的下標&#x…...

pikachu靶场-File Inclusion

介绍&#xff1a; File Inclusion(文件包含漏洞)概述 文件包含&#xff0c;是一个功能。在各种开发语言中都提供了内置的文件包含函数&#xff0c;其可以使开发人员在一个代码文件中直接包含&#xff08;引入&#xff09;另外一个代码文件。 比如 在PHP中&#xff0c;提供了&…...

[今天跟AI聊聊职场] ~你能接受你的直接领导能力不如你,年纪还比你小很多吗?

知乎问题&#xff1a; 弟弟今年35岁&#xff0c;刚换了一份工作&#xff0c;直接领导小A比他小5岁&#xff0c;各方面经验没有他成熟。难的工作都是弟弟在做&#xff0c;功劳都被直接领导小A抢走了&#xff0c;有时候还要被直接领导小A打压。弟弟感觉升职加薪无望。现在找工作不…...

网络原理TCP之“三次握手“

TCP内核中的建立连接 众所周知,TCP是有连接的. 当我们在客户端敲出socket new Socket(serverIp,severPort)时,就在系统内核就在建立连接 真正建立连接是在系统内核中建立的,我们程序员只是调用相关的api. 在此处,我们把TCP的建立连接称为三次握手. 系统在内核建立连接时如上…...

990-03产品经理与程序员:什么是 IT 与业务协调以及为什么它很重要?

What is IT-business alignment and why is it important? 什么是IT-业务一致性&#xff1f;为什么它很重要&#xff1f; It’s more important than ever that IT and the business operate from the same playbook(剧本). So why do so many organizations struggle to ach…...

Java Web(七)__Tomcat(二)

Tomcat工作模式 Tomcat作为Servlet容器&#xff0c;有以下三种工作模式。 1&#xff09;独立的Servlet容器&#xff0c;由Java虚拟机进程来运行 Tomcat作为独立的Web服务器来单独运行&#xff0c;Servlet容器组件作为Web服务器中的一部分而存在。这是Tomcat的默认工作模式。…...

【项目实战】帮美女老师做一个点名小程序(Python tkinter)

前言 博主有一个非常漂亮的老师朋友&#x1f60d;。最近&#xff0c;她急需一个能够实现随机点名的小程序&#xff0c;而博主正好擅长这方面的技术&#x1f90f;。所以&#xff0c;今天博主决定为她制作一个专门用于点名的小程序&#x1f4aa;。 博主在美女老师面前吹完牛皮之…...

Elasticsearch 去重后求和

标题的要求可以用如下 SQL 表示 select sum(column2) from (select distinct(column1),column2 from table)t 要如何用 DSL 实现呢&#xff0c;先准备下索引和数据 PUT test_index {"mappings": {"properties": {"column1": {"type"…...

考研数学——高数:函数与极限(3)

函数的连续性与间断点 函数的连续性 左连续 右连续 区间上的连续性 在xo处连续 函数的间断点 第一类间断点(左右极限都存在) 可去间断点: f(xo-0)= f(xo+0) 跳跃间断点: f(xo-0)≠ f(xo+0) 第二类间断点(震荡间断点、无穷间断点)...

LeetCode49 字母异位词分组

LeetCode49 字母异位词分组 在这篇博客中&#xff0c;我们将探讨 LeetCode 上的一道经典算法问题&#xff1a;字母异位词分组。这个问题要求将给定的字符串数组中的字母异位词组合在一起&#xff0c;并以任意顺序返回结果列表。 问题描述 给定一个字符串数组 strs&#xff0…...

【Python】Windows本地映射远程Linux服务器上的端口(解决jupyter notebook无法启动问题)

创作日志&#xff1a; 学习深度学习不想在本地破电脑上再安装各种软件&#xff0c;我就用实验室的服务器配置环境&#xff0c;启动jupyter notebook时脑子又瓦特了&#xff0c;在自己Windows电脑上打开服务器提供的网址&#xff0c;那肯定打不开啊&#xff0c;以前在其它电脑上…...

C++面试:用户态和内核态的基本概念、区别

目录 一、基本概念 概念&#xff1a; 区别&#xff1a; 二、Windows示例 基础介绍 用户态到内核态的切换过程&#xff1a; 程序实例 三、Linux示例 特权级别&#xff1a; 用户态到内核态的切换过程&#xff1a; 调度和中断处理&#xff1a; 程序实例 总结 在操作系…...

C++中string流知识详解和示例

一、概览与类体系 C 提供三种基于内存字符串的流&#xff0c;定义在 <sstream> 中&#xff1a; std::istringstream&#xff1a;输入流&#xff0c;从已有字符串中读取并解析。std::ostringstream&#xff1a;输出流&#xff0c;向内部缓冲区写入内容&#xff0c;最终取…...

【HTTP三个基础问题】

面试官您好&#xff01;HTTP是超文本传输协议&#xff0c;是互联网上客户端和服务器之间传输超文本数据&#xff08;比如文字、图片、音频、视频等&#xff09;的核心协议&#xff0c;当前互联网应用最广泛的版本是HTTP1.1&#xff0c;它基于经典的C/S模型&#xff0c;也就是客…...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比

目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec&#xff1f; IPsec VPN 5.1 IPsec传输模式&#xff08;Transport Mode&#xff09; 5.2 IPsec隧道模式&#xff08;Tunne…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

使用Matplotlib创建炫酷的3D散点图:数据可视化的新维度

文章目录 基础实现代码代码解析进阶技巧1. 自定义点的大小和颜色2. 添加图例和样式美化3. 真实数据应用示例实用技巧与注意事项完整示例(带样式)应用场景在数据科学和可视化领域,三维图形能为我们提供更丰富的数据洞察。本文将手把手教你如何使用Python的Matplotlib库创建引…...

CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)

漏洞概览 漏洞名称&#xff1a;Apache Flink REST API 任意文件读取漏洞CVE编号&#xff1a;CVE-2020-17519CVSS评分&#xff1a;7.5影响版本&#xff1a;Apache Flink 1.11.0、1.11.1、1.11.2修复版本&#xff1a;≥ 1.11.3 或 ≥ 1.12.0漏洞类型&#xff1a;路径遍历&#x…...

【无标题】路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论

路径问题的革命性重构&#xff1a;基于二维拓扑收缩色动力学模型的零点隧穿理论 一、传统路径模型的根本缺陷 在经典正方形路径问题中&#xff08;图1&#xff09;&#xff1a; mermaid graph LR A((A)) --- B((B)) B --- C((C)) C --- D((D)) D --- A A -.- C[无直接路径] B -…...

淘宝扭蛋机小程序系统开发:打造互动性强的购物平台

淘宝扭蛋机小程序系统的开发&#xff0c;旨在打造一个互动性强的购物平台&#xff0c;让用户在购物的同时&#xff0c;能够享受到更多的乐趣和惊喜。 淘宝扭蛋机小程序系统拥有丰富的互动功能。用户可以通过虚拟摇杆操作扭蛋机&#xff0c;实现旋转、抽拉等动作&#xff0c;增…...

在树莓派上添加音频输入设备的几种方法

在树莓派上添加音频输入设备可以通过以下步骤完成&#xff0c;具体方法取决于设备类型&#xff08;如USB麦克风、3.5mm接口麦克风或HDMI音频输入&#xff09;。以下是详细指南&#xff1a; 1. 连接音频输入设备 USB麦克风/声卡&#xff1a;直接插入树莓派的USB接口。3.5mm麦克…...