C语言双向链表如何实现向指定索引位置插入元素

核心代码:

int insertDoubleLinkedList(DoubleLinkedList *list, int index, int value) {if (list == NULL || list->head == NULL || list->tail == NULL) {return -1;}// 当一个元素都没有的时候,或者index=size的时候,是支持插入的if (index < 0 || index > list->size) {return -1;}// 创建节点DoubleLinkedListNode *newNode = newDoubleLinkedListNode(value);// 元素个数增加list->size++;// 插入到头部if (index == 0) {DoubleLinkedListNode *oldNode = list->head->next;list->head->next = newNode;newNode->prev = list->head;newNode->next = oldNode;oldNode->prev = newNode;return 0;}// 插入到尾部if (index == list->size) {DoubleLinkedListNode *oldNode = list->tail->prev;oldNode->next = newNode;newNode->prev = oldNode;newNode->next = list->tail;list->tail->prev = newNode;return list->size;}// 插入到中间int i = 0;DoubleLinkedListNode *current = list->head->next;while (i < index) {i++;current = current->next;}// 此时,current就是索引位置的节点,将该节点往后移动DoubleLinkedListNode *oldPrev = current->prev;oldPrev->next = newNode;newNode->prev = oldPrev;newNode->next = current;current->prev = newNode;// 返回return index;
}

double_linked_list.h

#ifndef ZDPC_ALGORITHM_DEV_DOUBLE_LINKED_LIST_H
#define ZDPC_ALGORITHM_DEV_DOUBLE_LINKED_LIST_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 双向链表的节点
typedef struct doubleLinkedListNode {int data;struct doubleLinkedListNode *next; // 下一个节点struct doubleLinkedListNode *prev; // 上一个节点
} DoubleLinkedListNode;// 双向链表
typedef struct doubleLinkedList {DoubleLinkedListNode *head; // 头节点DoubleLinkedListNode *tail; // 尾节点int size; // 节点的个数
} DoubleLinkedList;extern DoubleLinkedList *newDoubleLinkedList(); // 创建双向链表
extern void freeDoubleLinkedList(DoubleLinkedList *list); // 释放双向链表内存
extern int isEmptyDoubleLinkedList(DoubleLinkedList *list); // 判断是否为空链表
extern int sizeDoubleLinkedList(DoubleLinkedList *list); // 获取链表的元素个数
extern void printDoubleLinkedList(DoubleLinkedList *list); // 打印链表的内容
extern void appendDoubleLinkedList(DoubleLinkedList *list, int value); // 将元素添加到链表末尾
extern int removeDoubleLinkedList(DoubleLinkedList *list, int value); // 从链表中移除元素,成功返回其索引,失败返回-1
extern int insertDoubleLinkedList(DoubleLinkedList *list, int index, int value); // 将数据插入到链表指定索引位置,成功返回其索引,失败返回-1#endif //ZDPC_ALGORITHM_DEV_DOUBLE_LINKED_LIST_H

double_linked_list.c

