双向循环链表的代码和实现

今天,学习了双向循环链表,以下是链表的增删改查的功能的代码以及实现的截图

函数头文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdbool.h>
#include <assert.h>
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;
	struct ListNode* _next;
	struct ListNode* _prev;
}ListNode;

// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
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);

双线链表的主文件

#include "双向链表.h"

void test1(void)
{
	ListNode* pHead = ListCreate();
	ListPushBack(pHead, 1);
	ListPushBack(pHead, 2);
	ListPushBack(pHead, 3);
	ListPushBack(pHead, 5);
	ListPushBack(pHead, 6);
	ListPushBack(pHead, 7);
	ListPushBack(pHead, 8);
	ListPrint(pHead);

	ListPopBack(pHead);
	ListPopBack(pHead);
	ListPopBack(pHead);
	ListPopBack(pHead);
	ListPopBack(pHead);
	//ListPopBack(pHead);
	//ListPopBack(pHead);
	//ListPopBack(pHead);
	ListPrint(pHead);

	ListPushFront(pHead, 8);
	ListPushFront(pHead, 9);
	ListPushFront(pHead, 10);
	ListPrint(pHead);

	ListPopFront(pHead);
	ListPopFront(pHead);
	ListPrint(pHead);

	ListInsert(ListFind(pHead, 1), 30);
	ListPrint(pHead);

	ListDestory(pHead);
	pHead = NULL;
}
int main(void)
{
	test1();
	return 0;
}

函数功能的实现文件

// 创建返回链表的头结点.
#include "双向链表.h"
//函数功能:创建一个头节点
ListNode* ListCreate()
{
	ListNode* head = (ListNode*)calloc(sizeof(ListNode), 1);
	if (head == NULL)
	{
		perror("内存分配错误");
	}
	head->_next = head;
	head->_prev = head;
	return head;
}
//函数功能:创建一个双向循环链表的节点
ListNode* BuyNode(LTDataType x)
{
	ListNode* newNode = (ListNode*)calloc(sizeof(ListNode), 1);
	if (newNode == NULL)
	{
		perror("内存分配错误:");
	}
	else
	{
		newNode->_data = x;
		newNode->_next = NULL;
		newNode->_prev = NULL;
		return newNode;
	}
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		printf("%d <==> ",cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newNode = BuyNode(x);
	ListNode* tail = pHead->_prev;
	newNode->_next = pHead;
	newNode->_prev = tail;
	tail->_next = newNode;
	pHead->_prev = newNode;
}

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->_next != pHead);
	ListNode* tail = pHead->_prev;
	ListNode* tail_prev = tail->_prev;
	tail_prev->_next = pHead;
	pHead->_prev = tail_prev;
	free(tail);
	tail = NULL;
}

// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newNode = BuyNode(x);
	ListNode* pHead_Next = pHead->_next;
	newNode->_next = pHead_Next;
	newNode->_prev = pHead;
	pHead->_next = newNode;
	pHead_Next->_prev = newNode;
}

// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->_next != pHead);
	ListNode* del = pHead->_next;
	ListNode* next = del->_next;
	pHead->_next = next;
	next->_prev = pHead;
	free(del);
	del = NULL;
}

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

// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	
	ListNode* newNode = BuyNode(x);
	ListNode* pos_prev = pos->_prev;
	newNode->_next = pos;
	newNode->_prev = pos_prev;
	pos_prev->_next = newNode;
	pos->_prev = newNode;
}

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	assert(pos);
	assert(pos->_next != pos);
	ListNode* pos_pre = pos->_prev;
	ListNode* pos_next = pos->_next;
	pos_pre->_next = pos_next;
	pos_next->_prev = pos_pre;
	free(pos);
	pos = NULL;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值