数据结构(C实现)------- 双向链表
来源:互联网 发布:d3.js视频 编辑:程序博客网 时间:2024/04/26 04:40
双向链表中的每一个结点都含有两个指针域,一个指针域存放其后继结点的存储地址,另一个指针域则存放其前驱结点的存储地址。
双向链表结点的类型描述:
//双向链表的类型描述typedef int ElemType;typedef struct node{ElemType data;struct node *prior,*next;}DuLNode,*DuLinkList;
其中,prior域存放的是其前驱结点的存储地址,next域存放的是其后继结点的存储地址。
双向链表有两个特点:一是可以从两个方向搜索某个结点,这使得链表的某些操作(如插入和删除)变得比较简单; 二是无论利用前链还是后链都可以遍历整个双向链表。
双向链表的操作基本和单链表的操作相同;
1. 头插法创建带头结点的双向链表Create_DLinkListF(int n)
//头插法创建带头结点的双向链表DuLinkList Create_DLinkListF(int n){DuLinkList L,p;int i = n - 1;ElemType x;//新建头结点L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一个结点scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;//加入其他结点while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;p->next = L->next;L->next->prior = p;p->prior = L;L->next = p;i--;}return L;}
2. 尾插法创建带头结点的双向链表Create_DLinkListR(int n)
//尾插法创建带头结点的双向链表DuLinkList Create_DLinkListR(int n){DuLinkList L,p,lastNode;int i = n - 1;ElemType x;//新建头结点L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一个结点scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;lastNode = p;//加入其他结点while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;lastNode->next = p;p->prior = lastNode;p->next = NULL;lastNode = p;i--;}return L;}3. 在指定结点之前插入新结点Insert_DLinkListBefore(DuLinkList p,ElemType x)
//在指定结点之前插入新结点void Insert_DLinkListBefore(DuLinkList p,ElemType x){DuLinkList newNode;//判断结点p之前的结点的合法性:if(p->prior == NULL)printf("结点不合法,不能在该结点之前插入结点\n");else{newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;newNode->next = p;p->prior->next = newNode;newNode->prior = p->prior;p->prior = newNode;}}4. 在指定结点之后插入新结点Insert_DLinkListAfter(DuLinkList p,ElemType x)
//在指定结点之后插入新结点void Insert_DLinkListAfter(DuLinkList p,ElemType x){DuLinkList newNode;newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;//当插入位置是最后一个结点之后时if(p->next == NULL){p->next = newNode;newNode->prior = p;newNode->next = NULL;}else{newNode->next = p->next;p->next->prior = newNode;p->next = newNode;newNode->prior = p;}}5. 删除指定结点Delete_DLinkList(DuLinkList p)
//删除指定结点void Delete_DLinkList(DuLinkList p){//如果删除的是最后一个元素if(p->next == NULL)p->prior->next = NULL;else{p->prior->next = p->next;p->next->prior = p->prior;}free(p);}6. 后链输出双向链表Print_DLinkListN(DuLinkList L)
//后链输出双向链表void Print_DLinkListN(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p->next;}printf("\n");}7.前链输出双向链表Print_DLinkListP(DuLinkList p)
//前链输出双向链表void Print_DLinkListP(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p-prior;}printf("\n");}
至于双向链表的其他操作,如定位,和单链表的操作类同,不再赘述。
0 0
- 数据结构(C实现)------- 双向链表
- C实现通用数据结构--双向链表
- 数据结构--双向循环链表C实现
- 数据结构之双向链表(C语言实现)
- 数据结构 - 双向链表(C++)
- 数据结构(C#)--双向链表
- java 实现双向链表(数据结构)
- 数据结构--双向链表实现(java)
- 数据结构的C实现_双向循环链表
- 数据结构C语言实现系列——双向链表
- (C语言)双向链表实现案例(数据结构六)
- 数据结构--双向循环链表c语言实现
- <C/C++数据结构>双向链表(C++模板实现)
- 数据结构之---c语言实现双向链表操作
- 数据结构——双向链表(C语言实现)
- 【数据结构】双向链表实现
- 数据结构双向链表c语言实现(linux下多文件实现)
- 双向链表(C实现)
- UVALive 2531 The K-League 网络流+建图
- HDU4939Stupid Tower Defense (有思想的dp)
- MPMoviePlayerController支持播放哪些类型的文件
- 快点给个希望的要光明
- Cocos2dx3.2alpha0+Vs2013环境配置
- 数据结构(C实现)------- 双向链表
- 华为机试:数学应用
- VS2012 每次编译程序都会出现”无法查找或打开 PDB 文件“问题解决方案
- android 自定义对话框
- 自定义缓存,自动创建文件夹无权限
- c/c++ 混乱代码
- 关闭IE当前安全设置会使计算机有风险提示
- 802.11n 是什么东东?
- MySQL基础0