单链表的基本操作

链表

文章目录

  • 链表
    • 创建链表
      • 单链表
        • 实现一:
        • 实现二:
        • 错例
    • 循环链表
      • 单独创建
      • 逐节点创建
      • 约瑟夫环问题
    • 删除节点
      • 实现方式一:
      • 实现方式二:
      • 删除节点并建立新链表
    • 逆置链表
      • 实现:
    • 链表排序
      • 实现一:
      • 实现二:
      • 实现三:
    • 链表查询(跳表)

struct List
{int data;struct List* next;
}

创建链表

单链表

实现一:
struct List* listCreate()
{int data;struct List* head = NULL;struct List* pre = NULL;struct List* current = NULL;while(scanf("%d",&data) && data != -1){current = (struct List*)malloc(sizeof(struct List));if(head == NULL)head = current;elsepre->next = current;current->next = NULL;current->data = data;pre = current;}return head;
}
实现二:
struct List* listCreate()
{struct List* head;struct List* current;head = (struct List*)malloc(sizeof(struct List));current = head;while (scanf("%d", &data) && data != -1){current->next = (struct List*)malloc(sizeof(struct List));current = current->next;current->data =  current;current->next = NULL;}return head;
}
错例
struct List* listCreate()
{int data;;struct List* current = NULL;struct List* head = current;while (scanf("%d", &data) && data != -1){current = (struct List*)malloc(sizeof(struct List));if (head == NULL)head = current;current->data = data;current = current->next;}return head;
}

在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配

循环链表

https://pic2.zhimg.com/v2-6d8f7d01b3b74ce646e73b76ee414c40_r.jpg

单独创建

struct List* circle_listCreate()
{int data;struct List* head = NULL;struct List* pre = NULL;struct List* current = NULL;while(scanf("%d",&data) && data != -1){current = (struct List*)malloc(sizeof(struct List));if(head == NULL)head = current;elsepre->next = current;current->next = head;current->data = data;pre = current;}return head;
}

逐节点创建

void Append(struct List** L,int data)
{struct List* head = *L;struct List* newNode = NULL;if((*L) == NULL){(*L) = (struct List*)malloc(sizeof(struct List));(*L)->data = data;head = (*L);(*L)->next = head;}else{while ((*L)->next != head){(*L) = (*L)->next;}newNode = (struct List*)malloc(sizeof(struct List));newNode->data = data;(*L)->next = newNode;newNode->next = head;*L = head;}
}

约瑟夫环问题

void Append(struct List** L,int data)
{struct List* head = *L;struct List* newNode = NULL;if((*L) == NULL){(*L) = (struct List*)malloc(sizeof(struct List));(*L)->data = data;head = (*L);(*L)->next = head;}else{while ((*L)->next != head){(*L) = (*L)->next;}newNode = (struct List*)malloc(sizeof(struct List));newNode->data = data;(*L)->next = newNode;newNode->next = head;*L = head;}
}
void Display(struct List* L,int num)
{struct List* head = L;struct List* pre = NULL;struct List* kill = NULL;int nodeNum = 0;while (L->next != head){nodeNum++;L = L->next;}pre = L;L = L->next;nodeNum++;while (nodeNUm){if (nodeNum == 1){printf("%d",L->data);free(L);return;}for (int i=1; i < m; i++){pre = L;L = L->next;}printf("%d ", L->data);kill = L;L = L->next;free(kill);nodeNum--;}
}

删除节点

实现方式一:

struct list* listDelete(struct list* L,int data)
{struct list* pre = L;struct list* head = L;struct list* kill;while(head != NULL && head->data == m){kill = head;head = head->next;free(kill);}if(head == NULL)return head;pre = head;kill = head->next;while(kill!=NULL){if(kill->data == m){pre->next = kill->next;free(kill);kill = pre->next;}else{pre = kill;kill = kill->next;}}return head;
}

实现方式二:

