單向迴圈連結串列的定義
採用鏈式儲存結構儲存的順序表
定義單向迴圈連結串列型別以及單向迴圈連結串列中的元素型別
定義單向連結串列中的元素型別
//指的是雙向連結串列中的結點有效資料型別,使用者可以根據需要進行修改
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_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{
DoubleLList_t *Phead = Head;
DoubleLList_t *Phead_prev;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next =NULL)
{
Phead->next = New;
New->next =NULL;
New->prev =NULL;
}
while(Phead->data != destval)
{
Phead_prev = Phead;
Phead = Phead->next;
}
New->next = Phead->next;
Phead->next->prev = New;
New->prev = Phead;
Phead->next = New;
return true;
}
- 頭插
//頭插
bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
DoubleLList_t *Phead=Head;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next==NULL)
{
printf("*\n");
Head->next=New;
New->next=NULL;
New->prev=NULL;
return true;
}
printf("#\n");
New->next=Phead->next;
Phead->prev=New;
Head->next=New;
return true;
}
- 尾插
//尾插
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
DoubleLList_t *Phead = Head;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next==NULL)
{
Head->next=New;
New->next=NULL;
New->prev=NULL;
return true;
}
while(Phead->next!=NULL)
{
Phead = Phead->next;
}
Phead->next = New;
New->next = NULL;
New->prev = Phead;
return true;
}
單向迴圈連結串列的刪除
//指定刪除
bool DoubleLList_Del(DoubleLList_t *Head, DataType_t dest)
{
// 對雙連結串列的頭結點的地址進行備份
DoubleLList_t *Phead = Head;
if (Phead == NULL)
{
printf("雙連結串列為空\n");
return false;
}
while (Phead->data != dest)
{
if (Phead->next == NULL)
{
printf("未找到目標節點\n");
return false;
}
Phead = Phead->next;
}
// 找到目標節點
Phead->prev->next = Phead->next;
if (Phead->next != NULL)
{
Phead->next->prev = Phead->prev;
}
// 釋放節點記憶體
free(Phead);
return true;
}
- 頭刪
//頭刪
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
DoubleLList_t *Phead=Head->next;
if(Head->next =NULL)
{
printf("");
return false;
}
Head->next = Phead->next;
Phead->next=NULL;
Phead->prev = NULL;
free(Phead);
return true;
}
-尾刪
//尾刪
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
DoubleLList_t *Phead=Head;
if(Phead->next =NULL)
{
printf("");
return false;
}
while(Phead->next!=NULL)
{
Phead=Phead->next;
}
Phead->prev->next=NULL;
Phead->next = NULL;
free(Phead);
return true;
}
單向迴圈連結串列的遍歷
bool DoubleLList_Print(DoubleLList_t *Head)
{
//對單向迴圈連結串列的頭結點的地址進行備份
DoubleLList_t *Phead = Head;
//判斷當前連結串列是否為空,為空則直接退出
if (Head->next == NULL)
{
printf("current linkeflist is empty!\n");
return false;
}
//從首結點開始遍歷
while(Phead->next)
{
//把頭結點的直接後繼作為新的頭結點
Phead = Phead->next;
//輸出頭結點的直接後繼的資料域
printf("data = %d\n",Phead->data);
//判斷是否到達尾結點,尾結點的next指標是指向首結點的地址
if (Phead->next == NULL)
{
break;
}
}
return true;
}