【数据结构】双向链表

目录

双向链表

        头文件

        源文件


双向链表

带头双向循环链表

        头文件

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>


typedef int LTDataType;

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

LTNode* ListInit();
void ListPushBack(LTNode* phead, LTDataType x);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPrint(LTNode* phead);
void ListPopBack(LTNode* phead);
void ListPopFront(LTNode* phead);
LTNode* ListFind(LTNode* phead, LTDataType x);
void ListInsert(LTNode* pos, LTDataType x);
void ListErase(LTNode* pos);
void ListDestroy(LTNode* phead);

        源文件

#include"List.h"

LTNode* ListInit()//初始化链表,返回哨兵位头结点
{
	LTNode* plist = (LTNode*)malloc(sizeof(LTNode));
	assert(plist);
	plist->prev = plist;
	plist->next = plist;
	return plist;
}

void ListPrint(LTNode* phead)//打印链表
{
	assert(phead);

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

void ListDestroy(LTNode* phead)//释放链表
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* del = cur;
		cur = cur->next;
		free(del);
		del = NULL;
	}
	free(phead);
	//由于参数为一级指针,内部改变不了外部,所以 phead 需要在函数外置空
}

LTNode* BuyLTNode(LTDataType x)//创建新节点
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	assert(newnode);
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->val = x;
	return newnode;
}

void ListPushFront(LTNode* phead, LTDataType x)//头插
{
	assert(phead);

	LTNode* newnode = BuyLTNode(x);

	//当链表内无数据
	if (phead->next == phead)
	{
		phead->next = newnode;
		phead->prev = newnode;
		newnode->next = phead;
		newnode->prev = phead;
	}
	//当链表内有数据
	else
	{
		LTNode* next = phead->next;
		phead->next = newnode;
		newnode->prev = phead;
		newnode->next = next;
		next->prev = newnode;
	}
}

void ListPushBack(LTNode* phead, LTDataType x)//尾插
{
	assert(phead);
	
	LTNode* newnode = BuyLTNode(x);

	//当链表内无数据
	if (phead->next == phead)
	{
		phead->next = newnode;
		phead->prev = newnode;
		newnode->next = phead;
		newnode->prev = phead;
	}
	//当链表内有数据
	else
	{
		LTNode* end = phead->prev;
		end->next = newnode;
		newnode->prev = end;
		newnode->next = phead;
		phead->prev = newnode;
	}
}

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

	//当链表内只有 1 个数据
	if (phead->next->next == phead)
	{
		LTNode* del = phead->next;
		phead->next = phead;
		phead->prev = phead;
		free(del);
		del = NULL;
	}
	//当链表内有多个数据时
	else
	{
		LTNode* del = phead->next;
		LTNode* next = phead->next->next;
		phead->next = next;
		free(del);
		del = NULL;
	}
}

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

	//当链表内只有 1 个数据
	if (phead->next->next == phead)
	{
		LTNode* del = phead->next;
		phead->next = phead;
		phead->prev = phead;
		free(del);
		del = NULL;
	}
	//当链表内有多个数据时
	else
	{
		LTNode* del = phead->prev;
		LTNode* prev = del->prev;
		phead->prev = prev;
		prev->next = phead;
		free(del);
		del = NULL;
	}
}

LTNode* ListFind(LTNode* phead, LTDataType x)//查找指定数据,返回节点,未找到返回空
{
	assert(phead);
	assert(phead->next != phead);
	
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->val == x)
			return cur;

		cur = cur->next;
	}
	return NULL;
}

void ListInsert(LTNode* phead, LTNode* pos, LTDataType x)//在 pos 前插入指定数据
{
	assert(phead);
	assert(phead->next != phead);
	assert(pos);

	LTNode* newnode = BuyLTNode(x);

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

void ListErase(LTNode* phead, LTNode* pos)//删除 pos 节点
{
	assert(phead);
	assert(phead->next != phead);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值