单向循环链表
单向循环链表
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef int DataType_t;
// 构造链表的结点,链表中所有结点的数据类型是相同的
typedef struct CirLinkedList
{
DataType_t data; // 节点的数据域
struct CirLinkedList *next; // 节点的指针域
} CirLiList_t;
// 创建一个空单向循环链表,空链表应该有一个头节点,对链表进行初始化
CirLiList_t *CirLiList_Creat(void)
{
// 1. 创建一个头结点,为头节点申请内存
CirLiList_t *Head = (CirLiList_t *)calloc(1, sizeof(CirLiList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is failed");
return NULL;
}
// 2. 对头节点进行初始化,头节点不存储有效内容,指针域指向自身形成循环
Head->next = Head;
// 3. 把头结点的地址返回即可
return Head;
}
// 创建链表的新的节点并对链表进行初始化
CirLiList_t *CirLiList_NewNode(DataType_t data)
{
// 1. 创建一个新结点,为新节点申请内存
CirLiList_t *New = (CirLiList_t *)calloc(1, sizeof(CirLiList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is failed");
return NULL;
}
// 2. 对新节点进行初始化
New->data = data;
New->next = NULL;
return New;
}
// 将新的节点插入到链表首节点//头插
bool CirLiList_HeadInsert(CirLiList_t *Head, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
CirLiList_t *New = CirLiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 将新节点插入到链表头部
New->next = Head->next;
Head->next = New;
return true;
}
// 将新的节点插入到链表尾节点//尾插
bool CirLiList_TailInsert(CirLiList_t *Head, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
CirLiList_t *New = CirLiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 找到链表的尾节点
CirLiList_t *Phead = Head;
while (Phead->next != Head)
{
Phead = Phead->next;
}
// 3. 将新节点插入到链表尾部
Phead->next = New;
New->next = Head;
return true;
}
// 将新的节点插入到链表指定节点//指定插
bool CirLiList_DestInsert(CirLiList_t *Head, DataType_t dest, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
CirLiList_t *New = CirLiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 找到指定节点的前一个节点
CirLiList_t *Phead = Head;
while (Phead->next != Head && Phead->next->data != dest)
{
Phead = Phead->next;
}
if (Phead->next == Head)
{
printf("data is not found\n");
return false;
}
// 3. 将新节点插入到指定节点之后
New->next = Phead->next;
Phead->next = New;
return true;
}
// 遍历
void CirLiList_Print(CirLiList_t *Head)
{
// 对链表的头节点的地址进行备份
CirLiList_t *Phead = Head->next;
while (Phead != Head)
{
// 输出当前节点的数据域
printf("data = %d\n", Phead->data);
printf("data = %x\n", Phead->next);
Phead = Phead->next;
}
}
// 删除首节点
bool CirLiList_HeadDel(CirLiList_t *Head)
{
if (Head == NULL || Head->next == Head)
{
perror("The LiList is empty");
return false;
}
CirLiList_t *DelNode = Head->next;
Head->next = DelNode->next;
free(DelNode);
return true;
}
// 删除尾节点
bool CirLiList_TailDel(CirLiList_t *Head)
{
if (Head == NULL || Head->next == Head)
{
perror("The LiList is empty");
return false;
}
CirLiList_t *Phead = Head;
while (Phead->next->next != Head)
{
Phead = Phead->next;
}
CirLiList_t *DelNode = Phead->next;
Phead->next = Head;
free(DelNode);
return true;
}
// 删除指定节点
bool CirLiList_DestDel(CirLiList_t *Head, DataType_t dest)
{
if (Head == NULL || Head->next == Head)
{
perror("The LiList is empty");
return false;
}
CirLiList_t *Phead = Head;
while (Phead->next != Head && Phead->next->data != dest)
{
Phead = Phead->next;
}
if (Phead->next == Head)
{
printf("data is not found\n");
return false;
}
CirLiList_t *DelNode = Phead->next;
Phead->next = DelNode->next;
free(DelNode);
return true;
}
int main(int argc, char const *argv[])
{
CirLiList_t *Head = CirLiList_Creat();
CirLiList_TailInsert(Head, 1);
CirLiList_TailInsert(Head, 2);
CirLiList_TailInsert(Head, 3);
CirLiList_TailInsert(Head, 4);
CirLiList_Print(Head);
printf("\n");
CirLiList_HeadInsert(Head, 6);
// 可以在此处调用其他操作函数进行测试
CirLiList_Print(Head);
printf("\n");
CirLiList_HeadDel(Head);
CirLiList_Print(Head);
printf("\n");
CirLiList_TailDel(Head);
CirLiList_Print(Head);
printf("Head = %x\n",Head);
return 0;
}
运行结果


浙公网安备 33010602011771号