【数据结构】双向链表

尾插图解

中间插入图解

List.h代码

复制代码
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int LTDataType;

typedef struct ListNode
{
	struct ListNode* next;
	struct ListNode* prev;//头节点

	LTDataType data;
}LTNode;

LTNode*LTInit();
void LTDestroy(LTNode* phead);
LTNode* LTFind(LTNode* phead, LTDataType x);

void LTPushFront(LTNode* phead, LTDataType x);
void LTPopFront(LTNode* phead);
void LTPushBack(LTNode* phead, LTDataType x);
void LTPopBack(LTNode* phead);
void LTInsert(LTNode* pos, LTDataType x);//在pos位置之前插入一个值
void LTErase(LTNode* pos);//删除pos

void LTPrint(LTNode* phead);
bool LTEmpty(LTNode* phead);//判断一个双向链表是否为空(删除时要判断)

List.c代码

复制代码
#include "List.h"

LTNode* BuyListNode(LTDataType x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	node->next = NULL;
	node->prev = NULL;
	node->data = x;
	return node;
}


void LTPrint(LTNode* phead)
{
	assert(phead);

	printf("head<=>");
	LTNode* cur = phead->next;
	while (cur !=phead)
	{
		printf("%d<=>", cur->data);
		cur=cur->next;
	}
	printf("\n");
}


bool LTEmpty(LTNode* phead)
{
	assert(phead);
	return phead->next == phead;
}


LTNode* LTInit()
{
	LTNode* phead = BuyListNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}


void LTDestroy(LTNode* phead)
{
	assert(phead);
	LTNode* cru = phead->next;
	while (cru != phead)
	{
		LTNode* next = cru->next;
		free(cru);
		cru = next;
	}
	free(phead);
}


LTNode* LTFind(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}


void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* newnode = BuyListNode(x);
	LTNode* first = phead->next;

	newnode->prev = phead;
	newnode->next = first;
	phead->next = newnode;
	first->prev = newnode;

}


void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(!LTEmpty(phead));

	LTNode* pphead = phead->next;
	LTNode* first = pphead->next;

	phead->next = first;
	first->prev = phead;
	free(pphead);
	pphead = NULL;
}


void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);

	LTNode* newnode = BuyListNode(x);
	LTNode* tail = phead->prev;

	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
	tail->next = newnode;
}


void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(!LTEmpty(phead));

	LTNode* tail = phead->prev;
	LTNode* tailPrev = tail->prev;

	tailPrev->next = phead;
	phead->prev = tailPrev;
	free(tail);
	tail = NULL;
}

void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* newnode = BuyListNode(x);

	prev->next = newnode;
	pos->prev = newnode;
	newnode->prev = prev;
	newnode->next = pos;
}


void LTErase(LTNode* pos)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;

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

Test.c代码

复制代码
#include "List.h"

void TestList1()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 1);
	LTPushBack(plist, 2);
	LTPushBack(plist, 3);
	LTPushBack(plist, 4);
	LTPrint(plist);

	LTPopBack(plist);
	LTPrint(plist);

	LTPushFront(plist, 0);
	LTPushFront(plist, 1);
	LTPushFront(plist, 2);
	LTPushFront(plist, 3);
	LTPrint(plist);

	LTPopFront(plist);
	LTPrint(plist);

}

TestList2()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 1);
	LTPushBack(plist, 2);
	LTPushBack(plist, 3);
	LTPushBack(plist, 4);
	LTPrint(plist);

	LTNode* pos = LTFind(plist, 3);
	if (pos)
	{
		/*LTErase(pos);
		pos = NULL;*/
		LTInsert(pos, 10);
	}
	LTPrint(plist);
}

TestList3()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 1);
	LTPushBack(plist, 2);
	LTPushBack(plist, 3);
	LTPushBack(plist, 4);
	LTPrint(plist);
	LTDestroy(plist);
	plist = NULL;
}
int main()
{
	//TestList1();
	//TestList2();
	TestList3();
	return 0;
}
相关推荐
xiaoye-duck1 天前
计数排序:高效非比较排序解析
数据结构
稚辉君.MCA_P8_Java1 天前
通义 插入排序(Insertion Sort)
数据结构·后端·算法·架构·排序算法
无限进步_1 天前
C语言动态内存的二维抽象:用malloc实现灵活的多维数组
c语言·开发语言·数据结构·git·算法·github·visual studio
Swift社区1 天前
LeetCode 432 - 全 O(1) 的数据结构
数据结构·算法·leetcode
芬加达1 天前
leetcode34
java·数据结构·算法
leoufung1 天前
链表题目讲解 —— 删除链表的倒数第 n 个节点(LeetCode 19)
数据结构·leetcode·链表
dragoooon341 天前
[优选算法专题八.分治-归并 ——NO.46~48 归并排序 、数组中的逆序对、计算右侧小于当前元素的个数]
数据结构·算法·排序算法·分治
招摇的一半月亮1 天前
P2242 公路维修问题
数据结构·c++·算法
JHC0000001 天前
交换链表中的节点
数据结构·链表