栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

前面实现了单链表

文章目录
  • 双向链表
    • 声明部分
    • 函数实现部分
  • 总结
    • 单链表与双向链表

双向链表 声明部分
#pragma once

#include 
#include 
#include 

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)
{
	

	ListInsertFront(phead, x);


}

void ListPopBack(LTNode* phead)
{
	

	ListErast(phead->prev);

}

void ListPushFront(LTNode* phead, LTDateType x)
{
	

	ListInsertBack(phead, x);

}

void ListPopFront(LTNode* phead)
{
	

	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);
}

总结 单链表与双向链表

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号