數(shù)據(jù)結(jié)構(gòu)線性結(jié)構(gòu)之單向循環(huán)鏈表的基本操作文章來源地址http://www.zghlxwxcb.cn/news/detail-856693.html
/********************************************************************************************************
*
*
* 設(shè)計(jì)單向循環(huán)鏈表的接口
*
*
*
* Copyright (c) 2023-2024 17820413718@163.com All right Reserved
* ******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 指的是單向循環(huán)鏈表中的結(jié)點(diǎn)有效數(shù)據(jù)類型,用戶可以根據(jù)需要進(jìn)行修改
typedef int DataType_t;
// 構(gòu)造單向循環(huán)鏈表的結(jié)點(diǎn),鏈表中所有結(jié)點(diǎn)的數(shù)據(jù)類型應(yīng)該是相同的
typedef struct CircularLinkedList
{
DataType_t data; // 結(jié)點(diǎn)的數(shù)據(jù)域
struct CircularLinkedList *next; // 結(jié)點(diǎn)的指針域
} CircLList_t;
// 遍歷鏈表
bool CircLList_Print(CircLList_t *Head)
{
// 對單向循環(huán)鏈表的頭結(jié)點(diǎn)的地址進(jìn)行備份
CircLList_t *Phead = Head;
// 判斷當(dāng)前鏈表是否為空,為空則直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
// 從首結(jié)點(diǎn)開始遍歷
while (Phead->next)
{
// 把頭結(jié)點(diǎn)的直接后繼作為新的頭結(jié)點(diǎn)
Phead = Phead->next;
// 輸出頭結(jié)點(diǎn)的直接后繼的數(shù)據(jù)域
printf("data = %d\n", Phead->data);
// 判斷是否到達(dá)尾結(jié)點(diǎn),尾結(jié)點(diǎn)的next指針是指向首結(jié)點(diǎn)的地址
if (Phead->next == Head->next)
{
break;
}
}
return true;
}
// 創(chuàng)建一個(gè)空單向循環(huán)鏈表,空鏈表應(yīng)該有一個(gè)頭結(jié)點(diǎn),對鏈表進(jìn)行初始化
CircLList_t *CircLList_Create(void)
{
// 1.創(chuàng)建一個(gè)頭結(jié)點(diǎn)并對頭結(jié)點(diǎn)申請內(nèi)存
CircLList_t *Head = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.對頭結(jié)點(diǎn)進(jìn)行初始化,頭結(jié)點(diǎn)是不存儲數(shù)據(jù)域,指針域指向自身,體現(xiàn)“循環(huán)”思想
Head->next = Head;
// 3.把頭結(jié)點(diǎn)的地址返回即可
return Head;
}
// 創(chuàng)建新的結(jié)點(diǎn),并對新結(jié)點(diǎn)進(jìn)行初始化(數(shù)據(jù)域 + 指針域)
CircLList_t *CircLList_NewNode(DataType_t data)
{
// 1.創(chuàng)建一個(gè)新結(jié)點(diǎn)并對新結(jié)點(diǎn)申請內(nèi)存
CircLList_t *New = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
// 2.對新結(jié)點(diǎn)的數(shù)據(jù)域和指針域進(jìn)行初始化
New->data = data;
New->next = NULL;
return New;
}
// 頭插
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
if (Head == NULL)
{
printf("鏈表為空!\n");
return false;
}
// 創(chuàng)建一個(gè)指針代替Head
CircLList_t *phead = Head;
// 新建一個(gè)結(jié)點(diǎn)
CircLList_t *newNode = CircLList_NewNode(data);
// 讓新建的結(jié)點(diǎn)指向原來的首結(jié)點(diǎn)
newNode->next = phead->next;
// 讓頭結(jié)點(diǎn)指向新建的結(jié)點(diǎn)
phead->next = newNode;
return true;
}
// 尾插
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
if (Head == NULL)
{
printf("鏈表為空!\n");
return false;
}
// 創(chuàng)建一個(gè)指針代替Head
CircLList_t *phead = Head->next;
// 新建一個(gè)結(jié)點(diǎn)
CircLList_t *newNode = CircLList_NewNode(data);
while (phead->next != Head)
{
phead = phead->next;
if (phead->next == Head->next)
{
break;
}
}
// 尾結(jié)點(diǎn)指向新結(jié)點(diǎn)
phead->next = newNode;
// 新結(jié)點(diǎn)指向首結(jié)點(diǎn)
newNode->next = Head->next;
return true;
}
// 指定位置插入(后插)
bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
if (Head == NULL)
{
printf("鏈表為空!\n");
return false;
}
// 創(chuàng)建一個(gè)指針代替Head
CircLList_t *phead = Head;
// 新建一個(gè)結(jié)點(diǎn)
CircLList_t *newNode = CircLList_NewNode(data);
while (phead)
{
// 找到目標(biāo)結(jié)點(diǎn)的上前結(jié)點(diǎn)
if (phead->next->data == destval)
{
break;
}
phead = phead->next;
}
newNode->next = phead->next;
phead->next = newNode;
}
// 頭刪
bool CircLList_HeadDel(CircLList_t *Head)
{
// 對單向循環(huán)鏈表的首結(jié)點(diǎn)的地址進(jìn)行備份
CircLList_t *Phead = Head->next;
while (Phead->next != Head)
{
if (Phead->next == Head->next)
{
break;
}
Phead = Phead->next;
}
Phead->next = Head->next->next;
Phead = Head->next;
Head->next = Head->next->next;
Phead->next = NULL;
free(Phead);
return true;
}
// 尾刪
bool CircLList_TailDel(CircLList_t *Head)
{
// 對單向循環(huán)鏈表的首結(jié)點(diǎn)的地址進(jìn)行備份
CircLList_t *phead = Head->next;
// 找到尾結(jié)點(diǎn)的前一個(gè)結(jié)點(diǎn)
while (phead->next->next != Head)
{
if (phead->next->next == Head->next)
{
break;
}
phead = phead->next;
}
phead->next->next = NULL;
free(phead->next);
phead->next = Head->next;
return true;
}
// 指定數(shù)據(jù)的結(jié)點(diǎn)刪除(后刪)
bool CircLList_DestDes(CircLList_t *Head, DataType_t destval)
{
// 對單向循環(huán)鏈表的首結(jié)點(diǎn)的地址進(jìn)行備份
CircLList_t *phead = Head->next;
// 前結(jié)點(diǎn)
CircLList_t *preNode = Head;
// 要?jiǎng)h除的結(jié)點(diǎn)
CircLList_t *delNode = Head->next;
while (phead->next != Head)
{
if (delNode->data == destval)
{
break;
}
preNode = delNode;
delNode = delNode->next;
}
preNode->next = delNode->next;
delNode->next = NULL;
free(delNode);
return true;
}
int main(int argc, char const *argv[])
{
CircLList_t *Head = CircLList_Create();
CircLList_HeadInsert(Head, 40);
CircLList_HeadInsert(Head, 30);
CircLList_HeadInsert(Head, 20);
CircLList_HeadInsert(Head, 10);
CircLList_TailInsert(Head, 50);
CircLList_DestInsert(Head, 30, 15);
CircLList_HeadDel(Head);
CircLList_Print(Head);
CircLList_TailDel(Head);
CircLList_DestDes(Head, 20);
CircLList_Print(Head);
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-856693.html
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)的練習(xí)day2(未完待續(xù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!