單向循環(huán)鏈表接口設(shè)計(jì)
/**
* @file name: 單向循環(huán)鏈表接口設(shè)計(jì)
* @brief :設(shè)計(jì)單向循環(huán)鏈表,實(shí)現(xiàn)各種功能函數(shù)并測試
* @author ni456xinmie@163.com
* @date 2024/04/23
* @version 1.0
* @property
* @note
* CopyRight (c) 2023-2024 ni456xinmie@163.com All Right Reseverd文章來源:http://www.zghlxwxcb.cn/news/detail-856717.html
*/文章來源地址http://www.zghlxwxcb.cn/news/detail-856717.html
構(gòu)造單向循環(huán)鏈表結(jié)構(gòu)體
// 指的是單向循環(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;
創(chuàng)建一個(gè)空單向循環(huán)鏈表并初始化
CircLList_t *CircLList_Create()
{
// 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;
return Head; // 3.把頭結(jié)點(diǎn)的地址返回即可
}
創(chuàng)建新的結(jié)點(diǎn),并對新結(jié)點(diǎn)進(jìn)行初始化
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;
}
功能函數(shù):從首節(jié)點(diǎn)進(jìn)行插入元素
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *new = CircLList_NewNode(data);
CircLList_t *tmp = Head->next;
if (Head->next == Head) // empty list
{
Head->next = new;
new->next = new;
return true;
}
while (tmp->next != Head->next) // normal situation,find the last node
tmp = tmp->next;
new->next = Head->next;
Head->next = new;
tmp->next = new;
return true;
}
功能函數(shù):從尾部插入新元素
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *new = CircLList_NewNode(data);
if (Head->next == Head) // judge is the null
{
Head->next = new;
new->next = new;
return true;
}
CircLList_t *tmp;
while (tmp->next != Head->next) // when the normal situation,find the last node
tmp = tmp->next;
tmp->next = new;
new->next = Head->next;
return true;
}
功能函數(shù):從指定位置插入新元素
bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
CircLList_t *tmp = Head->next;
DataType_t i = Head->data;
if (Head->next == Head) // judge the empty list
{
printf("The list is empty");
return false;
}
CircLList_t *new = CircLList_NewNode(data);
while (destval != tmp->data && tmp->next != Head->next)
{
tmp = tmp->next;
}
if (destval == tmp->data)
{
new->next = tmp->next;
tmp->next = new;
return true;
}
else
{
printf("There is no destval\n");
return false;
}
}
功能函數(shù):遍歷打印鏈表
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;
}
功能函數(shù):刪除首節(jié)點(diǎn)
bool CircLList_HeadDel(CircLList_t *Head)
{
// 對單向循環(huán)鏈表的頭結(jié)點(diǎn)的地址進(jìn)行備份
CircLList_t *Phead = Head->next;
CircLList_t *tmp = Head->next;
if (Head->next == Head) // 判斷當(dāng)前鏈表是否為空,為空則直接退出
{
printf("current linkeflist is empty!\n");
return false;
}
while (tmp->next != Head->next) // find the last node
tmp = tmp->next;
tmp->next = Phead->next;
Head->next = Phead->next;
Phead->next = NULL;
free(Phead);
return true;
}
功能函數(shù):刪除尾部節(jié)點(diǎn)
bool CircLList_TailDel(CircLList_t *Head)
{
CircLList_t *tmpFormer;
CircLList_t *tmp = Head->next;
if (Head->next == Head) // 判斷當(dāng)前鏈表是否為空,為空則直接退出
{
printf("current linkeflist is empty!\n");
return false;
}
while (tmp->next != Head->next) // find the last node
{
tmpFormer = tmp;
tmp = tmp->next;
}
tmpFormer->next = Head->next;
tmp->next = NULL;
free(tmp);
return true;
}
功能函數(shù):刪除指定位置的節(jié)點(diǎn)
bool CircLList_MidDel(CircLList_t *Head, DataType_t destval)
{
CircLList_t *tmpFormer;
CircLList_t *tmp = Head->next;
if (Head->next == Head) // 判斷當(dāng)前鏈表是否為空,為空則直接退出
{
printf("current linkeflist is empty!\n");
return false;
}
while (tmp->data != destval && tmp->next != Head->next) // find the specific node
{
tmpFormer = tmp;
tmp = tmp->next;
}
if (tmp->data == destval)
{
tmpFormer->next = tmp->next;
tmp->next = NULL;
free(tmp);
return true;
}
else
{
printf("The is no destival\n");
return false;
}
}
主函數(shù),調(diào)用并測試各功能函數(shù)
int main()
{
CircLList_t *H = CircLList_Create();
CircLList_HeadInsert(H, 10);
CircLList_HeadInsert(H, 20);
CircLList_HeadInsert(H, 30);
CircLList_TailInsert(H, 30);
CircLList_TailInsert(H, 40);
CircLList_DestInsert(H, 30, 15);
CircLList_Print(H);
puts("");
CircLList_HeadDel(H);
CircLList_Print(H);
puts("");
CircLList_TailDel(H);
CircLList_Print(H);
puts("");
CircLList_MidDel(H, 12);
CircLList_Print(H);
return 0;
}
到了這里,關(guān)于單向循環(huán)鏈表接口設(shè)計(jì)(C語言)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!