#include <iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L)
{
L=new LNode;
L->next=L;
}
void CreatList(LinkList &L)//循环链表的创建
{
L=new LNode;
L->next=L;
int x;
cin>>x;
LNode *p;
LNode *r=L;
while(x!=0)
{
p=new LNode;
p->data=x;
p->next=r->next;
r->next=p;
r=p;
cin>>x;
}
}
void ListInsert(LinkList &L,int i,int e)//循环链表插入
{
int count=0;
LNode *p;
p=L;
while(count<i-1)
{
p=p->next;
count++;
}
LNode *temp=new LNode;
temp->data=e;
temp->next=p->next;
p->next=temp;
}
void ListDelete(LinkList &L,int i,int e)//循环链表删除结点
{
int count=0;
LNode *p;
p=L;
while(count<i-1)
{
p=p->next;
count++;
}
p->next=p->next->next;
}
void ListPrint(LinkList L)//循环链表的打印
{
LNode *p;
p=L->next;
while(p->next!=L)
{
p=p->next;
}
p=p->next->next;
while(p!=L)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
//简单的测试
LinkList L;
CreatList(L);
ListPrint(L);
cout<<endl;
int x;
cin>>x;
ListDelete(L,x,x);
ListPrint(L);
cout<<endl;
ListInsert(L,x,x);
ListPrint(L);
cout<<endl;
}
循环单链表代码(无注释,自用)
最新推荐文章于 2023-03-29 09:26:58 发布