單鏈迴圈連結串列(初版

Eon4051發表於2024-04-24

單向迴圈連結串列的定義

採用鏈式儲存結構儲存的順序表

定義單向迴圈連結串列型別以及單向迴圈連結串列中的元素型別

定義單向連結串列中的元素型別

//指的是單向連結串列中的結點有效資料型別,使用者可以根據需要進行修改
typedef int  DataType_t;

定義單向連結串列型別

//構造單向迴圈連結串列的結點,連結串列中所有結點的資料型別應該是相同的
typedef struct CircularLinkedList
{
	DataType_t  		 data; //結點的資料域
	struct LinkedList	*next; //結點的指標域

}CircLList_t;

單向迴圈連結串列的初始化

//建立一個空單向迴圈連結串列,空連結串列應該有一個頭結點,對連結串列進行初始化
CircLList_t * CircLList_creat(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_DestInsert(CircLList_t *Head,DataType_t destval,DataType_t data)
{

	CircLList_t *Phead = Head->next;
	CircLList_t *Phead_prev = Head;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}
	while(Phead->data != destval)
	{
		Phead = Phead->next;
		Phead_prev = Phead_prev->next;
	}

	New->next = Phead->next;
	Phead_prev->next = New;
	Phead->next = NULL;
	free(Phead);
}

- 頭插

bool CircLList_HeadInsert(CircLList_t *Head,DataType_t data)
{
	
	CircLList_t *Phead=Head->next;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	while(Phead->next!=Head->next)
	{
		Phead=Phead->next;
	}
	New->next = Head->next;
	Phead->next=New;
	Head->next = New;
}

- 尾插

//尾插
bool CircLList_TailInsert(CircLList_t *Head,DataType_t data)
{
	CircLList_t *Phead = Head->next;
	CircLList_t *Phead_prev = Head;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	if(Phead==Head)
	{
		Head->next=New;
		New->next=New;
		return true;
	}

	while(Phead->next!=Head->next)
	{
		Phead = Phead->next;
		Phead_prev = Phead_prev->next;
	}

	New->next = Phead->next;
	Phead->next = New;
}

單向迴圈連結串列的刪除

bool CircLList_Del(CircLList_t *Head,DataType_t dest)
{
	//對單向迴圈連結串列的頭結點的地址進行備份
	CircLList_t *Phead=Head->next;
	CircLList_t *Phead_prev=Head;
	
	while(Phead->data!=dest)
	{
		Phead=Phead->next;
		Phead_prev=Phead_prev->next;
	}
	Phead_prev->next=Phead->next;
	Phead->next=NULL;
	free(Phead);
}

- 頭刪

//頭刪
bool CircLList_HeadDel(CircLList_t *Head)
{
	//對單向迴圈連結串列的頭結點的地址進行備份
	CircLList_t *Phead=Head->next;
	CircLList_t *First=Head->next;
	
	while(Phead->next!=Head->next)
	{
		Phead=Phead->next;
	}
	Head->next=First->next;
	Phead->next=Head->next;
	First->next=NULL;
	free(First);
}

單向迴圈連結串列的遍歷

//遍歷連結串列
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;
}

相關文章