双链表与双循环链表的C语言实现

news/2025/2/26 19:04:47/文章来源:https://www.cnblogs.com/GJ504b/p/18739378

双链表与双循环链表的C语言实现

目录
  • 双链表与双循环链表的C语言实现
    • 双链表的增删查改
    • 双循环链表的增删查改

双链表的增删查改

/*双链表*/#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node* pre;struct Node* next;
}Node;Node* initList(){Node* node = (Node*)malloc(sizeof(Node));node -> data = 0;node -> pre = NULL;node -> next = NULL;return node;
}void headInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));node -> data = data;if(L -> next){				node -> next = L -> next;node -> pre = L;L -> next = node;}else{node -> next = L -> next;node -> pre = L;L -> next = node;L -> next -> pre = node;//就是原链表第二个的pre指向新插来的node,如果原来为空,这个就不需要了}L -> data ++;
}void printList(Node*L){Node* node = (Node*)malloc(sizeof(Node));node = L -> next;while(node){printf("%d\n",node -> data);node = node -> next;}printf("\n");
}void tailInsert(Node* L, int data){Node* tail = L;Node* node = (Node*)malloc(sizeof(Node));node -> data = data;while(tail -> next){tail = tail -> next;}node -> next = tail -> next;tail -> next = node;node -> pre = tail;L -> data ++;
}void deleList(Node* L, int data){Node* node = L -> next;int flag = 0;while(node){if(node -> data == data){node -> pre -> next = node -> next;if(node -> next){node -> next -> pre = node -> pre;//如果被删的是最后一个就不存在下一个了,所以需要判断}L -> data --;free(node);printf("已经把%d从双链表里删除\n",data);flag = 1;}node = node -> next;}if(!flag){printf("没有在双链表里找到%d\n",data);}
}void findList(Node*L, int data){int flag = 0;Node* node = L -> next;while(node){if(node -> data == data){printf("已经在双链表里找到%d\n",data);flag = 1;}node = node -> next;}if(!flag){printf("没有在双链表里找到%d\n",data);}
}void changeList(Node*L, int data,int newdata){int flag = 0;Node* node = L -> next;while(node){if(node -> data == data){printf("已经把双链表里的%d改成%d\n",data,newdata);flag = 1;}node = node -> next;}if(!flag){printf("没有在双链表里找到%d,无法改成%d\n",data,newdata);}
}int main (){Node* L = initList();headInsert(L,22);headInsert(L,24);headInsert(L,22);headInsert(L,22);tailInsert(L,33);tailInsert(L,34);tailInsert(L,33);tailInsert(L,36);printList(L);deleList(L,33);deleList(L,13);
//	findList(L,24);findList(L,240);changeList(L,24,11);changeList(L,10,34);printList(L);return 0;
}

alt text

/*双链表 复习*/#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node* pre;struct Node* next;
}Node;Node* initList(){Node* node = (Node*)malloc(sizeof(Node));node -> data = 0;node -> pre = NULL;node -> next = NULL;return node;
}void headInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));node -> data = data;node -> next = L -> next;node -> pre = L;L -> next = node;if(!(L -> next)){L -> next -> pre = node;}L -> data ++;
}void printList(Node* L){Node* node = L -> next;while(node){printf("%d\n",node -> data);node = node -> next;}printf("\n");
}void tailInsert(Node* L, int data){Node* tail = L;Node* node = (Node*)malloc(sizeof(Node));node -> data = data;while(tail -> next){tail = tail -> next;}node -> pre = tail;node -> next = tail -> next;tail -> next = node;
}void deleList(Node* L, int data){Node* node = L -> next;int flag = 0;while(node){if(node -> next == data){node -> pre -> next = node -> next;if(node -> next){node ->next -> pre = node -> pre;}free(node);L -> data --;printf("已经把%d从双链表删除\n",data);flag = 1;}node = node -> next;}if(!flag){printf("无法删除,没有找到%d\n",data);}
}void findList(Node* L, int data){int flag = 0;Node* node = L -> next;while(node){if(node -> data == data){printf("已经在上联表里找到%d\n",data);flag = 1;}node = node -> next;}if(!flag){printf("未在双链表找到%d\n",data);}
}void changeList(Node* L, int data, int newdata){int flag = 0;Node* node = L -> next;while(node){if(node -> data == data){node -> data = newdata;printf("已经将双联表里的%d更新为%d\n",data,newdata);flag = 1;}node = node -> next;}if(!flag){printf("未在双链表找到%d,无法更新为%d\n",data,newdata);}
}int main(){Node* L = initList();headInsert(L,4);headInsert(L,3);headInsert(L,2);headInsert(L,1);tailInsert(L,5);tailInsert(L,6);tailInsert(L,7);tailInsert(L,8);printList(L);deleList(L,12);deleList(L,2);findList(L,1);findList(L,7);changeList(L,4,40);changeList(L,9,22);printList(L);return 0;
}

