1、双向链表的定义:双向链表也是链表的一种,它每个数据结点中都有两个结点,分别指向其直接前驱和直接后继。所以我们从双向链表的任意一个结点开始都可以很方便的访问其前驱元素和后继元素。
2、双向链表的节点结构:
3、双向链表的存储结构:
双向链表也是采用的链式存储结构,它与单链表的区别就是每个数据结点中多了一个指向前驱元素的指针域 ,它的存储结构如下图:
4、当双向链表只有一个节点的时候它的存储结构:
5、双向链表的实现与操作:
(1)dlist.h:
#pragma once
//带头节点的双向链表,不循环
//链表尾节点的next为NULL,头节点的prio为NULL
typedef struct DNode
{
int data; //数据
struct DNode *next;//下一个节点的地址
struct DNode *prio;//前趋节点的地址
}DNode,*DList;
//初始化
void InitList(DList plist);
//头插法
bool Insert_head(DList plist,int val);
//尾插法
bool Insert_tail(DList plist,int val);
//在pos下标插入数据val
bool Insert_pos(DList plist,int pos,int val);
//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist,int key);
//删除第一个key对应的节点
bool Delete(DList plist,int key);
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist,int *rtval);
//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist,int *rtval);
//获取长度,统计数据节点的个数
int GetLength(DList plist);
//判空
bool IsEmpty(DList plist);
//清除所以数据
void Clear(DList plist);
//销毁所有节点
void Destroy(DList plist);
//打印
void Show(DList plist);
//反转
void Reverse(DList plist);
(2)dlist.cpp:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "dlist.h"
//初始化
void InitList(DList plist)
{
assert(plist != NULL);
if(plist == NULL)
{
return ;
}
plist->prio = NULL;
plist->next = NULL;
}
static DNode *BuyNode(int val)
{
DNode *p = (DNode *)malloc(sizeof(DNode));
p->data = val;
return p;
}
//头插法
bool Insert_head(DList plist,int val)
{
DNode *p = BuyNode(val);
if(plist->next != NULL)//*****
{
plist->next->prio = p;//将plist的后一个节点的前驱设置成p
}
p->next = plist->next;
p->prio = plist;
plist->next = p;
return true;
}
//尾插法
bool Insert_tail(DList plist,int val)
{
DNode *p = BuyNode(val);
DNode *q;
//找尾巴
for(q = plist;q->next != NULL;q = q->next);
p->next = q->next;
q->next = p;
p->prio = q;
return true;
}
//在pos下标插入数据val
bool Insert_pos(DList plist,int pos,int val)
{
if(pos < 0)
{
return false;
}
DNode *p = BuyNode(val);
DNode *q;
int i;
for(i = 0,q = plist;q->next != NULL && i < pos;i++,q = q->next);
p->next = q->next;
p->prio = q->next->prio;
q->next->prio = p;
q->next = p;
return true;
}
//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist,int key)
{
for(DNode *p = plist->next;p != NULL;p = p->next)
{
if(p->data == key)
{
return p;
}
}
return NULL;
}
//删除第一个key对应的节点
bool Delete(DList plist,int key)
{
DNode *p = Search(plist,key);
if(p == NULL)
{
return false;
}
p->prio->next = p->next;
//只有当p的后继节点存在时才能将p的后继节点的前驱改变
if(p->next != NULL)
{
p->next->prio = p->prio;
}
free(p);
return true;
}
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist,int *rtval)
{
assert(plist != NULL);
if(plist == NULL)
{
return false;
}
if(plist->next == NULL)
{
return false;
}
DNode *p = plist->next;
plist->next = p->next;
if(p->next != NULL)
{
p->next->prio = plist;
}
if(rtval = NULL)
{
return false;
}
*rtval = p->data;
free(p);
return true;
}
//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist,int *rtval)
{
assert(plist != NULL);
if(plist == NULL)
{
return false;
}
if(plist->next == NULL)
{
return false;
}
DNode *p;
for(p = plist;p->next != NULL;p = p->next)
{
*rtval = p->data;
}
p->prio->next = p->next;
p->next->prio = p->prio;
free(p);
return true;
}
//获取长度,统计数据节点的个数
int GetLength(DList plist)
{
int count = 0;
for(DNode *p = plist->next;p != NULL;p = p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(DList plist)
{
if(plist->next == NULL)
{
return true;
}
return false;
}
//清除所有数据
void Clear(DList plist)
{
Destroy(plist);
}
//销毁所有节点
void Destroy(DList plist)
{
DNode *p;
while(plist->next != NULL)
{
p->prio->next = p->next;
p->next->prio = p->prio;
free(p);
}
}
//打印
void Show(DList plist)
{
for(DNode *p = plist->next;p != NULL;p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
//反转
void Reverse(DList plist)
{
if(plist == NULL || plist->next == NULL || plist->next->next == NULL)
{
return;
}
DNode *p = plist->next;//第一个数据节点
DNode *q;
for(q = plist;q->next != NULL;q = q->next);//最后一个数据节点
int len = GetLength(plist);
for(int i = 0;i < len/2;i++)
{
int tmp = p->data;
p->data = q->data;
q->data = tmp;
p = p->next;
q = q->prio;
}
}
(3)main.cpp:
#include <stdio.h>
#include "dlist.h"
int main()
{
DNode head;
InitList(&head);
for(int i=0;i<10;i++)
{
Insert_head(&head,i);
}
Show(&head);
Delete(&head,9);
Delete(&head,5);
Delete(&head,0);
Show(&head);
Reverse(&head);
Show(&head);
return 0;
}