循环链表 -- c语言实现

cpp 复制代码
#pragma once
// 带头+双向+循环链表增删查改实现
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>

typedef int LTDataType;

typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;

//双链表申请一个新节点
ListNode* BuyListNode(LTDataType x);

// 创建返回链表的头结点
ListNode* ListCreate();

// 双向链表打印
void ListPrint(ListNode* pHead);

// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);

// 双向链表尾删
void ListPopBack(ListNode* pHead);

// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);

// 双向链表头删
void ListPopFront(ListNode* pHead);

// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);

// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);

// 双向链表销毁
void ListDestory(ListNode* pHead);

//双链表申请一个新节点
ListNode* BuyListNode(LTDataType x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	if (node == NULL)
	{
		perror("BuyListNode::malloc");
		return NULL;
	}
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}

// 创建返回链表的头结点
ListNode* ListCreate()
{
	ListNode* head = BuyListNode(-1);
	head->next = head;
	head->prev = head;
}

// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	printf("head<=>");
	while (cur != pHead)
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
	return;
}

// 双向链表尾插
// void ListPushBack(ListNode* pHead, LTDataType x)
// {
// 	assert(pHead);
// 	ListNode* newnode = BuyListNode(x);
// 	ListNode* tail = pHead->prev;
// 	tail->next = newnode;
// 	newnode->prev = tail;
// 	newnode->next = pHead;
// 	pHead->prev = newnode;
// 	return;
// }
// 下面的写法更容易理解
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newnode = BuyListNode(x);
	newnode->next = pHead;
	newnode->prev = pHead->prev;
	pHead->prev->next = newnode;
	pHead->prev = newnode;
	return;

}

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	ListNode* tail = pHead->prev;
	if (tail == pHead)
		return;
	tail->prev->next = pHead;
	pHead->prev = tail->prev;
	free(tail);
	tail = NULL;
	return;
}

// 双向链表头插
// void ListPushFront(ListNode* pHead, LTDataType x)
// {
// 	assert(pHead);
// 	ListNode* newnode = BuyListNode(x);
// 	newnode->next = pHead->next;
// 	pHead->next->prev = newnode;
// 	pHead->next = newnode;
// 	newnode->prev = pHead;
// 	return;
// }

void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newnode = BuyListNode(x);
	newnode->next = pHead->next;
	newnode->prev = pHead;
	pHead->next->prev = newnode;
	pHead->next = newnode;
	return;
}

// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	ListNode* tmp = pHead->next;
	if (tmp == pHead)
		return;
	tmp->next->prev = pHead;
	pHead->next = tmp->next;
	free(tmp);
	tmp = NULL;
}

// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* cur = pHead;
	while (cur->data != x)
	{
		cur = cur->next;
		if (cur == pHead)
			return NULL;
	}
	return cur;
}

// 双向链表在pos的前面进行插入
// void ListInsert(ListNode* pos, LTDataType x)
// {
// 	assert(pos);
// 	ListNode* newnode = BuyListNode(x);
// 	pos->prev->next = newnode;
// 	newnode->prev = pos->prev;
// 	pos->prev = newnode;
// 	newnode->next = pos;
// 	return;
// }
// 下面这个更好理解,且避免了pos为头节点时若头节点的 next 为 NULL 时的错误
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newnode = BuyListNode(x);

	newnode->next = pos;
	newnode->prev = pos->prev;
	pos->prev->next = newnode;
	pos->prev = newnode;
	return;
}

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	assert(pos);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
	return;
}

// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		ListNode* tmp = cur->next;
		free(cur);
		cur = tmp;
	}
	free(pHead);
	return;
}

int main() {
	// 创建一个双向循环链表,头节点head
	ListNode* myList = ListCreate();

	// 尾插元素
	ListPushBack(myList, 1);
	ListPushBack(myList, 2);
	ListPushBack(myList, 3);

	// 头插元素
	ListPushFront(myList, 0);

	// 打印链表
	ListPrint(myList); // 应该输出:head<=>0<=>1<=>2<=>3<=>

	// 查找元素
	ListNode* foundNode = ListFind(myList, 2);
	if (foundNode != NULL) {
		printf("找到的节点的data: %d\n", foundNode->data); // 应该输出:Found node with value: 2
	} else {
		printf("没有找到\n");
	}

	// 在指定位置前插入元素
	ListInsert(foundNode, 10);

	// 打印链表
	ListPrint(myList); // 应该输出:head<=>0<=>1<=>10<=>2<=>3<=>

	// 删除指定位置的元素
	ListErase(foundNode);

	// 打印链表
	ListPrint(myList); // 应该输出:head<=>0<=>1<=>10<=>3<=>

	// 头删元素
	ListPopFront(myList);

	// 尾删元素
	ListPopBack(myList);

	// 打印链表
	ListPrint(myList); // 应该输出:head<=>1<=>2<=>
	
	// 打印 head 的 prev 的 data
    printf("head->prev->data: %d", myList->prev->data);

	// 销毁链表
	ListDestory(myList);

    return 0;
}

运行结果:

相关推荐
【心态好不摆烂】8 小时前
数据结构基础精讲3
数据结构·算法
岑梓铭8 小时前
《考研408数据结构》第七章(B树 和 B+树)复习笔记
数据结构·笔记·b树·考研·408
岑梓铭14 小时前
《考研408数据结构》第八章(排序)复习笔记
数据结构·笔记
another heaven14 小时前
【C/C++ 第一次跨越(上):用 C++11/14 停止制造新的旧代码】
c语言·c++·制造
another heaven1 天前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
Code_Solitude1 天前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
Zwawa2 天前
让屏幕动起来:OLED动画效果,丝滑光标、帧动画和一只贪吃蛇
c语言·单片机·嵌入式
Pniubi2 天前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
nice_lcj5202 天前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂2 天前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法