/******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
/********************************************************************************************************
*
*
* 設計雙向鏈表的介面
*
*
*
* Copyright (c) 2023-2024 [email protected] All right Reserved
* ******************************************************************************************************/
//指的是雙向鏈表中的結點有效數據類型,用戶可以根據需要進行修改
typedef int DataType_t;
//構造雙向鏈表的結點,鏈表中所有結點的數據類型應該是相同的
typedef struct DoubleLinkedList
{
DataType_t data; //結點的數據域
struct DoubleLinkedList *prev; //直接前驅的指針域
struct DoubleLinkedList *next; //直接後繼的指針域
}DoubleLList_t;
//創建一個空雙向鏈表,空鏈表應該有一個頭結點,對鏈表進行初始化
DoubleLList_t * DoubleLList_Create(void)
{
//1.創建一個頭結點並對頭結點申請記憶體
DoubleLList_t *Head = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
//2.對頭結點進行初始化,頭結點是不存儲數據域,指針域指向NULL
Head->prev = NULL;
Head->next = NULL;
//3.把頭結點的地址返回即可
return Head;
}
//創建新的結點,並對新結點進行初始化(數據域 + 指針域)
DoubleLList_t * DoubleLList_NewNode(DataType_t data)
{
//1.創建一個新結點並對新結點申請記憶體
DoubleLList_t *New = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
//2.對新結點的數據域和指針域(2個)進行初始化
New->data = data;
New->prev = NULL;
New->next = NULL;
return New;
}
//頭插
bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
// 1.創建新的結點,並對新結點進行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3.如果雙向鏈表為非空,把新結點插入到鏈表的頭部\
New->next = Head->next;
Head->next->prev = New;
Head->next = New;
return true;
}
//尾插
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
// 1.創建新的結點,並對新結點進行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3.如果雙向鏈表為非空,遍歷鏈表,找到尾結點
DoubleLList_t *Tail = Head;
while(Tail->next != NULL)
{
Tail = Tail->next;
}
// 4.把新結點插入到鏈表的尾部
Tail->next = New;
New->prev = Tail;
return true;
}
//指定插
bool DoubleLList_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{
// 1.創建新的結點,並對新結點進行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3.如果雙向鏈表為非空,遍歷鏈表,找到插入位置
DoubleLList_t *Dest = Head->next;
while (Dest->data != destval && Dest != NULL)
{
Dest = Dest->next;
}
if (NULL == Dest)
{
return false;
}
// 4.說明找到目標結點,則把新結點插入到目標結點的後面
New->next = Dest->next;
Dest->next->prev = New;
New->prev = Dest;
Dest->next = New;
return true;
}
//頭刪
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
// 1.判斷鏈表是否為空,如果為空,則直接退出
if (NULL == Head->next)
{
return false;
}
// 2.對鏈表的首結點的地址進行備份
DoubleLList_t *Temp = Head->next;
// 3.鏈表是非空的,則直接刪除首結點
Head->next = Temp->next;
Temp->next = NULL;
Temp->next->prev = NULL;
free(Temp);
return true;
}
// 尾刪
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
// 1.判斷鏈表是否為空,如果為空,則直接退出
if (NULL == Head->next)
{
return false;
}
// 2.遍歷鏈表,找到尾結點
DoubleLList_t *Tail = Head;
while (Tail->next != NULL)
{
Tail = Tail->next;
}
// 3.刪除尾結點
Tail->prev->next = NULL;
Tail->prev = NULL;
free(Tail);
return true;
}
// 指定刪
bool DoubleLList_DestDel(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
// 1.判斷鏈表是否為空,如果為空,則直接退出
if (NULL == Head->next)
{
return false;
}
// 2.鏈表是非空的,遍歷鏈表,找到待刪除結點
DoubleLList_t *Dest = Head;
while (Dest->data != destval && Dest != NULL)
{
Dest = Dest->next;
}
if (NULL == Dest)
{
return false;
}
// 3.刪除指定結點
Dest->prev->next = Dest->next;
Dest->next->prev = Dest->prev;
Dest->prev = NULL;
Dest->next = NULL;
free(Dest);
return true;
}
//遍歷鏈表
bool DoubleLList_Print(DoubleLList_t *Head)
{
//對雙向鏈表的頭結點的地址進行備份
DoubleLList_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;
}
int main(int argc, char const *argv[])
{
return 0;
}