#include "double_linked_list.h"// 创建头节点
DoubleLinkedListNode *newDoubleLinkedListNode(int value) {// 申请节点的空间DoubleLinkedListNode *node = malloc(sizeof(DoubleLinkedListNode));// 初始化node->next = NULL;node->prev = NULL;node->data = value;// 返回return node;
}// 创建双向链表
DoubleLinkedList *newDoubleLinkedList() {// 申请链表的内存DoubleLinkedList *list = malloc(sizeof(DoubleLinkedList));// 头节点DoubleLinkedListNode *head = newDoubleLinkedListNode(0);// 尾结点DoubleLinkedListNode *tail = newDoubleLinkedListNode(0);// 初始化list->head = head;list->tail = tail;list->size = 0;// 关系list->head->next = list->tail;list->tail->prev = list->head;// 返回return list;
}// 释放双向链表内存
void freeDoubleLinkedList(DoubleLinkedList *list) {// 释放节点内存DoubleLinkedListNode *node = list->head->next;while (node->next != list->tail) {DoubleLinkedListNode *next = node->next;free(node);node = next;}// 释放首尾节点的内存free(list->head);free(list->tail);// 释放链表内存free(list);
}// 判断是否为空链表
int isEmptyDoubleLinkedList(DoubleLinkedList *list) {return list != NULL && list->head->next == list->tail;
}// 获取链表的元素个数
int sizeDoubleLinkedList(DoubleLinkedList *list) {return list->size;
}// 打印链表的内容
void printDoubleLinkedList(DoubleLinkedList *list) {if (list == NULL) {return;}if (list->head == NULL || list->head->next == NULL) {return;}DoubleLinkedListNode *current = list->head->next;while (current != list->tail) {printf("%d ", current->data);current = current->next;}printf("\n");
}// 将元素添加到链表末尾
void appendDoubleLinkedList(DoubleLinkedList *list, int value) {insertDoubleLinkedList(list, list->size, value);
}// 弃用旧的追加方法
void appendDoubleLinkedList2(DoubleLinkedList *list, int value) {if (list == NULL || list->head == NULL || list->tail == NULL) {return;}// 构造新节点DoubleLinkedListNode *node = newDoubleLinkedListNode(value);// 插入到末尾DoubleLinkedListNode *oldPrev = list->tail->prev;oldPrev->next = node;node->prev = oldPrev;node->next = list->tail;list->tail->prev = node;// 元素个数增加list->size++;
}// 从链表中移除元素
int removeDoubleLinkedList(DoubleLinkedList *list, int value) {int index = -1;if (list == NULL || list->head == NULL || list->tail == NULL || list->size <= 0) {return index;}// 查找对应的元素DoubleLinkedListNode *current = list->head->next;while (current != list->tail) {index++;if (current->data == value) {break;}current = current->next;}// 如果找到了,则删除if (index >= 0 && index < list->size) {DoubleLinkedListNode *oldPrev = current->prev;DoubleLinkedListNode *oldNext = current->next;oldPrev->next = oldNext;oldNext->prev = oldPrev;free(current); // 记得释放节点的内存}// 元素个数减少list->size--;// 返回return index;
}// 将数据插入到链表指定索引位置
int insertDoubleLinkedList(DoubleLinkedList *list, int index, int value) {if (list == NULL || list->head == NULL || list->tail == NULL) {return -1;}// 当一个元素都没有的时候,或者index=size的时候,是支持插入的if (index < 0 || index > list->size) {return -1;}// 创建节点DoubleLinkedListNode *newNode = newDoubleLinkedListNode(value);// 元素个数增加list->size++;// 插入到头部if (index == 0) {DoubleLinkedListNode *oldNode = list->head->next;list->head->next = newNode;newNode->prev = list->head;newNode->next = oldNode;oldNode->prev = newNode;return 0;}// 插入到尾部if (index == list->size) {DoubleLinkedListNode *oldNode = list->tail->prev;oldNode->next = newNode;newNode->prev = oldNode;newNode->next = list->tail;list->tail->prev = newNode;return list->size;}// 插入到中间int i = 0;DoubleLinkedListNode *current = list->head->next;while (i < index) {i++;current = current->next;}// 此时,current就是索引位置的节点,将该节点往后移动DoubleLinkedListNode *oldPrev = current->prev;oldPrev->next = newNode;newNode->prev = oldPrev;newNode->next = current;current->prev = newNode;// 返回return index;
}

main.c

#include "double_linked_list.h"int main(void) {// 创建链表DoubleLinkedList *list = newDoubleLinkedList();// 添加数据appendDoubleLinkedList(list, 11);appendDoubleLinkedList(list, 22);appendDoubleLinkedList(list, 33);printDoubleLinkedList(list);// 插入到前面insertDoubleLinkedList(list,0,333);printDoubleLinkedList(list);// 插入到后面insertDoubleLinkedList(list,list->size,333);printDoubleLinkedList(list);// 插入到中间insertDoubleLinkedList(list,2,333);printDoubleLinkedList(list);printf("元素个数:%d\n",list->size);// 释放链表freeDoubleLinkedList(list);return 0;
}

