c语言实现循环单链表

//循环单链表
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>

typedef int ElemType;

//定义链表节点 
typedef struct ListNode
{
    struct ListNode* next;
    ElemType data;
}ListNode,*LinkList;

//初始化头节点 
LinkList InitHead()
{
    LinkList L=(LinkList)malloc(sizeof(ListNode));
    L->next=L;
    return L;
}

//增---头插法
bool HeadInsertList(LinkList L,int data)
{
	LinkList p=(LinkList)malloc(sizeof(ListNode));
	
	p->next=L->next;
	L->next=p;
	p->data=data; 
	
	return true;
}
//删
bool DeleteList(LinkList L,int data)
{
	LinkList p=L->next;
	LinkList q=L;
	while(p!=L) 
	{
		if(p->data==data)
		{
			q->next=p->next;
			free(p);
			return true;
		}
		q=p;
		p=p->next;	
	}
	return false;
}
//改 将链表L中的a改成b 
bool ChageList(LinkList L,int a,int b)
{
	if(L->next==L)
	{
		printf("无元素,修改失败\n");
		return false;
	}
	LinkList p=L->next; 
	while(p!=L)
	{
		if(p->data==a)
		{
			p->data=b;
			return true;
		}
		p=p->next;
	}
	return false;
}

//查 
bool FindList(LinkList L,int data)
{
	LinkList p=L;
	while(p!=L)
	{
		if(p->data==data)
		{
			return true;
		}
	}
	return false;
}
bool isEmptyList(LinkList L)
{
	if(L->next==L)
	{
		return true;
	}
	return false;
} 

//打印 
void PrintList(LinkList L)
{
	LinkList p=L->next;
	while(p!=L)
	{
		printf("%d\t",p->data);
		p=p->next;
	}
	printf("\n");
}

int main(void)
{
	LinkList L=InitHead(); 
	
	for(int i=0;i<10;i++)
	{
		HeadInsertList(L,i);
	}
	PrintList(L);
	
	DeleteList(L,0);
	PrintList(L);
	
	ChageList(L,1,99); 
	PrintList(L);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值