#include<iostream>
#include<cstring>
#include<stdlib.h>
using namespace std;
typedef struct DLnode
{
int data;
struct DLnode *prior;
struct DLnode *next;
}DLnode,*Dlnode;
void InitDlist(Dlnode &head) //循环双向初始化
{
head=(Dlnode)malloc(sizeof(DLnode));
head->prior=head;
head->next=head;
cout<<"初始化成功!"<<endl;
}
void InsertDlist(DLnode *head,int i) //头插法
{
Dlnode p=NULL;
p=(DLnode *)malloc(sizeof(DLnode));
if(!p)
{
cout<<"对不起,无法分配更多的内存!"<<endl;
return;
}
p->data=i;
p->next=head->next;
p->next->prior=p;
p->prior=head;
head->next=p;
}
int EmptyDlist(Dlnode head)
{
if(head->prior==head&&head->next==head) //双向判空
return 1;
return 0;
}
int LengthDlist(Dlnode head)
{
DLnode *p=NULL;
int len=0;
if(EmptyDlist(head))
return 0;
p=head->next;
while(p!=head)
{
len++;
p=p->next;
}
return len;
}
int DeleteDlist(DLnode *head,int x)
{
DLnode *p=NULL;
int i=1;
p=head->next;
if(EmptyDlist(head))
{
cout<<"对不起,链表为空!"<<endl;
return 0;
}
if(x>LengthDlist(head)||x<=0)
{
cout<<"对不起,删除的位置不合适!"<<endl;
return 0;
}
while(i<x)
{
p=p->next;
i++;
}
p->prior->next=p->prior->next->next;
p->prior->next->prior=p->prior;
return p->data;
}
void InsearchDlist(Dlnode head,int x)
{
DLnode *p=NULL;
int len=1;
if(EmptyDlist(head))
{
cout<<"对不起链表是空!"<<endl;
return;
}
p=head->next;
while(p!=head)
{
if(p->data==x)
{
cout<<"你查找的元素在第"<<len<<"个"<<endl;
return;
}
p=p->next;
len++;
}
cout<<"你所找的元素不存在!"<<endl;
return;
}
void ModifyDlist(DLnode *head,int local,int x)
{
DLnode *p=NULL;
int len=1;
p=head->next;
while(len<local)
{
p=p->next;
len++;
}
p->data=x;
cout<<"修改成功!"<<endl;
return;
}
void DestoryDlist(Dlnode head)
{
Dlnode p=head->next;
while(p!=head)
{
p->prior->next=p->next;
p->next->prior=p->prior;
p=head->next;
}
cout<<"销毁成功!"<<endl;
}
void PrintDlist(Dlnode head)
{
Dlnode p=head->next;
if(EmptyDlist(head))
{
cout<<"链表是空的,无法输出链表!"<<endl;
return;
}
while(head!=p)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
return;
}
void instruction(Dlnode head)
{
cout<<"1. 初始操作"<<endl;
cout<<"2. 新增操作"<<endl;
cout<<"3. 删除操作"<<endl;
cout<<"4. 查找操作"<<endl;
cout<<"5. 修改操作"<<endl;
cout<<"6. 销毁操作"<<endl;
cout<<"7. 求长操作"<<endl;
cout<<"8. 输出操作"<<endl;
cout<<"9. 退出程序"<<endl;
cout<<"请输入要完成的指令:";
int n,x,i,local,len;
cin>>n;
if(n<1||n>9)
{
cout<<"对不起,你的操作无效!"<<endl;
exit(0);
}
switch(n)
{
case 1:
InitDlist(head);
cout<<"接下来完成建表操作"<<endl;
cout<<"请输入你要添加的元素的个数:";
cin>>x;
while(x--)
{
cout<<"请输入一个值:";
cin>>i;
InsertDlist(head,i);
}
break;
case 2:
if(!head)
{
cout<<"对不起,请先初始化!"<<endl;
break;
}
cout<<"请输入你要添加的元素的个数:";
cin>>x;
while(x--)
{
cout<<"请输入一个值:";
cin>>i;
InsertDlist(head,i);
}
break;
case 3:
cout<<"请输入删除结点的位置:";
cin>>x;
DeleteDlist(head,x);
break;
case 4:
cout<<"请输入你要查找的元素:";
cin>>x;
InsearchDlist(head,x);
break;
case 5:
if(EmptyDlist(head))
{
cout<<"对不起,链表为空!"<<endl;
break;
}
cout<<"请输入你要改变的元素的位置:";
do
{
cin>>local;
if(local<1||local>LengthDlist(head))
cout<<"对不起,你输入的元素不在区域内!请重新输入:";
}while(local<1||local>LengthDlist(head));
cout<<"请输入修改后的值:";
cin>>x;
ModifyDlist(head,local,x);
break;
case 6:
DestoryDlist(head);
break;
case 7:
len=LengthDlist(head);
cout<<"当前链长为:"<<len<<endl;
break;
case 8:
PrintDlist(head);
break;
case 9:
return;
default:
instruction(head);
break;
}
instruction(head);
}
int main()
{
Dlnode head=NULL;
instruction(head); //实现不同功能的函数
return 0;
}