输出:

11 22 33
333 11 22 33
333 11 22 33 333
333 11 333 22 33 333
元素个数:6

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

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

相关文章

前端html中iframe的基本使用

以下是一个比较复杂的 <iframe> 示例&#xff0c;展示了如何加载外部页面、控制样式和与 <iframe> 中加载的文档进行通信&#xff1a; <!DOCTYPE html> <html> <head><style>.iframe-container {position: relative;width: 100%;height: …

【Python】常用数据结构

1、熟悉字典和列表 2、使用条件判断语句 3、list列表中计算 1、从键盘输人一个正整数列表,以-1结束,分别计算列表中奇数和偶数的和。 &#xff08;1&#xff09;源代码&#xff1a; # 初始化奇数和偶数的和为0 odd_sum 0 even_sum 0 #输入 while True:num int(input(&qu…

Android学习系列目录

Android学习1 -- 从嵌入式Linux到嵌入式Android-CSDN博客 Android学习2 -- SDK 1&#xff08;概览&#xff09;-CSDN博客 Android学习3 -- SDK2 &#xff08;实操三个小目标&#xff09;-CSDN博客 Android学习4 -- ADB的使用-CSDN博客 Android学习5 -- HAL-1 概述-CSDN博客…

ubuntu下安装配置python3.11

方案1 添加仓库&#xff1a; $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt update $ sudo apt install python3.11然后查看有多少个python版本已经安装了&#xff1a; ls -l /usr/bin/python*python2.7,python 3.8 ,python 3.11. 然后&#xff0c;设置系统默认…

智能车入门——‘教程引导’ <新手从零做车>

目录 前言 本系列文章是为了帮助第一次接触智能车或者学校没有传承&#xff0c;不知道如何上手做智能车的同学。 通过阅读完整个系列&#xff0c;你应该能够制作一辆正常参赛的智能车。 我写这一系列博客的初衷主要是为了方便新手快速入门智能车。 如果追求高级算法以及提速&a…

【Jenkins】持续集成与交付 (七):Gitlab添加组、创建用户、创建项目和源码上传到Gitlab仓库

🟣【Jenkins】持续集成与交付 (七):Gitlab添加组、创建用户、创建项目和源码上传到Gitlab仓库 1、创建组2、创建用户3、将用户添加到组中4、在用户组中创建项目5、源码上传到Gitlab仓库5.1 初始化版本控制5.2 将文件添加到暂存区5.3 提交代码到本地仓库5.4 推送代码到 Git…

【如何成功安装 Python 软件包 weditor】

如何成功安装 Python 软件包 weditor 在进行软件开发或者使用 Python 进行编程时&#xff0c;经常会遇到需要安装第三方软件包的情况。然而&#xff0c;有时候安装过程并不顺利&#xff0c;可能会遇到各种问题。在本文中&#xff0c;我将分享我解决安装 Python 软件包 weditor…

Q1季度家用健身器械行业线上市场销售数据分析

自疫情开始&#xff0c;全民健身的浪潮就持续至今。然而&#xff0c;水能载舟亦能覆舟&#xff0c;一边是不断释放的健身需求&#xff0c;另一边却是无数健身房的闭店潮。 越来越多人倾向于选择家用健身器械来运动或是直接选择无器械的健身运动&#xff0c;比如各类健身操。而…

AngularJS 的生命周期和基础语法

AngularJS 的生命周期和基础语法 文章目录 AngularJS 的生命周期和基础语法1. 使用步骤2. 生命周期钩子函数3. 点击事件4. if 语句1. if 形式2. if else 形式 5. for 语句6. switch 语句7. 双向数据绑定 1. 使用步骤 // 1. 要使用哪个钩子函数&#xff0c;就先引入 import { O…