struct list* listDelete(struct list** L,int data)
{struct list* head = (*L), * pre = (*L);struct list* newL = *L;struct list* kill = NULL;while (*L != NULL){if((*L)->data == data){if((*L) == newL)newL == newL->next;elsepre->next = (*L)->next;kill = (*L);(*L) = (*L)->next;free(kill);}else{pre = (*L);(*L) = (*L)->next;}}*L = newL;return head;
}

删除节点并建立新链表

struct list* list_Delete_Create(struct list** L) //数据为奇数存为新链表
{struct list* newhead = NULL, * newcurrent = NULL, * newpre = NULL;struct list* newL = *L;struct list* kill = NULL;struct list* pre = *L;while (*L){if((*L)->data%2 == 1){newcurrent = (struct list*)malloc(sizeof(struct list));if(newhead == NULL)newhead = newcurrent;elsenewpre->next = newcurrent;newcurrent->data = (*L)->data;newcurrent->next = NULL;newpre = newcurrent;if((*L) == newL)newL = newL->next;elsepre-next = (*L)->next;kill = (*L);(*L)=(*L)->next;free(kill);}else{pre = (*L);(*L) = (*L)->next;}}*L = newL;return newhead;
}

逆置链表

实现:

struct list* reverse(struct list* L)
{struct list* newhead = NULL, * current;while (L != NULL){current = (struct list*)malloc(sizeof(struct list));current->data = L->data;L = L->next;current->next = newhead;newhead = current;}free(L);return newhead;
}

链表排序

实现一:

struct List* listSort(struct List** head, int n, int(*compare)(float a, float b)) {int i, j;struct List *temp;struct List *current;struct List *init = *head;for (i = 0; i < n; i++) {current = *head;temp = *head;for (j = 0; j < n - i - 1; j++) {if ((*compare)(current->data, current->next->data)) {if (current == *head) {*head = current->next;temp = current->next;current->next = temp->next;temp->next = current;init = *head;}else {temp->next = current->next;current->next = current->next->next;temp->next->next = current;temp = temp->next;}}else {temp = current;current = current->next;}}}return init;
}
int Descending(float a, float b) {return a > b;
}int Ascending(float a, float b) {return a < b;
}

实现二:

void listSort(struct List* head, int n, int(*compare)(float a, float b)) {int i, j;struct List *temp;struct List *current;for (i = 0; i < n; i++) {current = head;temp = head;for (j = 0; j < n - i - 1; j++) {if ((*compare)(current->data, current->next->data)) {if (current == head) {head = current->next;temp = current->next;current->next = temp->next;temp->next = current;}else {temp->next = current->next;current->next = current->next->next;temp->next->next = current;temp = temp->next;}}else {temp = current;current = current->next;}}}
}

实现三:

//头节点置为空

void sortlist(struct List* head, int n, int(*compare)(float a, float b))
{int i, j;struct List *temp;struct List *current;for (i = 0; i < n; i++) {temp = head;current = head->next;for (j = 0; j < n - i - 1; j++) {if ((*compare)(current->data, current->next->data)) {temp->next = current->next;current->next = current->next->next;temp->next->next = current;temp = temp->next;}else {temp = current;current = current->next;}}}
}

链表查询(跳表)

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

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

相关文章

Linux 系统 docker搭建LNMP环境

1、安装nginx docker pull nginx (默认安装的是最新版本) 2、运行nginx docker run --name nginx -p 80:80 -d nginx:latest 备注&#xff1a;--name nginx 表示容器名为 nginx -d 表示后台运行 -p 80:80 表示把本地80端口绑定到Nginx服务端的 80端口 nginx:lates…

mac-git上传至github(ssh版本,个人tokens总出错)

第一步 git clone https://github.com/用户名/项目名.git 第二步 cd 项目名 第三步 将本地的文件移动到项目下 第四步 git add . 第五步 git commit -m "添加****文件夹" 第六步 git push origin main 报错&#xff1a; 采用ssh验证 本地文件链接公钥 …

