#include<iostream>
using namespace std;
typedef char ElemType;
//定义循环双链表数据结构
typedef struct DNode
{
ElemType data;
struct DNode *prior;
struct DNode *next;
}DLinkNode;
//初始化循环双链表
void InitList(DLinkNode *&h)
{
h=(DLinkNode *)malloc(sizeof(DLinkNode));
h->prior=h->next=h;
}
//尾插法
void CreateListR(DLinkNode *&h,ElemType a[],int n)
{
DLinkNode *s,*r;
h=(DLinkNode *)malloc(sizeof(DLinkNode));
h->next=NULL;
r=h;
for(int i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));
s->data=a[i];
r->next=s;s->prior=r;
r=s;
}
r->next=h;
h->prior=r;
}
//输出循环双链表
void DispList(DLinkNode *h)
{
DLinkNode *p=h->next;
while(p!=h)
{
cout<<p->data<<" ";
p=p->next;
}
printf("\n");
}
//输出循环双链表长度
int ListLength(DLinkNode *h)
{
int n=0;
DLinkNode *p=h;
while(p->next!=h)
{
n++;
p=p->next;
}
return n;
}
//判断循环双链表是否空
bool ListEmpty(DLinkNode *h)
{
return(h->next==h);
}
//输出某个元素
bool GetElem(DLinkNode *h,int i,ElemType &e)
{
int j=1;
DLinkNode *p=h->next;
if(i<=0 || h->next==h) return false;
if(i==1)
{
e=h->next->data;
return true;
}
else
{
while(j<=i-1 && p!=h)
{
j++;
p=p->next;
}
if(p==h)
return false;
else
{
e=p->data;
return true;
}
}
}
//输出某个元素的位置
int LocateElem(DLinkNode *h,ElemType e)
{
int i=1;
DLinkNode *p=h->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return(i);
}
//在某位置插入元素
bool ListInsert(DLinkNode *&h,int i,ElemType e)
{
int j=1;
DLinkNode *p=h,*s;
if(i<=0) return false;
if(p->next==h)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));
s->data=e;
s->next=s;s->next=p;
p->prior=s;s->prior=p;
return true;
}
else if(i==1)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));
s->data=e;
s->next=p->next;p->next=s;
s->next->prior=s;s->prior=p;
return true;
}
else
{
p=h->next;
while(j<=i-2 && p!=h)
{
j++;
p=p->next;
}
if(p==h)
return false;
else
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));
s->data=e;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
}
}
//删除某个元素
bool ListDelete(DLinkNode *&h,int i,ElemType &e)
{
int j=1;
DLinkNode *p=h,*q;
if(i<=0 || h->next==h) return false;
if(i==1)
{
q=h->next;
e=q->data;
h->next=q->next;
q->next->prior=h;
free(q);
return true;
}
else
{
p=h->next;
while(j<=j-2 && p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
q=p->next;
if(q==NULL) return 0;
e=q->data;
p->next=q->next;
if(p->next!=NULL)
p->next->prior=p;
free(q);
return true;
}
}
}
//销毁线性表
void DestroyList(DLinkNode *&h)
{
DLinkNode *pre=h,*p=h->next;
while(p!=h)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
//主函数
int main()
{
DLinkNode *h1;
cout<<"1.初始化循环双链表:"<<endl;InitList(h1);
cout<<"\n2.尾插法插入元素:"<<endl;
ElemType a[5]={'a','b','c','d','e'};
CreateListR(h1,a,5);
cout<<"\n3.循环双链表为:";
DispList(h1);
cout<<"\n4.循环双链表的长度:"<<ListLength(h1)<<endl;
cout<<"\n5.该循环链表";
if(ListEmpty(h1))
cout<<"为空"<<endl;
else
cout<<"不为空"<<endl;
cout<<"\n6.取元素"<<endl;
ElemType temp;cout<<"请输入取的位置:";int k;cin>>k;
if(GetElem(h1,k,temp))
cout<<"取值成功,该循环链表的第"<<k<<"个元素的值为:"<<temp<<endl;
else
cout<<"取值失败,你输入的位置"<<k<<"越界"<<endl;
cout<<"\n7.查找元素:"<<endl<<"请输入查找元素的值:";cin>>temp;
if(LocateElem(h1,temp))
cout<<"输出元素"<<temp<<"的位置为:"<<LocateElem(h1,temp);
else
cout<<"元素"<<temp<<"不存在";
cout<<"\n8.在循环链表指定位置插入元素:"<<endl;
cout<<"请输入插入的位置:";cin>>k;
cout<<"请输入插入元素的值:";cin>>temp;
if(ListInsert(h1,k,temp))
cout<<"插入成功"<<endl;
else
cout<<"插入失败"<<endl;
cout<<"\n9.输出循环链表"<<endl;
DispList(h1);
cout<<"\n10.删除循环链表指定位置的元素"<<endl;
cout<<"请输入删除的位置:";cin>>k;
if(ListDelete(h1,k,temp))
cout<<"删除成功,删除的元素为:"<<temp<<endl;
else
cout<<"删除失败"<<endl;
cout<<"\n11.输出循环链表"<<endl;
DispList(h1);
cout<<"\n12.释放循环链表"<<endl;
DestroyList(h1);
}
c++实现循环双链表的各种运算
于 2024-04-08 21:51:44 首次发布