//循环单链表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef struct LNode //定义单链表结点类型
{
char data;
struct LNode *next;
} LinkNode;
void CreateListF(LinkNode *&L,char a[],int n)
//头插法建立循环单链表
{
LinkNode *s;int i;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
for (i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
s->data=a[i];
s->next=L->next; //将结点s插在原开始结点之前,头结点之后
L->next=s;
}
s=L->next;
while (s->next!=NULL) //查找尾结点,由s指向它
s=s->next;
s->next=L; //尾结点next域指向头结点
}
void CreateListR(LinkNode *&L,char a[],int n)
//尾插法建立循环单链表
{
LinkNode *s,*r;int i;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
r=L; //r始终指向终端结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
s->data=a[i];
r->next=s; //将结点s插入结点r之后
r=s;
}
r->next=L; //尾结点next域指向头结点
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=L;
}
void DestroyList(LinkNode *&L)
{
LinkNode *p=L,*q=p->next;
while (q!=L)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
bool ListEmpty(LinkNode *L)
{
return(L->next==L);
}
int ListLength(LinkNode *L)
{
LinkNode *p=L;int i=0;
while (p->next!=L)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while (p!=L)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,char &e)
{
int j=0;
LinkNode *p;
if (L->next!=L) //单链表不为空表时
{
if (i==1)
{
e=L->next->data;
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-1 && p!=L)
{
j++;
p=p->next;
}
if (p==L)
return false;
else
{
e=p->data;
return true;
}
}
}
else //单链表为空表时
return false;
}
int LocateElem(LinkNode *L,char e)
{
LinkNode *p=L->next;
int n=1;
while (p!=L && p->data!=e)
{
p=p->next;
n++;
}
if (p==L)
return(0);
else
return(n);
}
bool ListInsert(LinkNode *&L,int i,char e)
{
int j=0;
LinkNode *p=L,*s;
if (p->next==L || i==1) //原单链表为空表或i==1时
{
s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
p->next=s;
return true;
}
else
{
p=L->next;
while (j<i-2 && p!=L)
{
j++;
p=p->next;
}
if (p==L) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
p->next=s;
return true;
}
}
}
bool ListDelete(LinkNode *&L,int i,char &e)
{
int j=0;
LinkNode *p=L,*q;
if (p->next!=L) //原单链表不为空表时
{
if (i==1) //i==1时
{
q=L->next; //删除第1个结点
e=q->data;
L->next=q->next;
free(q);
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-2 && p!=L)
{
j++;
p=p->next;
}
if (p==L) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
q=p->next; //q指向要删除的结点
e=q->data;
p->next=q->next; //从单链表中删除q结点
free(q); //释放q结点
return true;
}
}
}
else return false;
}
int main()
{
LinkNode *h;
char e;
printf("循环单链表的基本运算如下:\n");
printf(" (1)初始化循环单链表h\n");
InitList(h);
printf(" (2)依次采用尾插法插入a,b,c,d,e 元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');
printf(" (3)输出循环单链表h:");
DispList(h);
printf(" (4)循环单链表h的长度: %d\n", ListLength(h));
printf(" (5)循环单链表h为: %s\n",(ListEmpty(h)?"空":"非空"));
GetElem(h,3,e);
printf(" (6)循环单链表h的第3个元素: %c\n",e);
printf(" (7)元素a的位置: %d\n",LocateElem(h,'a'));
printf(" (8)在第4个元素位置上插入f元素\n");
ListInsert(h,4,'f');
printf(" (9)输出循环单链表h:");DispList(h);
printf(" (10)删除h的第3个元素\n");
ListDelete(h,3,e);
printf(" (11)输出循环单链表h:");
DispList(h);
printf(" (11)输出循环单链表h:");
DispList(h);
printf(" (12)释放循环单链表h\n");
DestroyList(h);
return 1;
}
数据结构循环单链表代码算法
最新推荐文章于 2023-11-04 16:31:41 发布