带头结点的双向链表的基本操作

DList.h

#pragma once
#include<malloc.h>
#include<assert.h>
//带头结点的双向链表
typedef int DateType;

typedef struct DListNode
{
	struct DListNode* pNext;
	struct DListNode* pPre;
	DateType data;
}DLNode, *PDNode;
//DLNode:双向链表结点的类型
//PDNode:操作该结点的指针

void DListInit(PDNode* pHead);//初始化要改变其外部的指针,也就是头结点,所以要传二级指针
void DListPushBack(PDNode pHead, DateType data);//尾插
void DListPopBack(PDNode pHead);//尾删
void DListPushFront(PDNode pHead, DateType data);//头插
void DListPopFront(PDNode pHead);//头删
void DListInsert(PDNode pos, DateType data);//任意位置插入
void DListErase(PDNode pos);//任意位置删除
int DListEmpty(PDNode pHead);//判断链表是否是空链表
int DListSize(PDNode pHead);//求链表有效结点个数
void DListClear(PDNode pHead);//只清空有效的结点,不删除头结点
void DListDestory(PDNode* pHead);//销毁所有结点,包括头结点
PDNode BuyDListNode(DateType data);//构建结点

DList.c

#include "DList.h"

void DListInit(PDNode * pHead)
{
	assert(pHead);
	*pHead = BuyDListNode(0);//创建一个头结点
}

void DListPushBack(PDNode pHead, DateType data)
{
	PDNode pCur = pHead;
	PDNode pNewNode = NULL;
	assert(pHead);
	while (pCur->pNext)//找当前链表最后一个结点
	{
		pCur = pCur->pNext;
	}
	pNewNode = BuyDListNode(data);
	pCur->pNext = pNewNode;
	pNewNode->pPre = pCur;
}

void DListPopBack(PDNode pHead)
{
	PDNode pTailNode = pHead;
	assert(pHead);
	while (pTailNode->pNext)//找当前链表最后一个结点
	{
		pTailNode = pTailNode->pNext;
	}
	if (pTailNode != pHead)//链表有结点
	{
		pTailNode->pPre->pNext = NULL;
		free(pTailNode);
	}
}

void DListPushFront(PDNode pHead, DateType data)
{
	PDNode pNewNode = NULL;
	assert(pHead);
	pNewNode = BuyDListNode(data);
	pNewNode->pNext = pHead->pNext;
	pHead->pNext = pNewNode;
	pNewNode->pPre = pHead;
	if (pNewNode->pNext != NULL)//头插时链表中已经有结点
	{
		pNewNode->pNext->pPre = pNewNode;
	}
}

void DListPopFront(PDNode pHead)
{
	PDNode pDelNode = NULL;
	assert(pHead);
	pDelNode = pHead->pNext;
	if (pDelNode == NULL)//空链表
	{
		return;
	}
	pHead->pNext = pDelNode->pNext;
	if (pDelNode->pNext)//待删结点后还有结点
	{
		pDelNode->pNext->pPre = pHead;
	}
	free(pDelNode);
}

void DListInsert(PDNode pos, DateType data)
{
	PDNode pNewNode = NULL;
	if (pos == NULL || pos->pPre == NULL)
	{
		return;
	}
	pNewNode = BuyDListNode(data);
	pNewNode->pNext = pos;
	pNewNode->pPre = pos->pPre;
	pos->pPre = pNewNode;
	pNewNode->pPre->pNext = pNewNode;
}

void DListErase(PDNode pos)
{
	if (pos == NULL || pos->pPre == NULL)//空位置与头结点位置无法删除
	{
		return;
	}
	pos->pPre->pNext = pos->pNext;
	if (pos->pNext)//pos不是最后一个结点
	{
		pos->pNext->pPre = pos->pPre;
	}
}

int DListEmpty(PDNode pHead)
{
	assert(pHead);
	return NULL == pHead->pNext;
}

int DListSize(PDNode pHead)
{
	PDNode pCur = NULL;
	int count = 0;
	assert(pHead);
	pCur = pHead->pNext;
	while (pCur)
	{
		count++;
		pCur = pCur->pNext;
	}
	return count;
}

void DListClear(PDNode pHead)
{
	PDNode pCur = NULL;
	assert(pHead);
	pCur = pHead->pNext;
	while (pCur)
	{
		pHead->pNext = pCur->pNext;
		free(pCur);
		pCur = pHead->pNext;
	}
}

void DListDestory(PDNode * pHead)
{
	assert(pHead);
	DListClear(*pHead);
	free(*pHead);
	*pHead = NULL;
}

PDNode BuyDListNode(DateType data)
{
	PDNode pNewNode = (PDNode)malloc(sizeof(DLNode));
	if (pNewNode == NULL)//系统空间不足,申请空间失败就直接停止
	{
		assert(0);
		return NULL;
	}
	pNewNode->data = data;
	pNewNode->pNext = NULL;
	pNewNode->pPre = NULL;
	return pNewNode;
}

猜你喜欢

转载自blog.csdn.net/weixin_40995778/article/details/83004335