一、概述
单向循环链表是对单向链表的一种改进方式。本质是
链表尾结点
的
指针域存放头节点的地址
,这种首尾 相连的链表,叫做单向循环链表
。
初始化
void inti_loop_linklist(linkedNode_t** p_head)
{
linkedNode_t* inti_node = malloc(sizeof(linkedNode_t));
if(inti_node == NULL)
{
printf("malloc failure\n");
return NULL;
}
memset(inti_node,0,sizeof(linkedNode_t*));
inti_node->next = inti_node;
*p_head = inti_node;
}
创建新链表
linkedNode_t* creat_empty_linklist()
{
linkedNode_t* loop_node = (linkedNode_t*)malloc(sizeof(linkedNode_t));
if(loop_node = NULL)
{
printf("creat failure\n");
return NULL;
}
memset(loop_node,0,sizeof(linkedNode_t));
loop_node->next = loop_node;
return loop_node;
}
插入数据
void insert_order_linkedList(linkedNode_t* p,datatype_t data)
{
// 创建结点
linkedNode_t* node = (linkedNode_t*)malloc(sizeof(linkedNode_t));
node->data= data;
//查找插入的位置
linkedNode_t* cur = p;
while(p!=cur->next && cur->next->data<data)
{
cur=cur->next;
}
node->next = cur->next;
cur->next = node;
}
显示链表信息
void show_linkedList(linkedNode_t* p)
{
if(is_empty_linkedList(p)==1)
{
printf("loopLinkedList is empty!\n");
return;
}
linkedNode_t* cur = p;
while(p!=cur->next)
{
printf("%d----->",cur->next->data);
cur=cur->next;
}
printf("\n");
}
判断链表是否为空
int is_empty_linkedList(linkedNode_t* p)
{
return (NULL==p || NULL==p->next)?1:0;
}
清空链表
void clear_linkedList(linkedNode_t* p)
{
linkedNode_t* cur = p;
while(p!=cur->next)
{
linkedNode_t* delNode = cur->next;
cur->next = delNode->next;
free(delNode);
delNode=NULL;
}
printf("clear successful.\n");
}