数据结构之带头结点的双向循环链表(含全部代码)

带头结点的双向循环链表的实现

带头结点的双向循环链表和单链表相比具有许多的优点,增删查改更加的便捷,时间复杂度均为O(1)
头文件创建

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

定义数据类型以及创建链表结点

typedef int DataType;
typedef struct ListNode
{
	struct ListNode* prev;
	struct ListNode* next;
	DataType data;
}LN;

所需要实现的函数

//初始化
LN* ListInit();
//创建结点
LN* BuyListNode(DataType x);
//销毁链表
void ListDestory(LN* phead);
//打印链表
void ListPrint(LN* phead);
//头插
void ListPushFront(LN* phead, DataType x);
//尾插
void ListPushBack(LN* phead, DataType x);
//头删
void ListPopFront(LN* phead);
//尾删
void ListPopBack(LN* phead);
//查找x并返回x结点位置
LN* ListFind(LN* phead, DataType x);
//在pos前插入x
void ListInsert(LN* pos, DataType x);
//删除pos处的元素
void ListErase(LN* pos);
//判断链表是否为空
bool ListEmpty(LN* phead);
//计算元素个数
int ListSize(LN* phead);

函数的实现

#include"DList.h"

//初始化
LN* ListInit()
{
	LN* phead = BuyListNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
//创建结点
LN* BuyListNode(DataType x)
{
	LN* newnode = (LN*)malloc(sizeof(LN));
	if (newnode == NULL)
	{
		printf("malloc fail");
		exit(-1);
	}
	else
	{
		newnode->data = x;
		newnode->prev = NULL;
		newnode->next = NULL;
		return newnode;
	}
}
//销毁链表
void ListDestory(LN* phead)
{
	assert(phead);
	LN* cur = phead->next;
	while (cur != phead)
	{
		LN* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}
//打印链表
void ListPrint(LN* phead)
{
	assert(phead);
	LN* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
//头插
void ListPushFront(LN* phead, DataType x)
{
	assert(phead);
	LN* newnode = BuyListNode(x);
	LN* first = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = newnode;
}
//尾插
void ListPushBack(LN* phead, DataType x)
{
	assert(phead);
	LN* tail = phead->prev;
	LN* newnode = BuyListNode(x);

	tail->next = newnode;
	newnode->prev = tail;
	phead->prev = newnode;
	newnode->next = phead;
}
//头删
void ListPopFront(LN* phead)
{
	assert(phead);
	assert(phead->next != phead);
	LN* first = phead->next;

	phead->next = first->next;
	first->next->prev = phead;
	free(first);
	first = NULL;
}
//尾删
void ListPopBack(LN* phead)
{
	assert(phead);
	assert(phead->next != phead);
	LN* tail = phead->prev;

	//phead->prev = tail->prev;
	tail->prev->next = phead;
	phead->prev = tail->prev;
	free(tail);
	tail = NULL;
}
//查找x并返回x结点位置
LN* ListFind(LN* phead, DataType x)
{
	assert(phead);
	LN* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
//在pos前插入x
void ListInsert(LN* pos, DataType x)
{
	assert(pos);
	LN* newnode = BuyListNode(x);

	LN* prev = pos->prev;
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}
//删除pos处的元素
void ListErase(LN* pos)
{
	assert(pos);
	LN* prev = pos->prev;
	LN* next = pos->next;

	prev->next = next;
	next->prev = prev;

	free(pos);
	pos = NULL;
}
//判断链表是否为空
bool ListEmpty(LN* phead)
{
	if (phead == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//计算元素个数
int ListSize(LN* phead)
{
	int size = 0;
	assert(phead);
	LN* cur = phead->next;
	while (cur != phead)
	{
		size++;
		cur = cur->next;
	}
	return size;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求知.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值