带头节点循环双向链表

DHCList.h

#pragma once

typedef int DLDataType;
typedef struct DHCListNode
{
	DLDataType data;
	struct DHCListNode* next;//指向当前节点的下一个节点
	struct DHCListNode* prev;//指向当前节点的前一个节点
}DHCLNode;

DHCLNode* DHCListInit();//初始化
void DHCListPushBack(DHCLNode* pHead, DLDataType data);//尾插
void DHCListPopBack(DHCLNode* pHead);//尾删
void DHCListPushFront(DHCLNode* pHead, DLDataType data);//头插
void DHCListPopFront(DHCLNode* pHead);//头删
void DHCListInsrt(DHCLNode* pos, DLDataType data);//任意位置插入
void DHCListErase(DHCLNode* pos);//任意位置删除
DHCLNode* DHCListFind(DHCLNode* pHead, DLDataType data);//寻找某个节点
int DHCListSize(DHCLNode* pHead);//检测一共多少有效节点
int DHCListEmoty(DHCLNode* pHead);//判空----链表不存在与链表为空不相同
//注意:只是将链表中的有效节点删除,不删除头
void DHCListClear(DHCLNode* pHead);//清空
//需要经头节点以及所有的有效节点全部删除
void DHCListDestroy(DHCLNode** pHead);//销毁

DHCList.c

#include "DHCList.h"
#include<malloc.h>
#include<assert.h>
#include<stdio.h>
DHCLNode* BuyDHCListNode(DLDataType data)
{
	DHCLNode* newNode = (DHCLNode*)malloc(sizeof(DHCLNode));
	if (NULL == newNode)
	{
		assert(0);
		return NULL;
	}

	newNode->data = data;
	newNode->next = NULL;
	newNode->prev = NULL;

	return newNode;
}

//只需将头节点申请好
DHCLNode* DHCListInit()//初始化
{
	DHCLNode* head = BuyDHCListNode(0);
	head->next = head;
	head->prev = head;
	return head;
}

void DHCListPushBack(DHCLNode* pHead, DLDataType data)//尾插
{
	assert(pHead);
	DHCListInsrt(pHead, data);
}

void DHCListPopBack(DHCLNode* pHead)//尾删
{
	if (DHCListEmoty(pHead))
		return;

	DHCListEmoty(pHead->prev);
}

void DHCListPushFront(DHCLNode* pHead, DLDataType data)//头插
{
	assert(pHead);
	DHCListInsrt(pHead->next, data);
}

void DHCListPopFront(DHCLNode* pHead)//头删
{
	if (DHCListEmoty(pHead))
		return;

	DHCListEmoty(pHead->next);

}

void DHCListInsrt(DHCLNode* pos, DLDataType data)//任意位置插入
{
	DHCLNode* newNode;
	if (NULL == pos)
		return;

	//1.申请新节点
    newNode = BuyDHCListNode(data);

	//2.先将新节点链接到双向链表中
	newNode->next = pos;
	newNode->prev = pos->prev;

	//3.断开新链表,擦汗如新节点
	pos->prev = newNode;
	newNode->prev->next = newNode;
}
void DHCListErase(DHCLNode* pos)//任意位置删除
{
	if (NULL == pos)
		return;

	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
}

DHCLNode* DHCListFind(DHCLNode* pHead, DLDataType data)//寻找某个节点
{
	assert(pHead);
	DHCLNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (data == cur->data)
			return cur;

		cur = cur->next;
	}
	return NULL;
}
int DHCListSize(DHCLNode* pHead)//检测一共多少有效节点
{
	assert(pHead);
	DHCLNode* cur = pHead->next;
	int count = 0;
	while (cur != pHead)
	{
		++count;
		cur=cur->next;
	}
	return count;
}

int DHCListEmoty(DHCLNode* pHead)//判空----链表不存在与链表为空不相同
{
	assert(pHead);
	return pHead->next == pHead;
}

//注意:只是将链表中的有效节点删除,不删除头
void DHCListClear(DHCLNode* pHead)//清空
{
	assert(pHead);
	DHCLNode* cur = pHead->next;

	while (cur != pHead)
	{
		pHead->next = cur->next;
		free(cur);
		cur = pHead->next;
	}

	//链表已经为空
	pHead->next = pHead;
	pHead->prev = pHead;
}

//需要经头节点以及所有的有效节点全部删除
void DHCListDestroy(DHCLNode** pHead)//销毁
{
	assert(pHead);
	DHCListClear(*pHead);
	free(*pHead);
	*pHead = NULL;
}

void PrintDHCList(DHCLNode* head)
{
	DHCLNode* cur = head->next;
	while (cur != head)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

void TestDHCList()
{
	DHCLNode* head = DHCListInit();
	DHCListPushBack(head, 1);
	DHCListPushBack(head, 2);
	DHCListPushBack(head, 3);
	DHCListPushBack(head, 4);
	DHCListPushBack(head, 5);
	PrintDHCList(head);

	DHCListPushFront(head, 0);
	PrintDHCList(head);

	DHCListPopBack(head);
	DHCListPopFront(head);
	PrintDHCList(head);

	DHCListInsrt(DHCListFind(head, 3), 100);
	PrintDHCList(head);

	DHCListErase(DHCListFind(head, 3));
	PrintDHCList(head);

	DHCListDestroy(&head);	

}

test.c

#include"DHCList.h"
int main()
{
	TestDHCList();
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值