数据结构双链表的实现

#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkList.h"

//申请空间
LTNode* BuyLTNode(LTtypeData x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror("malloc ");
		exit(-1);

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

	
	return newnode;

}

//打印测试链表
void Print(LTNode* phead)
{
	/*assert(phead);
	LTNode* cur = phead->next;
	printf("print ");
	while (cur != phead);
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");*/

	assert(phead);

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

//初始化链表
LTNode* ListInit()
{
	LTNode* phead = BuyLTNode(-1);
	
	phead->next = phead;
	phead->prev = phead;
	
	return phead;
}

//链表的尾插
void LTPushBack(LTNode* phead, LTtypeData x)
{
	/*assert(phead);
	LTNode* newnode = BuyLTNode(x);
	LTNode* tail = phead->prev;

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

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


	/*assert(phead);

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

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

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

	LTInsert(phead, x);
	
}
//链表的尾删除
void LTPopBack(LTNode* phead)
{
	/*assert(phead);
	assert(phead->next != phead);

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

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

	LTErase(phead->prev);
}
//链表的头插
void LTPushFront(LTNode* phead, LTtypeData x)
{
	/*assert(phead);
	LTNode* tail = phead->next;
	LTNode* newnode = BuyLTNode(x);

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

	newnode->prev = phead;
	phead->next = newnode;*/
	
	LTInsert( phead->next, x);
}

//链表的头删除
void LTPopFront(LTNode* phead)
{
	/*assert(phead);

	LTNode* first = phead->next;
	phead->next = first->next;
	first->next->prev = phead;*/

	LTErase(phead->next);
}
//链表数据查找
LTNode* LTFind(LTNode* phead, LTtypeData x)
{
	assert(phead);
	LTNode* cur = phead;
	while (cur->next != cur )
	{
		if (x == cur->data)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
//链表的指定位置插入
void LTInsert(LTNode* pos, LTtypeData x)
{
	assert(pos);
	LTNode* first = pos->prev;
	LTNode* newnode = BuyLTNode(x);

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

	pos->prev = newnode;
	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);
}
//判断链表是否为空
bool LTEmpty(LTNode* phead)
{
	/*if (phead->next == phead)
		return true;
	else
		return false;*/

	return phead->next == phead;
}

//计算链表的长度
size_t LTSize(LTNode* phead)
{
	LTNode* cur = phead;
	size_t count = 0;
	while (phead != cur->next)
	{
		count++;
		cur = cur->next;
	}
	return count;
}
//释放链表
void LTDestroy(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		
		LTNode* next = cur->next;

		free(cur);
		cur = next;
	}
	free(phead);
	
}

 今天我学习了数据结构中的双链表,他非常的简单,对数据的操作也是非常的简便。双链表虽然看起来挺复杂,但是实现起来估计比单链表还简单。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Though even

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

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

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

打赏作者

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

抵扣说明:

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

余额充值