【办公类-21-11】 20240327三级育婴师 多个二级文件夹的docx合并成docx有页码,转PDF

背景展示&#xff1a;有页码的操作题 背景需求&#xff1a; 实操课终于全部结束了&#xff0c;把考试内容&#xff08;docx&#xff09;都写好了 【办公类-21-10】三级育婴师 视频转文字docx&#xff08;等线小五单倍行距&#xff09;&#xff0c;批量改成“宋体小四、1.5倍行…

【力扣hot100】1. 两数之和 49.字母异位词分组 128. 最长连续序列

目录 1. 两数之和题目描述做题思路参考代码 49.字母异位词分组题目描述做题思路参考代码 128. 最长连续序列题目描述做题思路参考代码 1. 两数之和 题目描述 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数…

深度学习编译工具链中的核心——图优化。

图优化 图优化的概念&#xff1a; 深度神经网络模型可以看做由多个算子连接而成的有向无环图&#xff0c;图中每个算子代表一类操作&#xff08;如乘法、卷积&#xff09;&#xff0c;连接各个算子的边表示数据流动。在部署深度神经网络的过程中&#xff0c;为了适应硬件平台…

[AIGC] 对比MySQL全文索引,RedisSearch,和Elasticsearch的详细区别

全文搜索是数据库和搜索引擎的重要功能。这个功能能在一个或多个列中查找用户查询的文本&#xff0c;这对诸如电子商务网站和检索大量文本数据的应用是必需的。在这篇文章中&#xff0c;我们将详细对比三种主流全文搜索技术&#xff1a; MySQL全文索引&#xff0c;Redis的Redis…

2024年 导出环境依赖requirements.txt

2024年 导出环境依赖 一、前言 有时候需要导出环境依赖&#xff0c;遂记录一下这个短短的步骤 二、具体步骤 1、使用pip进行安装和管理环境 安装导出依赖的库pipreqs pip install pipreqs将环境依赖项导出到当前目录的requirements.txt文件&#xff0c;编码格式用utf-8 …

YoloV5改进策略:BackBone改进|ECA-Net:用于深度卷积神经网络的高效通道注意力

摘要 本文使用ECA-Net注意力机制加入到YoloV5中。我尝试了多种改进方法&#xff0c;并附上改进结果&#xff0c;方便大家了解改进后的效果&#xff0c;为论文改进提供思路。&#xff08;更新中。。。。&#xff09; 论文&#xff1a;《ECA-Net&#xff1a;用于深度卷积神经网…

解决方案:如何安装neo4j软件

文章目录 一、安装JDK二、安装neo4j 一、安装JDK 第一步先安装JDK&#xff0c;因为neo4j环境需要JDK&#xff0c;过程比较多&#xff0c;截图如下&#xff1a; 安装JDK网址 https://www.oracle.com/java/technologies/downloads winR&#xff0c;输入cmd&#xff0c;再输入j…

【计算机考研】408到底有多难?

你真以为大家是学不会408吗&#xff1f; 不是&#xff01;单纯是因为时间不够&#xff01;&#xff01;&#xff01; 再准确一些就是不会分配时间 408的知识其实并不难&#xff0c;要说想上130那确实有难度&#xff0c;但是100在时间充裕的情况下还是可以做到的 我本人是双…

基于springboot的房屋租赁管理系统+数据库+免费远程调试

项目介绍: 基于springboot的房屋租赁管理系统。Javaee项目&#xff0c;springboot项目&#xff0c;采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring SpringBoot JspMaven来实现。MyS…

力扣---网络延迟时间---迪杰斯特拉,弗洛伊德floyd

首先推荐博客&#xff1a;图论最短路径专题&#xff08;力扣743、5888&#xff09;_力扣 最短路径-CSDN博客 迪杰斯特拉算法&#xff1a; 太久没有做图论的题了&#xff0c;&#xff0c;临时抱佛脚。。 这道题可以转化为max{点x到点k的距离}。因为带权图&#xff08;权值为正…