双向链表简单实现及图示
————————————————————————————————————————————
双向链表
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
和单向链表相比有以下优势:
插入删除不需要移动元素外,可以原地插入删除
可以双向遍历
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
初始化+尾插法图示://head始终指向头结点,p指向尾节点,方便后续算法使用
删除单个图示:
实现代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef struct Node pNode; 5 struct Node 6 { 7 int data; 8 pNode *prev, *next; 9 }; 10 /* 初始化链表,尾插法 */ 11 pNode *InitList(pNode **head, int n) 12 { 13 pNode *p, *s; 14 (*head) = (pNode *)malloc(sizeof(pNode)); 15 if ((*head) == NULL) 16 exit(0); 17 (*head)->next = NULL;//head的prev和next均指向NULL 18 (*head)->prev = NULL; 19 p = (*head);//p指向head 20 int i; 21 for (i = 0; i < n; ++i) 22 { 23 s = (pNode *)malloc(sizeof(pNode)); 24 if (s == NULL) 25 exit(0); 26 printf("Input the value of the %dth node:", i + 1); 27 scanf("%d", &s->data); 28 s->next = NULL; 29 p->next = s; 30 s->prev = p; 31 p = s;//p指向尾节点 32 } 33 return p; 34 } 35 /* 遍历打印 */ 36 void PrintList(pNode *head) 37 { 38 pNode *p; 39 p = head->next; 40 if (head->next == NULL) 41 printf("the list is empty\n"); 42 while(p != NULL) 43 { 44 printf("%d ", p->data); 45 p = p->next; 46 } 47 printf("\n"); 48 } 49 /* 清空链表 */ 50 void DeleteList(pNode **head) 51 { 52 pNode *p; 53 while((*head)->next != NULL) 54 { 55 p = (*head); 56 p->next->prev = NULL; 57 (*head) = p->next; 58 free(p); 59 } 60 } 61 /* 查找链表内的某个值 */ 62 int SearchList(pNode *head) 63 { 64 int number; 65 printf("Values are about to be deleted:"); 66 scanf("%d", &number); 67 pNode *p; 68 p = head->next; 69 while(p != NULL) 70 { 71 if (p->data == number) 72 { 73 return number; 74 } 75 p = p->next; 76 } 77 return 0; 78 } 79 /* 删除链表中某个元素,令p的前驱节点和后驱节点相互指向即可,如果p是尾节点则直接将前驱节点指向NULL*/ 80 void DelNumqList(pNode **head, int n) 81 { 82 int i; 83 pNode *p; 84 p = (*head)->next; 85 for (i = 1; i < n; ++i) 86 p = p->next; 87 if(p->next == NULL) 88 { 89 p->prev->next = NULL; 90 free(p); 91 } 92 else 93 { 94 p->next->prev = p->prev; 95 p->prev->next = p->next; 96 free(p); 97 } 98 } 99 int main(int argc, char const *argv[]) 100 { 101 int n, element, flag; 102 pNode *head, *last; 103 /***************************************************************/ 104 printf("Please input the size of the list:"); 105 scanf("%d", &n); 106 last = InitList(&head, n);//初始化链表并赋值,返回尾节点last 107 printf("%d %d \n", head->next->data, last->data); //打印为第一个元素和最后一个元素 108 PrintList(head); 109 /***************************************************************/ 110 flag = SearchList(head); //搜索某个值并删除节点 111 if (flag > 0 && flag <= n) 112 { 113 DelNumqList(&head, flag); 114 PrintList(head); 115 } 116 else 117 printf("Element does not exist, cannot be deleted\n"); 118 /***************************************************************/ 119 DeleteList(&head);//清空列表 120 PrintList(head); 121 return 0; 122 }
双向链表简单实现及图示相关推荐
- c语言结点初始化,C语言双向链表简单实现及图示(初始化/插入节点/删除节点)...
-------------------------------------------- 双向链表 - - - - - - - - - - - - - - - - - - - - - - - - - ...
- python os.system(cls)没反应_这真的是全宇宙最简单的Python安装方式了
大家安装Python一般都是怎么安装的呢?每次有同学来问老师Python的安装方法时,老师都会教大家如何到Python的官网下载最新的Python安装包.但近来也有同学在反应,由于Python的官网在 ...
- java datatable用法_Java中实现DataTable工具类,并利用其实现简单分页控件。
具体工具类代码,请见我上一个博客. 一.工具类的使用 1.1 DataTable工具类的使用 1.1.1DataTable简单解析 顾名思义,DataTable其实就是一张虚拟数据表,用于存储由数据库 ...
- 双向链表(java语言实现)
public class HeroNode2 {public int no;public String name;public String nickname;public HeroNode2 nex ...
- KEYSIGHT示波器 MSO-X 3024A 简单操作说明
KEYSIGHT示波器 MSO-X 3024A 简单操作说明 如下图示为一些基本操作,同时配合屏幕显示,应该能够较为熟悉上手:
- java 双向链表循环_双向循环链表的Java版本实现
1.单项循环列表 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环.和单链表相比,循环单链表的长处是从链尾到链 ...
- 服务器线路维修工具,免工具拆装及拆装导航图示
2.免工具拆装 免工具拆装也是为了简化服务器维护管理的一种设计,免工具拆装设计目前在服务器上已经比较常见,像三大国际服务器厂商IBM.HP.Dell的大部分服务器产品均采用了免工具拆装设计. 戴尔 ...
- C++程序设计:原理与实践(进阶篇)15.4 链表
15.4 链表 下面让我们再回顾一下序列概念的图形表示: 将它与我们描绘vector内存结构的示意图相比较: 下标0本质上与迭代器v.begin()一样都指向同一个元素,并且下标v.size()与v. ...
- python表白源代码加音乐_python炫酷烟花表白源代码
天天敲代码的朋友,有没有想过代码也可以变得很酷炫又浪漫?今天就教大家用Python模拟出绽放的烟花,工作之余也可以随时让程序为自己放一场烟花秀. python炫酷烟花表白源代码 这个有趣的小项目并不复 ...
最新文章
- 【怎样写代码】确保对象的唯一性 -- 单例模式(五):一种更好的单例实现方法(静态内部类)
- 人工智能基础-算法工程师为什么要懂线性代数?
- java 泛型参数的类型_Java获得泛型参数类型
- [html] 移动端如何设置页面以全屏模式运行?
- 荣耀v40搭载鸿蒙吗,荣耀V40照常发布,将更换操作系统,同nova8搭载鸿蒙2.0发布...
- vue取通过key取value_彻底理解Vue中的Watcher、Observer、Dep
- 大数据技术之 Kafka (第 3 章 Kafka 架构深入 ) Kafka 生产者
- JS 动态清除Div中内容
- 联发科发布天玑5G开放架构 采用该定制芯片终端7月上市
- 卷组删除pv_CentOS下删除一个卷组(VG)
- `ll/sc` 指令在`linux`中的软件实现
- Plugin “GsonFormat“ is incompatible
- docker打包镜像上传_docker制作自己的镜像并上传dockerhub
- kali linux学习手册,Kali Linux学习手册
- 搬运视频怎么做成原创 | 短视频批量伪原创
- 热修复——Tinker的集成与使用
- sql语句中GROUP BY 和 HAVING的使用 count()
- Redis底层数据结构详解(一)
- AKG K420 耳机线的维修
- 【金融量化】深度学习在金融中的研究热点以及应用
热门文章
- BZOJ3473: 字符串【后缀数组+思维】
- mint-ui 中 Infinite scroll 在tab-container中使用数据全部加载的问题
- c++stl应用入门
- jsoup抓取网页报错UnsupportedMimeTypeException
- Microsoft Azure Tutorial: Build your first movie inventory web app with just a few lines of code
- IE6 PNG 透明的方法
- iOS之深入解析Block的使用和外部变量捕获
- 【数据结构与算法】之深入解析“二叉树的锯齿形层序遍历”的求解思路与算法示例
- 【数据结构与算法】之电话号码键盘的字母组合算法
- LeetCode Algorithm 129. 求根节点到叶节点数字之和