啊~~~~双链表~~~~和操作

前面我记得说过,链表分为单双链表,还分为循环和不循环的,然后还分为带头节点的和不带头结点的

接下来的相关操作都是带头循环双链表的

为什么是带头循环双链表,因为操作简单

首先做个笔记

typedef struct ListNode {
	int data;
	struct ListNode *pNodeNext;
}Node,*pNode;

这里{}后面的Node是把struct ListNode重命名成Node,而",“后面的*pNode我原来一直以为是创建一个新的变量,最后跑过去问了一下大佬,大佬说这是把” *“重命名成”*pNode",这样定义指针可以分清楚

下来是双链表的操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "D.h"
void DListInit(PDLNode* pHead) {
	assert(pHead);
	*pHead = (PDLNode)malloc(sizeof(DLNode));
	if (NULL == *pHead) {
		assert(0);
		return;
	}
	(*pHead)->pNext = *pHead;
	(*pHead)->pPer = *pHead;
}

PDLNode BuyNewNode(DLDataType data) {
	PDLNode pNewNode = (PDLNode)malloc(sizeof(DLNode));
	if (NULL == pNewNode) {
		assert(0);
		return;
	}
	pNewNode->pNext = NULL;
	pNewNode->pPer = NULL;
}
//尾插
void DListPushBack(PDLNode pHead, DLDataType data) {
	PDLNode pNewNode = BuyNewNode(data);
	pNewNode->pNext = pHead;
	pNewNode->pPer = pHead->pPer;
	pHead->pPer->pNext = pNewNode;
	pHead->pPer = pNewNode;
}
//尾删
void DListPopBack(PDLNode pHead) {
	assert(pHead);
	if (pHead == pHead->pNext) {
		return;
	}
	PDLNode pDelNode = pHead->pPer;
	pDelNode->pPer->pNext = pHead;
	pHead->pPer = pDelNode->pPer;
	free(pDelNode);
}
//头插
void DListPushFront(PDLNode pHead, DLDataType data) {
	PDLNode pNewNode = BuyNewNode(data);
	pNewNode->pNext = pHead->pNext;
	pNewNode->pPer = pHead;
	pHead->pNext->pPer = pNewNode;
	pHead->pNext = pNewNode;

}
//头删
void DListPopFront(PDLNode pHead) {
	assert(pHead);
	if (pHead->pNext == pHead) {
		assert(0);
		return;
	}
	PDLNode pDelNode = pHead->pNext;
	pHead->pNext = pDelNode->pNext;
	pDelNode->pNext->pPer = pHead;
	free(pDelNode);

}
//pos插入
void DListInsert(PDLNode pos, DLDataType data) {
	if (NULL == pos) {
		return;
	}
	PDLNode pNewNode = BuyNewNode(data);
	pNewNode->pPer = pos;
	pNewNode->pNext = pos->pNext;
	pos->pNext->pPer = pNewNode;
	pos->pNext = pNewNode;
}
//删除pos的节点
void DListErase(PDLNode pos) {
	if (NULL == pos) {
		return;
	}
	pos->pNext->pPer = pos->pPer;
	pos->pPer->pNext = pos->pNext;
	free(pos);
}
//清空
void DListClear(PDLNode pHead) {
	PDLNode Pcur = pHead->pNext;
	while (Pcur != pHead) {
		pHead->pNext = Pcur->pNext;
		free(Pcur);
		Pcur = Pcur->pNext;
	}
	pHead->pNext = pHead;
	pHead->pPer = pHead;
}
//销毁
void DListDestroy(PDLNode* pHead) {
	DListClear(*pHead);
	free(*pHead);
	*pHead = NULL;
}
int main() {


	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nihuhui666/article/details/89843104