《数据结构初阶》之双向带头链表

功能实现部分

#pragma once

#include "List.h"

//初始化双向带头链表
LTNode* ListInit()
{
	LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
	phead->next = phead;
	phead->prev = phead;

	return phead;

}


//malloc一个新节点
LTNode* BuyListNode(LTDataType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));

	if (newnode == NULL)
	{
		printf("malloc");
		exit(-1);
	}

	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	
	return newnode;

}


//打印双向带头链表
void ListPrint(LTNode* phead)
{
	assert(phead);

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

}


//头插
void ListPushFront(LTNode* phead, LTDataType x)
{
	//LTNode* newnode = BuyListNode(x);
	//LTNode* next = phead->next;

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

	//也可使用指定节点前插入节点的办法
	ListInsert(phead->next, x);

}


//尾插
void ListPushBack(LTNode* phead, LTDataType x)
{
	/*LTNode* newnode = BuyListNode(x);
	LTNode* tail = phead->prev;

	tail->next = newnode;
	newnode->prev = tail;
	phead->prev = newnode;
	newnode->next = phead;*/

	//也可使用指定位置前插入的方法
	ListInsert(phead, x);

}


//头删
void ListPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	/*LTNode* next = phead->next;
	phead->next = next->next;
	next->next->prev = phead;
	free(next); 
	next = NULL;*/

	//使用指定节点删除的方法
	ListErase(phead->next)

}


//尾删
void ListPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->prev != phead);

	/*LTNode* tail = phead->prev;
	tail->prev->next = phead;
	phead->prev= tail->prev;
	free(tail);
	tail = NULL;*/

	//使用指定位置删除的方法
	ListErase(phead->next);

}


//寻找特定节点
LTNode* ListFind(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 ListInsert(LTNode* pos, LTDataType x)
{
	assert(pos);

	LTNode* newnode = BuyListNode(x);
	LTNode* posPrev = pos->prev;
	newnode->prev = posPrev;
	posPrev->next = newnode;
	newnode->next = pos;
	pos->prev = newnode;

}


//删除指定节点
void ListErase(LTNode* pos)
{
	assert(pos);

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

}


//销毁双向带头链表
void ListDestroy(LTNode* phead)
{
	assert(phead);

	LTNode* cur = phead;
	while (cur != phead)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
	cur = NULL;

}




头文件部分

#pragma once

#include
#include
#include

typedef int LTDataType;

typedef struct ListNode
{
	LTDataType data;
	struct ListNode*  prev;
	struct ListNode* next;
}LTNode;

LTNode* ListInit();
void ListPrint(LTNode* phead);
LTNode* BuyListNode(LTDataType x);

void ListPushBack(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopFront(LTNode* phead);

LTNode* ListFind(LTNode* phead, LTDataType x);

void ListInsert(LTNode* pos, LTDataType x);
void ListErase(LTNode* pos);
void ListDestroy(LTNode* phead);

测试部分

#pragma once

#include"List.h"

void TestList1()
{
	LTNode* plist = ListInit();
	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPrint(plist);

	ListPopFront(plist);
	ListPopFront(plist);


    ListPopBack(plist);
	ListPrint(plist);
}

void TestList2()
{
	LTNode* plist = ListInit();
	ListPushFront(plist, 1);
	ListPushFront(plist, 2);
	ListPushFront(plist, 3);
	ListPushFront(plist, 4);
	ListPrint(plist);

	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPrint(plist);

	LTNode* pos = ListFind(plist, 2);
	if (pos)
	{
		ListErase(pos);
	}
	ListPrint(plist);

	ListPopBack(plist);
	ListPopBack(plist);
	ListPopFront(plist);
	ListPopFront(plist);
	ListPrint(plist);

	ListDestroy(plist);
	plist = NULL;
}

int main()
{
	//TestList1();
	TestList2();
	return 0;
}

本文到此结束,感谢大家的阅读,欢迎点赞评论互关,祝大家万事如意。

你可能感兴趣的:(《数据结构初阶》,链表,数据结构,算法)