/**********************************************************************************
*
-
file name: 003_單向迴圈連結串列.c
-
author : A13326981379@163.com
-
date : 2024/04/23
-
function : 設計單向迴圈連結串列的介面
-
note : None
-
CopyRight (c) 2024-2024 A13326981379@163.com All Right Reseverd
- *******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
//指的是單向迴圈連結串列中的結點有效資料型別,使用者可以根據需要進行修改
typedef int DataType_t;
//構造單向迴圈連結串列的結點,連結串列中所有結點的資料型別應該是相同的
typedef struct CircularLinkedList
{
DataType_t data; //結點的資料域
struct CircularLinkedList *next; //結點的指標域
}CircLList_t;
//建立一個空單向迴圈連結串列,空連結串列應該有一個頭結點,對連結串列進行初始化
CircLList_t * CircLList_Create(void)
{
//1.建立一個頭結點並對頭結點申請記憶體
CircLList_t *Head = (CircLList_t *)calloc(1,sizeof(CircLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
//2.對頭結點進行初始化,頭結點是不儲存資料域,指標域指向自身,體現“迴圈”思想
Head->next = Head;
//3.把頭結點的地址返回即可
return Head;
}
//建立新的結點,並對新結點進行初始化(資料域 + 指標域)
CircLList_t * CircLList_NewNode(DataType_t data)
{
//1.建立一個新結點並對新結點申請記憶體
CircLList_t *New = (CircLList_t *)calloc(1,sizeof(CircLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
//2.對新結點的資料域和指標域進行初始化
New->data = data;
New->next = NULL;
return New;
}
//頭插
bool CircLList_HeadInsert(CircLList_t *head,DataType_t data)
{
//將傳進來的資料建立一個新的節點
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//定義一個變數儲存尾節點
CircLList_t *tmp = head->next;
//判斷是否為空連結串列
if (head->next == head)
{
head->next = New;
New->next = New;
return true;
}
//遍歷到尾節點
while (tmp->next != head->next)
{
tmp = tmp->next;
}
//將新節點插入頭部
New->next = head->next;
tmp->next = New;
head->next = New;
return true;
}
//尾插
bool CircLList_TailInsert(CircLList_t *head,DataType_t data)
{
//將傳進來的資料建立一個新的節點
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
CircLList_t *tmp = head->next;
//判斷是否為空連結串列
if (head->next == head)
{
head->next = New;
New->next = New;
return true;
}
//遍歷到尾節點
while (tmp->next != head->next)
{
tmp = tmp->next;
}
tmp->next = New;
New->next = head->next;
return true;
}
//指定位置插入(找連結串列中已存在的資料的後面插)
bool CircLList_DestInsert(CircLList_t *head,DataType_t destval,DataType_t data)
{
//將傳進來的資料建立一個新的節點
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//判斷是否為空連結串列
if (head->next == head)
{
head->next = New;
New->next = New;
return true;
}
//定義兩個變數記錄頭節點和首節點地址
CircLList_t *tmp1 = head;
CircLList_t *tmp2 = head->next;
while (tmp2->next != head->next)
{
tmp2 = tmp2->next;
tmp1 = tmp1->next;
if (tmp2->data == destval)
{
//指定位置剛好在尾部(後插)
if (tmp2->next == head->next)
{
New->next = tmp2->next;
tmp2->next = New;
New->next = head->next;
return true;
}
New->next = tmp2->next;
tmp2->next = New;
}
else if (tmp1->data == destval)
{
//指定位置剛好在頭部(後插)
if (tmp1 == head->next)
{
New->next = tmp1->next;
tmp1->next = New;
return true;
}
}
}
return true;
}
//遍歷連結串列
bool CircLList_Print(CircLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
CircLList_t *Phead = Head;
//判斷當前連結串列是否為空,為空則直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
//從首結點開始遍歷
while(Phead->next)
{
//把頭結點的直接後繼作為新的頭結點
Phead = Phead->next;
//輸出頭結點的直接後繼的資料域
printf("data = %d\n",Phead->data);
//判斷是否到達尾結點,尾結點的next指標是指向首結點的地址
if (Phead->next == Head->next)
{
break;
}
}
return true;
}
//頭刪
bool LList_HeadDel(CircLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
CircLList_t *Phead = Head->next;
CircLList_t *tmp = Head->next;
//連結串列為空的情況下
if (Head->next == Head)
{
printf("此連結串列為空!沒有元素可以刪除!");
return false;
}
//遍歷尾節點
while(tmp->next != Head->next)
{
tmp = tmp->next;
}
Head->next = Phead->next;
tmp->next = Phead->next;
Phead->next = NULL;
free(Phead);
return true;
}
//尾刪
bool LList_TailDel(CircLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
CircLList_t *Phead = Head;
CircLList_t *tmp = Head->next;
//連結串列為空的情況下
if (Head->next == Head)
{
printf("此連結串列為空!沒有元素可以刪除!");
return false;
}
//遍歷尾節點
while(tmp->next != Head->next)
{
Phead = Phead->next;
tmp = tmp->next;
}
Phead->next = tmp->next;
tmp->next = NULL;
free(tmp);
return true;
}
//指定刪
bool CircLList_DestDel(CircLList_t *Head,DataType_t destval)
{
//連結串列為空的情況下
if (Head->next == Head)
{
printf("此連結串列為空!沒有元素可以刪除!");
return false;
}
CircLList_t *tmp1 = Head;
CircLList_t *tmp2 = Head->next;
while (tmp2->next != Head->next)
{
tmp2 = tmp2->next;
tmp1 = tmp1->next;
if (tmp2->data == destval)
{
//指定位置剛好在尾部
if (tmp2->next == Head->next)
{
tmp1->next = tmp2->next;
tmp2->next = NULL;
free(tmp2);
return true;
}
tmp1->next = tmp2->next;
tmp2->next = NULL;
free(tmp2);
}
//指定位置剛好在頭部
else if (tmp1->data == destval)
{
if (tmp1 == Head->next)
{
CircLList_t *Phead = Head->next;
// 遍歷到尾節點
while (Phead->next != Head->next)
{
Phead = Phead->next;
}
Head->next = tmp1->next;
Phead->next = tmp1->next;
tmp1->next = NULL;
free(tmp1);
return true;
}
}
}
printf("該連結串列中沒有這個數!\n");
}
int main(int argc, char const *argv[])
{
CircLList_t *head = CircLList_Create();
//頭插入
CircLList_HeadInsert(head,5);
CircLList_HeadInsert(head,6);
CircLList_HeadInsert(head,7);
CircLList_HeadInsert(head,8);
CircLList_HeadInsert(head,9);
CircLList_Print(head);
//尾插
printf("尾插法:\n");
CircLList_TailInsert(head,55);
CircLList_Print(head);
//指定位置插入(找連結串列中已存在的資料的後面插)
printf("指定插:\n");
CircLList_DestInsert(head,9,99);
CircLList_DestInsert(head,6,909);
CircLList_DestInsert(head,55,9900);
CircLList_Print(head);
// LList_HeadDel(head);
// LList_TailDel(head);
printf("\n");
CircLList_DestDel(head,9900);
CircLList_DestDel(head,91);
CircLList_Print(head);
return 0;
}