双向链表、双向循环链表
1、双向链表的概念
双向链表还是一个链表:在逻辑上是连续存储的,在物理上是不连续存储的。
双向链表的每个结点除了存储后一个结点的地址,还需要存储前一个结点的地址。————从前向后遍历、从后往前遍历。
结构和方法的声明
#include<stdio.h>
typedef int DataType;typedef struct Node
{//数据类型union//结合体、联合体{DataType data;//存储元素int length;//存储元素个数};//结点指针类型struct Node *prior;struct Node *next;
}DoubleList;//初始化
void InitDoubleList(DoubleList *head);//销毁
void DestroyDoubleList(DoubleList *head);//按位置插入插入
bool InsertPos(DoubleList *head,DataType value,int pos);bool DeletePos(DoubleList *head,int pos);//求长度
int GetLength(DoubleList *head);//输出
bool PrintDoubleList(DoubleList *head);
方法的实现
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"static DoubleList *ApplyNode(DataType value,DoubleList *prior,DoubleList *next )
{DoubleList *new_node=(DoubleList *)malloc(sizeof(DoubleList));if(new_node==NULL) return NULL;new_node->data=value;new_node->next=next;new_node->prior =prior;return new_node;
}void InitDoubleList(DoubleList *head)
{if(head==NULL) exit(0);head->length=0;head->next=head->prior =NULL;
}int GetLength(DoubleList *head)
{if(head==NULL) exit(0);return head->length;
}bool InsertPos(DoubleList *head,DataType value,int pos)
{if(head==NULL) exit(0);if(pos<0||pos>GetLength(head)) return false;DoubleList *p=head;while(pos){p=p->next;pos--;}DoubleList *new_node=ApplyNode(value,p,p->next);//生成新的结点if(new_node==NULL) return false;if(p->next!= NULL) p->next->prior=new_node; p->next=new_node;head->length++;return true;}bool DeletePos(DoubleList *head,int pos)
{if (head == NULL) exit(0);if(pos<0||pos>GetLength(head)) return false;DoubleList *p=head;while(pos>0){p=p->next;pos--;}DoubleList *q=p->next;if(q->next!=NULL) q->next->prior=p;p->next=q->next;free(q);head->length--;return true;
}bool PointDoubleList(DoubleList *head)
{if (head == NULL) exit(0);DoubleList *p = head->next;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");DoubleList *q=head;int pos=GetLength(head);while(pos>0){q=q->next;pos--;}while (q != head){printf("%d ", q->data);q = q->prior;}printf("\n");
}void DestroyDoubleList(DoubleList *head)
{if(head==NULL) exit(0);if(head->next!=NULL){DeletePos(head,0);}
}int main()
{DoubleList list;InitDoubleList(&list);InsertPos(&list,2,0);InsertPos(&list,3,1);InsertPos(&list,4,2);PointDoubleList(&list);DeletePos (&list,0);PointDoubleList(&list);return 0;
}
双向循环链表
方法声明
#include<stdio.h>
typedef int DataType;typedef struct Node
{//数据类型union//结合体、联合体{DataType data;//存储元素int length;//存储元素个数};//结点指针类型struct Node *prior;struct Node *next;
}DoubleCycleList;//初始化
void InitDoubleCycleList(DoubleCycleList *head);//销毁
void DestroyDoubleCycleList(DoubleCycleList *head);//按位置插入插入
bool InsertPos(DoubleCycleList *head,DataType value,int pos);bool DeletePos(DoubleCycleList *head,int pos);//求长度
int GetLength(DoubleCycleList *head);//输出
bool PrintDoubleCycleList(DoubleCycleList *head);
方法实现:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"static DoubleCycleList *ApplyNode(DataType value,DoubleCycleList *prior,DoubleCycleList *next )
{DoubleCycleList *new_node=(DoubleCycleList *)malloc(sizeof(DoubleCycleList));if(new_node==NULL) return NULL;new_node->data=value;new_node->next=next;new_node->prior =prior;return new_node;
}static bool InsertAfterNode(DoubleCycleList *now,DataType value)
{DoubleCycleList *new_node=ApplyNode(value,now,now->next);if(new_node==NULL) return false;now->next->prior=new_node;now->next=new_node;return true;
}void InitDoubleCycleList(DoubleCycleList *head)
{if(head==NULL) exit(0);head->length=0;head->next=head->prior =head;
}int GetLength(DoubleCycleList *head)
{if(head==NULL) exit(0);return head->length;
}bool InsertPos(DoubleCycleList *head,DataType value,int pos)
{if(head==NULL) exit(0);if(pos<0) return false;pos%=(head->length+1);DoubleCycleList *p=head;while(pos){p=p->next;pos--;}if(InsertAfterNode(p,value)){head->length++;return true;}return false;}bool InsertFront(DoubleCycleList *head,DataType value)
{return InsertPos(head,value,0);
}bool InsertRear(DoubleCycleList *head,DataType value)
{if(head==NULL) exit(0);DoubleCycleList *p=head->prior;if(InsertAfterNode(p,value)){head->length++;return true;}return false;
}bool DeletePos(DoubleCycleList *head,int pos)
{if (head == NULL) exit(0);if(pos<0||pos>GetLength(head)) return false;DoubleCycleList *p=head;pos%=(head->length+1);while(pos>0){p=p->next;pos--;}DoubleCycleList *q=p->next;if(q->next!=NULL) q->next->prior=p;p->next=q->next;free(q);head->length--;return true;
}bool DeleteRear(DoubleCycleList *head)
{if(head==NULL) exit(0);if(head->length==0) return false;DoubleCycleList *p=head->prior;p->prior->next=p->next;p->next->prior=p->prior;free(p);head->length--;return true;
}bool PointDoubleCycleList(DoubleCycleList *head)
{if (head == NULL) exit(0);DoubleCycleList *p = head->next;while (p != NULL){printf("%d ", p->data);p = p->next;if(p==head) break;}printf("\n");DoubleCycleList *q=head;int pos=GetLength(head);while(pos>0){q=q->next;pos--;}while (q != head){printf("%d ", q->data);q = q->prior;if(q==head->prior) break;}printf("\n");
}int main()
{DoubleCycleList list;InitDoubleCycleList(&list);InsertPos(&list,2,0);InsertPos(&list,23,1);InsertFront(&list,11);InsertRear(&list,99);InsertPos(&list,24,2);PointDoubleCycleList(&list);DeletePos (&list,0);DeleteRear(&list);PointDoubleCycleList(&list);return 0;
}
双向链表、双向循环链表相关推荐
- 双向链表双向循环链表基础知识
双向链表: 双向链表:在每个节点前加了一个指向直接前驱的指针域 双向链表定义如下 typedef struct DuLNode {ElemType data;struct DuLNode* prior ...
- 单向链表与双向循环链表
文章目录 单链表操作 大纲 核心⭐ 1-链表的建立 1.1head,tail,p相关问题 2-链表的打印 3-链表的插入 3.1 尾插法 3.1.1一级指针无法对空链表进行尾插问题 3.2 头插法 3 ...
- 线性表:6.双向链表,可构成双向循环链表和C语言实现
之前接触到的链表都只有一个指针,指向直接后继,整个链表只能单方向从表头访问到表尾,这种结构的链表统称为 "单向链表"或"单链表". 如果算法中需要频繁地找某结点 ...
- c语言中next和prior连在一起,C语言中双向链表和双向循环链表详解
双向链表和双向循环链表 和单向链表相比,多了一个前驱结点.如果他为空,那么next和prior都指向自己.而对于双循环链表,只需要最后一个元素的next指向head->next,head-> ...
- java 双向链表循环_双向循环链表的Java版本实现
1.单项循环列表 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环.和单链表相比,循环单链表的长处是从链尾到链 ...
- 【数据结构】双向链表(带头双向循环链表)——超详细代码
文章目录 1. 双链表 1.1 前言 1.2 带头双向循环链表 2. 带头双向循环链表的实现 2.1 双向链表的定义声明 2.2 双向链表的初始化 2.3 释放双向链表 2.4 打印双向链表 2.5 ...
- 数据结构系列2——双向链表和双向循环链表
目录 一.双向链表 1.双向链表的封装和创建 2.双向链表的简单操作 1.头插法插入节点 2.尾插法插入节点 3.指定位置插入 4.表头法删除 5.表尾法删除 6.从头往尾打印节点数据 7.从尾往头打 ...
- 链表 -- 双向循环链表(线性表)
1,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环 ...
- 数据结构-单向循环链表、双向循环链表、仿真链表
一.单向循环链表: 1.概念: 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环. 和单链表相比,循环单链表的 ...
- Python创建一个循环链表、双向循环链表
循环链表与双向循环链表 循环链表 循环链表相较于单链表,将末尾结点的连接域指向了头结点,就类似于将播放器的顺序播放改成了列表循环 双向循环链表 双向链表相较于之前的链表多了上一节点连接域,在双向链表中 ...
最新文章
- 判断类之间的父子关系
- java三大集合_java中三大集合框架
- oculus rift 开发入门
- IIS 之 线程池最大线程数
- java统计文本中英文单词个数split_零基础入门NLP - 新闻文本分类Task2(天池入门赛)...
- PHP学习:字符串操作和正则表达式
- RabbitMQ简介和六种工作模式详解
- jsp标签 判断 余数_舟山铝箔纸不干胶标签制作-雷宝实业
- fread 和 read的区别
- 苏宁易购唱共享之歌,共享干衣、共享数据、共享快递盒为哪般?
- win7 / mysql-8.0.11-winx64 安装的测坑步骤
- 如何建立自己的知识体系?(摘)
- 的usb驱动裁剪协议_飞利浦25W双USB-A口车载快充充电器拆解
- 瑞芯微开发工具怎么刷机
- 《行为设计学》听后感及听书笔记
- Maya模型制作心得
- 计算机理论指导论文,关于计算机理论论文集锦-20210705011555.docx-原创力文档
- 如何修改SVN的地址
- RxJava 过滤操作符 ofType
- 【机器学习】EM 算法