#include<stdio.h>
#include<iostream>
using namespace std;
typedef char Elemtype;
typedef struct DbNode{
Elemtype data;
DbNode *prior,*next;
}DBLinklist;
//创建空双向循环链表
DBLinklist *InitList()
{
DbNode *head;
head=(DBLinklist*)malloc(sizeof(DBLinklist));
head->data=NULL;
head->next=head;
head->prior=head;
return head;
}
//添加结点函数
int AddDBlist(DBLinklist *L,Elemtype e)
{
//函数功能:添加双向循环结点
DbNode *adn;
adn=(DBLinklist*)malloc(sizeof(DBLinklist));
adn->data=e;
adn->prior=L->prior;//新增结点的前指针指向链表的头结点前指针(也就是链表的最后元素)
L->prior->next=adn;//链表的最后元素的next指向新结点
L->prior=adn;//链表的头结点的前指针prior指向新增结点
adn->next=L;//新增结点的next指向头结点
return 1;
}
//向前遍历删除L列表中的e元素
int DelDBlist(DBLinklist *L,Elemtype e)
{
//函数功能:向前遍历L链表删除e元素
DBLinklist *temp;
temp=L->next;
while(temp->data!=e)
{
temp=temp->next;
}
temp->prior->next=temp->prior->next->next;
temp->next->prior=temp->prior;
return 1;
}
int main()
{
DBLinklist *L;
L=InitList();
AddDBlist(L,'a');
AddDBlist(L,'b');
AddDBlist(L,'c');
AddDBlist(L,'d');
DelDBlist(L,'c');
L;
}
循环双向链表
最新推荐文章于 2022-08-28 07:00:00 发布