【初学数据结构】双向链表

大家好呀!👋这个是付青云同学的博客!😁
目前一直在学习C语言。🐸
写博客是为了来记录我的学习过程,同时也希望通过博客能够帮助到需要帮助的人。
如果我的博客可以帮助到你,不妨给我一个关注哦😁

前面实现了单链表

双向链表

声明部分

#pragma once

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

typedef int LTDateType;

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

//初始化
LTNode* ListInit(LTNode* phead);
//创建新节点
LTNode* BuyListNode(LTDateType x);
//打印
void ListPrint(LTNode* phead);
//尾插
void ListPushBack(LTNode* phead, LTDateType x);
//尾删
void ListPopBack(LTNode* phead);
//头插
void ListPushFront(LTNode* phead, LTDateType x);
//头删
void ListPopFront(LTNode* phead);
//查找
LTNode* ListFind(LTNode* phead, LTDateType x);
//x位置之后插入
void ListInsertBack(LTNode* pos, LTDateType x);
//x位置之前插入
void ListInsertFront(LTNode* pos, LTDateType x);
//删除
void ListErast(LTNode* pos);
//删除所有链表
void ListDestroy(LTNode* phead);

函数实现部分

#define _CRT_SECURE_NO_WARNINGS

#include "List.h"


LTNode* BuyListNode(LTDateType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	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;
	}
}

LTNode* ListInit(LTNode* phead)
{
	//哨兵位头节点
	phead = (LTNode*)malloc(sizeof(LTNode));
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

void ListPushBack(LTNode* phead, LTDateType x)
{
	/*assert(phead);

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

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

	ListInsertFront(phead, x);


}

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

	free(tail);
	tail = NULL; */

	ListErast(phead->prev);

}

void ListPushFront(LTNode* phead, LTDateType x)
{
	/*assert(phead);

	LTNode* cur = phead->next;
	LTNode* newhead = BuyListNode(x);

	newhead->prev = phead;
	newhead->next = cur;
	phead->next =newhead;*/

	ListInsertBack(phead, x);

}

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

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

	ListErast(phead->next);

}

LTNode* ListFind(LTNode* phead, LTDateType x)
{
	assert(phead);

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

void ListInsertBack(LTNode* pos, LTDateType x)
{
	assert(pos);

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

}

void ListInsertFront(LTNode* pos, LTDateType x)
{
	assert(pos);

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

}

void ListErast(LTNode* pos)
{
	assert(pos);
	assert(pos != pos->next);

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

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

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

总结

单链表与双向链表

虽说都是链表,在结构上双向链表看起来比单链表复杂了,但是实现起来它比单链表方便多了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

付青云同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值