單向迴圈連結串列介面設計(C語言)

Rice_rice發表於2024-04-24

單向迴圈連結串列介面設計

/**

* @file name: 單向迴圈連結串列介面設計

* @brief :設計單向迴圈連結串列,實現各種功能函式並測試

* @author ni456xinmie@163.com

* @date 2024/04/23

* @version 1.0

* @property

* @note

* CopyRight (c) 2023-2024 ni456xinmie@163.com All Right Reseverd

*/


構造單向迴圈連結串列結構體

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

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

} CircLList_t;

建立一個空單向迴圈連結串列並初始化

CircLList_t *CircLList_Create()
{
	// 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;
	return Head; // 3.把頭結點的地址返回即可
}

建立新的結點,並對新結點進行初始化

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_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;
}


功能函式:從尾部插入新元素

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;
}


功能函式:從指定位置插入新元素

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;
	}
}

功能函式:遍歷列印連結串列

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;
}


功能函式:刪除首節點

bool CircLList_HeadDel(CircLList_t *Head)
{
	// 對單向迴圈連結串列的頭結點的地址進行備份
	CircLList_t *Phead = Head->next;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判斷當前連結串列是否為空,為空則直接退出
	{
		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;
}

功能函式:刪除尾部節點

bool CircLList_TailDel(CircLList_t *Head)
{

	CircLList_t *tmpFormer;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判斷當前連結串列是否為空,為空則直接退出
	{
		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;
}

功能函式:刪除指定位置的節點

bool CircLList_MidDel(CircLList_t *Head, DataType_t destval)
{
	CircLList_t *tmpFormer;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判斷當前連結串列是否為空,為空則直接退出
	{
		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;
	}
}


主函式,呼叫並測試各功能函式

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;
}

相關文章