alt text

双循环链表的增删查改

alt text

void headInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));node -> data = data;node -> next = L -> next;node -> pre = L;L -> next = node;//先改变头节点的next,后面就变成 node的pre指向node,显然不对L -> next -> pre = node;L -> data ++;
}

alt text

void headInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));node -> data = data;node -> next = L -> next;node -> pre = L;L -> next -> pre = node;//先改变头节点的pre指向!!!L -> next = node;L -> data ++;
}
/*双循环链表*/
#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node* pre;struct Node* next;
}Node;Node* initList(){Node* node = (Node*)malloc(sizeof(Node));node -> data = 0;node -> pre = node;node -> next = node;return node;
}void headInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));node -> data = data;node -> next = L -> next;node -> pre = L;L -> next -> pre = node;L -> next = node;L -> data ++;
}void tailInsert(Node* L, int data){Node* node = (Node*)malloc(sizeof(Node));Node* tail = L -> next;while(tail -> next != L){tail = tail -> next;}node -> data = data;node -> next = L;L -> pre = node;node -> pre = tail;tail -> next = node;L -> data ++;
}void printList(Node* L){Node* node = L -> next;while(node != L){printf("%d\n",node -> data);node = node -> next;}
}void deleList(Node* L, int data){Node* node = L -> next;int flag = 0;while(node != L){Node* nodenext = node -> next;//!!!if (node -> data == data){node -> next -> pre = node -> pre;node -> pre -> next = node -> next;flag = 1;free(node);			printf("已经把%d从双链表删除\n",data);L -> data --;}//		node = node->next;  // 此时 node 已被释放,访问 node->next 导致未定义行为node = nodenext;//!!!}if(!flag){printf("无法删除,没有找到%d\n",data);}
}void findList(Node* L, int data){int flag = 0;Node* node = L -> next;while(node != L){if(node -> data == data){printf("已经在双联表里找到%d\n",data);flag = 1;}node = node -> next;}if(!flag){printf("未在双链表找到%d\n",data);}
}void changeList(Node* L, int data, int newdata){int flag = 0;Node* node = L -> next;while(node != L){if(node -> data == data){node -> data = newdata;printf("已经将双联表里的%d更新为%d\n",data,newdata);flag = 1;}node = node -> next;}if(!flag){printf("未在双链表找到%d,无法更新为%d\n",data,newdata);}
}int main(){Node* L = initList();headInsert(L,4);headInsert(L,3);headInsert(L,2);headInsert(L,1);tailInsert(L,5);tailInsert(L,6);tailInsert(L,7);tailInsert(L,8);printList(L);deleList(L,12);deleList(L,2);findList(L,1);findList(L,7);changeList(L,4,40);changeList(L,9,22);printList(L);return 0;
}

alt text

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

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

相关文章

Redis十大数据类型及命令详解

Redis数据类型 1、redis字符串(String)string是redis最基本的类型,一个key对应一个value string类型是二进制安全的,意思是redis的string可以包含任何数据。例如说是jpg图片或者序列化对象 一个redis中字符串value最多可以是512M应用场景: 缓存:存储用户会话、页面内容(…

第6章-控件

控件,继承自 System.Windows.Contorl 类 控件分类内容控件:Label/Button/ToolTip/ScrollViewer 带有标题的内容控件:TabItem/GroupBox/Expander 文本控件:Textbox/PasswordBox/RichTextBox 列表控件:ListBox/ComboBox 基于范围的控件:Slider/ProgressBar 日期控件:Calen…

第5章-路由事件

Handler: 处理器 Preview: 预览、这指隧道 Raise: 引发 Bubble: 冒泡 Handled: 已处理理解路由事件 事件路由允许源自某个元素的事件由另一个元素引发。 定义、注册和封装路由事件 public partial class Window1 : Window {// 定义路由事件,必须是 static readonly// 类型 Rou…

mysql 数据目录

前言 简单介绍一下mysql的数据目录。 正文 对于mysql,我们关心的一般是数据部分,那么关心的就是数据目录。 我们查看一下数据目录的位置。 SHOW VARIABLES LIKE datadir看一下数据目录的位置:再对比一下我们数据库的信息:好像除了这个information_schema 好像都有一个目录。 …

三句话生成 P5.js 粒子特效代码,人人都可以做交互式数字艺术

前几天在上海西岸艺术中心的全球开发者先锋大会GDC2025,通义灵码的老朋友-@同济子豪兄受邀参会,并带领大家观看了本届大会两大重磅看点:具身智能人形机器人和 AI 程序员编程写代码。前几天在上海西岸艺术中心的全球开发者先锋大会GDC2025,通义灵码的老朋友-@同济子豪兄受邀…

使用JAVA调用asmx服务,“http://tempuri.org/”有什么作用?

原文链接:https://bbs.csdn.net/topics/392507481 这个是域名 http://tempuri.org/ 是默认的命名空间.如果是web直接引用不需要管,但有时候别人发布改了这个命名,你动态引用就需要改成跟他一样的了.一般情况下有些人发布没改这个,有些注重网站安全的就修改了这个,给你个照片看…

为什么去IOE化的背景下,还有必要学Oracle

很多人都知道有“去IOE化”这个口号,但事实上,有多少人知道是哪一年提出的这个口号吗?有多少人知道去的哪个IOE吗?现在越来越多的国产数据库出现,还有必要学Oracle、考OCP认证吗? 去IOE化 “去IOE化”这个口号早在2008、2009的时候就提出来了,原因是互联网发展需要、成本…

08 梯度消失与梯度爆炸问题

由反向传播原理可知,梯度的计算遵循链式法则。由于网络层数不断加深,梯度的连乘效应可能会导致梯度呈指数形式衰减,又或以指数形式增加。 前者叫做梯度消失,梯度消失导致网络中的早期层几乎不更新,使得网络难以学习到输入数据的有效特征。可能导致网络权重更新非常缓慢,使…

GAMES101 作业三

重要知识点一 布林冯反射模型 漫反射+高光+环境光重要知识点二 通过作业也对空间中的坐标变换认识更清晰了一点,在摄像空间中进行变换是不对的,需要从原来的三维空间进行变换才对,所以会有一个矫正系数 重要知识点三 在计算光线时,要注意计算向量和单位化 不了解的 对于后两…

写一个简单的hexo-tag-plugin:quote

前置教程 [Akilarの糖果屋 - Akilar.top](https://akilar.top/posts/e2bf861f/) 为啥想写一个quote的标签外挂 我最近在写博客的时候,发现好多时候原生的Hexo标签不是很好用,效果如下。 {% tabs Hexo Block Quote, -1 %}没有提供参数,则只输出普通的 blockquote{% blockquot…

搭建DeepSeek-R1平台

前言 大家用到 DeepSeek-R1 时应该会经常出现下面的情况。但凡多问两个问题,不但缓慢,而且容易出现服务器繁忙的问题:今天教大家一种通过API部署的方式,可以体验满血版的DeepSeek-R1,不仅回答快速,而且不会出现服务器繁忙的情况。 注册账号 首先大家要通过下面的方式,先…

No.16 CSS--背景属性

一、CSS常见的背景属性 background-color: aqua; 设置背景颜色background-image: none; 设置背景图片background-position: 0%; 设置背景图片位置background-repeat: no-repeat; 设置背景图片如何重复填充background-size: 0%; …