数据结构与算法之循环单链表

循环单链表

#include <stdio.h>
#include <malloc.h>
typedef int Elemtype;
typedef struct LNode		
{
	Elemtype date;
    struct LNode *next;
} LinkNode;
/*******************************************
********************************************/
void CreateListF(LinkNode *&L,Elemtype 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->date=a[i];
		s->next=L->next;			
		L->next=s;
	}
	s=L->next;	
	while (s->next!=NULL)		
		s=s->next;
	s->next=L;					

}
/*******************************************
********************************************/
void CreateListR(LinkNode *&L,Elemtype a[],int n)  //尾插法建立循环单链表
{
	LinkNode *s,*r;int i;
	L=(LinkNode *)malloc(sizeof(LinkNode));  	
	L->next=NULL;
	r=L;				
	for (i=0;i<n;i++)
	{	
		s=(LinkNode *)malloc(sizeof(LinkNode));
		s->date=a[i];
		r->next=s;			
		r=s;
	}
	r->next=L;				
}
/*******************************************
********************************************/
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->date);
		p=p->next;
	}
	printf("\n");
}
/*******************************************
********************************************/
bool GetElem(LinkNode *L,int i)//根据元素查下标 
{
	int j=0;
	LinkNode *p;
	if (L->next!=L)	
	{
		if (i==1)
		{
			printf("%c",L->next->date);
			
		}
		else		
		{
			p=L->next;
			while (j<i-1 && p!=L)
			{
				j++;
				p=p->next;
			}
			if (p==L)
				return false;
			else
			{
				printf("%d",p->date);
			}
		}
	}
	else			
		return false;
}
/*******************************************
********************************************/
int LocateElem(LinkNode *L,Elemtype e)//根据下标查元素 
{
	LinkNode *p=L->next;
	int n=1;
	while (p!=L && p->date!=e)
	{
		p=p->next;
		n++;
	}
	if (p==L)
		return(0);
	else
		return(n);
}
/*******************************************
********************************************/
bool ListInsert(LinkNode *&L,int i,Elemtype e)//插入元素 
{
	int j=0;
	LinkNode *p=L,*s;
	if (p->next==L || i==1)		
	{
		s=(LinkNode *)malloc(sizeof(LinkNode));	
		s->date=e;								
		s->next=p->next;		
		p->next=s;
		return true;
	}
	else
	{
		p=L->next;
		while (j<i-2 && p!=L)
		{
			j++;
			p=p->next;
		}
		if (p==L)			
			return false;
		else				
		{
			s=(LinkNode *)malloc(sizeof(LinkNode));	
			s->date=e;								
			s->next=p->next;						
			p->next=s;
			return true;
		}
	}
}
/*******************************************
********************************************/
bool ListDelete(LinkNode *&L,int i,Elemtype &e)//删除元素 
{
	int j=0;
	LinkNode *p=L,*q;
	if (p->next!=L)					
	{
		if (i==1)				
		{
			q=L->next;			
			e=q->date;
			L->next=q->next;
			free(q);
			return true;
		}
		else						
		{
			p=L->next;
			while (j<i-2 && p!=L)
			{
				j++;
				p=p->next;
			}
			if (p==L)			
				return false;
			else					
			{
				q=p->next;			
				e=q->date;
				p->next=q->next;	
				free(q);			
				return true;
			}
		}
	}
	else return false;
}
/*******************************************
********************************************/
int main()
{
    LinkNode *L;
    Elemtype a[5]={'a','b','c','d','e'};       
    Elemtype e;
    InitList(L);                        //初始化链表L
    printf("初始化完成\n");
 /*******************************************
********************************************/   
    CreateListF(L,a,5);                  //依次插入a,b,c,d,e
    printf("头插入元素后输出的链表为:\n");
/*******************************************
********************************************/
    DispList(L);                        //输出链表L
/*******************************************
********************************************/
    printf("此链表长度为:");
    printf("%d\n",ListLength(L));            //输出链表L长度
/*******************************************
********************************************/ 
    if(ListEmpty(L)==false)                  //判断链表L是否为空
        printf("不为空\n");
    else
        printf("为空\n");
/*******************************************
********************************************/
    printf("链表的第3个元素为:");   //输出链表的第3个元素 
    GetElem(L,3);
/*******************************************
********************************************/ 
    printf("元素a的位置为:");         //查找a的位置 
    LocateElem(L,'a');
/*******************************************
********************************************/ 
    ListInsert(L,4,'f');            //在第4个元素位置上插入元素f 
    printf("插入元素后的链表为:\n");
/*******************************************
********************************************/
    DispList(L);                        //输出链表L
/*******************************************
********************************************/
    ListDelete(L,3,e);              //删除L的第3个元素
/*******************************************
********************************************/   
    printf("删除元素后的链表为:\n");
    DispList(L);                       //输出链表L
/*******************************************
********************************************/ 
    DestroyList(L);                   //释放链表L
    printf("释放链表");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值