目的:领会循环单链表存储结构和掌握循环单链表中的各种基本运算算法设计。
内容:实现循环单链表的各种基本运算和整体建表算法。(假设循环单链表的元素类型 ElemType 为 char)
程序要求:
1.初始化循环单链表h。
2.依次采用尾插法插入a、b、c、d、e元素。
3.输出循环单链表h。
4.输出循环单链表h的长度。
5.判断循环单链表h是否为空。
6.根据用户输入的位置,输出循环单链表h的元素。
7.根据用户输入的元素,输出循环单链表h的元素位置。
8.根据用户输入的位置和元素,对循环单链表h插入新的元素。
9.输出循环单链表h。
10.根据用户输入的位置,删除循环单链表h相应的元素。
11.输出循环单链表h。
12.释放循环单链表h。
----------------------------------------------------------------------------------------------
定义循环单链表的数据结构
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *next;
} LinkList;
1.初始化循环单链表
void InitList(LinkList *&L)
{
L= new LinkList;
L->next=L;
}
2.尾插法建表
最后一个节点指向头节点,形成循环单链表。
void CreateListR(LinkList *&L,ElemType a[],int n)
{
LinkList *s,*r;
L = new LinkList;
r = L;
for (int i=0;i<n;i++)
{
s = new LinkList;
s->data = a[i];
r->next = s;
r = s;
}
r->next = L;
}
3.销毁循环单链表
最后只剩下头节点和尾节点没有删除,pre 处于尾节点,p 处于头节点,所以循环结束后需要删除两点节点。
void DestroyList(LinkList *&L)
{
LinkList *pre=L->next,*p=pre->next;
while (p != L)
{
delete pre;
pre = p;
p = p->next;
}
delete pre;
delete p;
}
4.判断循环单链表是否为空表
bool ListEmpty(LinkList *L)
{
return (L->next==L);
}
5. 求循环单链表的长度
int ListLength(LinkList *L)
{
int count = 0;
LinkList *p=L->next;
while (p != L)
{
count ++;
p = p->next;
}
return count;
}
6.输出循环单链表
void DispList(LinkList *L)
{
LinkList *p=L->next;
while (p != L)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
7.求某个数据元素值
bool GetElem(LinkList *L,int i,ElemType &e)
{
int j=0; LinkList *p=L;
while (j < i && p->next != L)
{
j ++;
p = p->next;
}
if (i<1 || i>j)
return false;
e = p->data;
return true;
}
8.按元素值查找
int LocateElem(LinkList *L, ElemType e)
{
LinkList *p=L->next;
int i=1;
while (p != L && p->data != e)
{
i ++;
p = p->next;
}
if (p == L)
return 0;
else
return i;
}
9.插入数据元素
//8.插入数据元素
bool ListInsert(LinkList *&L,int i,ElemType e)
{
if (i < 1) return false;
int j=0; LinkList *p=L,*s;
if (i==1)
{
s = new LinkList;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
else
{
while (j < i-1 && p->next!=L)
{
j ++;
p = p->next;
}
if (i > j+1 || p==L)
return false;
s = new LinkList;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
}
10.删除数据元素
bool ListDelete(LinkList *&L,int i,ElemType &e)
{
if (i < 1) return false;
int j=0;
LinkList *p=L,*q;
if (i==1)
{
q = p->next;
e = q->data;
p->next = q->next;
delete q;
return true;
}
else
{
while (j < i-1 && p->next!=L)
{
j ++;
p = p->next;
}
if (p->next==L)
return false;
q = p->next;
e = q->data;
p->next = q->next;
delete q;
return true;
}
}
----------------------------------------------------------------------------------------------
主函数
int main()
{
LinkList *L1;
cout<<"1.初始化循环链表:"<<endl;InitList(L1);
cout<<"\n2.尾插法建表:"<<endl;
ElemType a[5]={'A','B','C','D','E'};
for(int i=0;i<5;i++)
if(!ListInsert(L1,i+1,a[i]))
cout<<"插入失败!";
cout<<"\n3.循环链表的元素为:";
DispList(L1);
cout<<"\n4.该循环链表的长度为:"<<ListLength(L1)<<endl;
cout<<"\n5.该循环链表 ";
if(ListEmpty(L1))
cout<<"为空!"<<endl;
else
cout<<"不为空!"<<endl;
cout<<"\n6.取元素..........."<<endl;
ElemType temp;cout<<"请输入取的位置:";int k;cin>>k;
if(GetElem(L1,k,temp))
cout<<"取值成功,该循环链表的第"<<k<<"个元素的值为:"<<temp<<endl;
else
cout<<"取值失败,你输入的位置"<<k<<"越界:"<<endl;
cout<<"\n7.查找元素:"<<endl<<"请输入查找元素的值:";cin>>temp;
if(LocateElem(L1,temp))
cout<<"输出元素'"<<temp<<"'的位置为:"<<LocateElem(L1,temp)<<endl;
else
cout<<"元素'"<<temp<<"'不存在."<<endl;
cout<<"\n8.在循环链表指定位置插入元素 :"<<endl;
cout<<"请输入插入的位置:";cin>>k;
cout<<"请输入插入元素的值:";cin>>temp;
if(ListInsert(L1,k,temp))
cout<<"插入成功"<<endl;
else
cout<<"插入失败!"<<endl;
cout<<"\n9.输出循环链表"<<endl;
DispList(L1);
cout<<"\n10.删除循环链表指定位置的元素"<<endl;
cout<<"请输入删除的位置:";cin>>k;
if(ListDelete(L1,k,temp))
cout<<"删除成功,删除的元素为:"<<temp<<endl;
else
cout<<"删除失败!"<<endl;
cout<<"\n11.输出循环链表"<<endl;
DispList(L1);
cout<<"\n12.释放循环链表"<<endl;
DestroyList(L1);
return 0;
}