C语言带头双向循环链表

带头双向循序链表

        带头双向循环链表是链表中最复杂的结构,同时也是最优的结构,头插头删,尾插尾删的时间复杂度都是O(1)。头也叫哨兵位。程序分模块写,一个头文件DList.h放头文件、结构体声明、函数声明。两个源文件DList.c、test.c。DList.c包好DList.h头文件,实现DList.h声明的函数,即实现顺序表所使用的函数。test.c用来测试函数有没有问题,最后实现目录,测试之前头文件中要写函数的声明,并且test.c文件中要包含头文件DList.h。

结构图 

        节点里有两个指针,一个指向前面的节点,一个指向后面的节点。

        需要什么操作?创建节点、初始化、打印数据、尾部插入、头部插入、尾部删除、头部删除、任意位置插入、任意位置删除、查找数据、修改数据、判断是否为空、链表元素个数、。 通过打印数据检查函数有没有问题。 

DList.h

        头文件、结构体声明、函数声明。

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

typedef int DLDataType;

typedef struct DListNode
{
	DLDataType x;
	struct ListNode* pre;
	struct ListNode* next;
}DListNode;

// 创建节点
DListNode* BuyListNode(DLDataType x);

// 初始化
DListNode* DListInit();

// 尾插
void DListPushBack(DListNode* head, DLDataType x);

// 打印数据
void DListPrint(DListNode* head);

// 头插
void DListPushFront(DListNode* head, DLDataType x);

// 判空
bool DListEmpty(DListNode* head);

// 尾删
void DListPopBack(DListNode* head);

// 头删
void DListPopFront(DListNode* head);

// 查找数据
DListNode* DListSearch(DListNode* head, DLDataType x);

// 修改数据
void DListModify(DListNode* pos, DLDataType x);

// 任意位置插入
void DListInsert(DListNode* pos, DLDataType x);

// 任意位置删除
void DListErase(DListNode* pos);

// 链表元素个数
int DListSize(DListNode* head);

创建节点

