//循环单链表
#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;
}
c语言实现循环单链表
最新推荐文章于 2024-09-09 23:14:05 发布