Windows下载MingGW

因为要配置vscode的c/c环境&#xff0c;需要下载一个编译器&#xff0c;gcc官方推荐开源的MingGW-W64&#xff0c;看了几个下载方法&#xff0c;决定用最简单的离线安装。 niXman/mingw-builds-binaries/releases 32位的操作系统&#xff1a;i686&#xff0c;64位的操作系统&a…

富格林:可信方略杜绝交易虚假

富格林指出&#xff0c;黄金市场是一个极具诱惑力的市场&#xff0c;它是在一个大家共同认可的游戏规则下&#xff0c;凭借自己可信的决策、判断来进行交易的一种投资市场。黄金市场不断有新手投资者的加入&#xff0c;但是要真正在该市场上获利&#xff0c;杜绝虚假套路是一个…

linux的常见命令

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;Linux ⛺️稳中求进&#xff0c;晒太阳 Linux中检查进程是否存在&#xff1a; ps -ef | grep [进程名或进程ID] pgrep -f [进程名|进程ID] pidof [进程名] Linux中检查某个端口是否被…

外包干了3天,技术就明显退步了。。。。。

先说一下自己的情况&#xff0c;本科生&#xff0c;19年通过校招进入广州某软件公司&#xff0c;干了接近4年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能测试…

rabbitMq 0 到1

前言 工作中MQ的使用场景是数不胜数&#xff0c;每个公司的技术选型又不太一样&#xff0c;用的哪个MQ&#xff0c;我们必须要先玩起来&#xff0c;RabbitMQ在windows安装遇到很多问题&#xff0c;博客也是五花八门&#xff0c;算了还是自己搞吧&#xff0c;记录一下&#xff…

机器视觉系统-同轴光源大小选择技巧

同轴光源多用于检测光滑平面产品上的缺陷&#xff0c;同样利用上述的方法计算得出光源尺寸。 实际上&#xff0c;同轴光源可理解为没有孔的开孔面光&#xff0c;因此可等效为发光面相等的面光源&#xff0c;如下图&#xff1a; 如图所示&#xff0c;同轴光源的效果与开孔面光的…

004 springCloudAlibaba Gateway

文章目录 gatewayServerGatewayServerApplication.javaServletInitializer.javaapplication.yamlpom.xml orderServerOrderController.javaProductClient.javaOrderServerApplication.javaServletInitializer.javaapplication.yamlpom.xml productServerProductController.java…

数论7-同余

点个关注吧&#xff0c;谢谢&#xff01; 后续将继续更新数论 一、定义 同余的概念很简单&#xff1a;给定三个整数 a , b , n a,b,n a,b,n&#xff0c;如果 n ∣ ( a − b ) n|(a-b) n∣(a−b)&#xff0c;那么 a a a模 n n n同余 b b b。记作 a b ( m o d n ) ab~(mod n) ab…

karpathy make more -- 4

1 Introduction 这个部分要完成一个网络的模块化&#xff0c;然后实现一个新的网络结构。 2 使用torch的模块化功能 2.1 模块化 将输入的字符长度变成8&#xff0c;并将之前的代码模块化 # Near copy paste of the layers we have developed in Part 3# -----------------…

PID控制技术有哪些?

PID&#xff08;比例-积分-微分&#xff09;控制是一种广泛使用的反馈控制技术&#xff0c;它通过调整控制系统的输入来使输出达到期望的设置值。PID控制器的三个组成部分—比例&#xff08;P&#xff09;、积分&#xff08;I&#xff09;和微分&#xff08;D&#xff09;—各自…

8. Django 表单与模型

8. 表单与模型 表单是搜集用户数据信息的各种表单元素的集合, 其作用是实现网页上的数据交互, 比如用户在网站输入数据信息, 然后提交到网站服务器端进行处理(如数据录入和用户登录注册等).网页表单是Web开发的一项基本功能, Django的表单功能由Form类实现, 主要分为两种: dj…