DListNode* BuyListNode(DLDataType x)
{
	DListNode* newNode = (DListNode*)malloc(sizeof(DListNode));
	if (newNode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newNode->x = x;
	newNode->pre = newNode->next = NULL;

	return newNode;
}

        链表按需申请节点,写成函数。

初始化

DListNode* DListInit()
{
	DListNode* node = BuyListNode(0);
	node->pre = node;
	node->next = node;

	return node;
}

        创建一个头节点,头节点的两个指针都指向自己。通过返回值接收头节点,可以不用传二级指针。

  

 打印数据

void DListPrint(DListNode* head)
{
	assert(head);

	DListNode* cur = head->next;
	// 当回到头节点的时候结束
	while (cur != head)
	{
		printf("%d ", cur->x);
		cur = cur->next;
	}
	printf("\n");
}

        从头节点的下一个节点开始打印数据,当该节点回头头节点的时候结束。

尾插

void DListPushBack(DListNode* head, DLDataType x)
{
	assert(head);

	DListNode* node = BuyListNode(x);
	DListNode* tail = head->pre;

	tail->next = node;
	node->pre = tail;
	node->next = head;
	head->pre = node;
}

        所有的插入都不需要传二级指针,因为头节点不需要改变。单链表尾插的时候需要对链表为空的情况特殊处理,改变第一个节点;而带头双向循环链表不需要。

尾插测试

#include "DList.h"
void test()
{
	DListNode* head = DListInit();
	DListPushBack(head, 1);
	DListPushBack(head, 2);
	DListPushBack(head, 3);
	DListPushBack(head, 4);
	DListPrint(head);
}
int main()
{
	test();
	return 0;
}

头插

void DListPushFront(DListNode* head, DLDataType x)
{
	assert(head);

	DListNode* node = BuyListNode(x);
	DListNode* next = head->next;

	head->next = node;
	node->pre = head;
	node->next = next;
	next->pre = node;
}

        将新节点插入到头节点和头节点的下一个节点的中间,完成头插。 先存储头节点的下一个节点,方便控制。

判空

bool DListEmpty(DListNode* head)
{
	assert(head);
	return head->next == head;
}

        删除数据的时候,要确保有数据存储在带头双向循环链表中。 

尾删

void DListPopBack(DListNode* head)
{
	assert(head);
	assert(!DListEmpty(head));

	DListNode* tail = head->pre;
	DListNode* tailPre = tail->pre;

	free(tail);
	tailPre->next = head;
	head->pre = tailPre;
}

        记录尾结点的前一个,free释放尾结点,再将尾结点的前一个跟头节点连接上。 尾删不需要像单链表一样考虑是不是只剩下一个数据,不需要改变头节点。

 尾删测试

#include "DList.h"
void test()
{
	DListNode* head = DListInit();
	DListPushBack(head, 1);
	DListPushBack(head, 2);
	DListPushBack(head, 3);
	DListPushBack(head, 4);
	DListPrint(head);
	DListPopBack(head);
	DListPrint(head);
	DListPopBack(head);
	DListPrint(head);
}
int main()
{
	test();
	return 0;
}

头删

void DListPopFront(DListNode* head)
{
	assert(head);
	assert(!DListEmpty(head));

	DListNode* del = head->next;
	DListNode* delNext = del->next;

	free(del);
	head->next = delNext;
	delNext->pre = head;
}

        记录第二个节点,free释放第一个节点,将头节点和第二个节点连接起来。

查找数据

DListNode* DListSearch(DListNode* head, DLDataType x)
{
	assert(head);

	DListNode* cur = head->next;
	while (cur != head)
	{
		if (cur->x == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

        从第一个节点开始查找数据,如果查找到就返回该节点,当节点回到头节点的时候结束,返回空指针NULL。

修改数据

void DListModify(DListNode* pos, DLDataType x)
{
	assert(pos);
	pos->x = x;
}

任意位置插入

void DListInsert(DListNode* pos, DLDataType x)
{
	assert(pos);

	DListNode* node = BuyListNode(x);
	DListNode* pre = pos->pre;

	pre->next = node;
	node->pre = pre;
	node->next = pos;
	pos->pre = node;
}

         记录pos位置的前一个节点,将新节点插入到前一个节点和pos节点中间。

 任意位置插入测试

#include "DList.h"
void test()
{
	DListNode* head = DListInit();
	DListPushBack(head, 1);
	DListPushBack(head, 2);
	DListPushBack(head, 3);
	DListPushBack(head, 4);
	DListPrint(head);
	DListNode* pos = DListSearch(head, 2);
	if (pos != NULL)
	{
		DListInsert(pos, 20);
	}
	DListPrint(head);
}
int main()
{
	test();
	return 0;
}

任意位置删除 

void DListErase(DListNode* pos)
{
	assert(pos);

	DListNode* pre = pos->pre;
	DListNode* next = pos->next;
	
	free(pos);

	pre->next = next;
	next->pre = pre;
}

        记录pos位置的前一个节点和后一个节点,free释放pos位置节点,将前一个节点和后一个节点连接起来。 

任意位置删除测试

#include "DList.h"
void test()
{
	DListNode* head = DListInit();
	DListPushBack(head, 1);
	DListPushBack(head, 2);
	DListPushBack(head, 3);
	DListPushBack(head, 4);
	DListPrint(head);
	DListNode* pos = DListSearch(head, 1);
	if (pos != NULL)
	{
		DListErase(pos);
	}
	DListPrint(head);
}
int main()
{
	test();
	return 0;
}

尾插头插复用任意位置插入

// 尾插
void DListPushBack(DListNode* head, DLDataType x)
{
	assert(head);

	DListInsert(head, x);
}

// 头插
void DListPushFront(DListNode* head, DLDataType x)
{
	assert(head);

	DListInsert(head->next, x);
}

尾删头删复用任意位置删除

// 尾删
void DListPopBack(DListNode* head)
{
	assert(head);
	assert(!DListEmpty(head));

	DListErase(head->pre);
}

// 头删
void DListPopFront(DListNode* head)
{
	assert(head);
	assert(!DListEmpty(head));

	DListErase(head->next);
}

链表元素个数

int DListSize(DListNode* head)
{
	assert(head);

	int size = 0;
	DListNode* cur = head->next;
	while (cur != head)
	{
		++size;
		cur = cur->next;
	}
	return size;
}

带头双向循环链表特点

         带头双向循环链表是链表中最优的结构,结构复杂,一般单独存储数据。存储空间上,物理地址不一定是连续的,逻辑上连续,不支持随机访问。插入和删除数据的时间复杂度是O(1),不需要挪动数据,也不需要遍历找前一个节点。插入数据和删除数据不需要改变头节点,不需要传二级指针。按需申请空间,不需要扩容。缓存命中率低。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值