【数据结构】双链表

理论

在这里插入图片描述
8种

sss

经典例题:复杂链表的复制

链表的插入时间复杂度O(1),链表不能随机访问,数组通过下标可以。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

代码:

DST.h

#pragma once

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

typedef int DataType;
typedef struct ST
{
	struct ST* prev;
	struct ST* next;
	DataType data;
}LTNode;

void DstInit(LTNode* pc);//初始化
void DstPrint(LTNode* pc);//遍历
void DstPushFront(LTNode* pc,DataType x);//头插
void DstPushBack(LTNode* pc, DataType x);//尾插
void DstPopBack(LTNode* pc);	 //尾删
void DstPopFront(LTNode* pc);	 //头删
bool EmptyDst(LTNode* pc);		//链表是否为空
int DstSize(LTNode* pc);		//表长

void DstInsert(LTNode* pos, DataType x); //在pos位置之前插入x
void DstErase(LTNode* pos);					//删除pos位置节点

DST.c

#include"DST.h"

//BuyNewNode(DataType x)  不对外开放
LTNode* BuyNewNode(DataType x)
{
	LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
	if (!newNode)
	{
		perror("BuyNewNode:malloc fail");
		return NULL;
	}
	newNode->data = x;
	newNode->prev = NULL;
	newNode->next = NULL;
	return newNode;
}

void DstInit(LTNode* pc)
{
	pc->next = pc;
	pc->prev = pc;
}

void DstPushFront(LTNode* pc, DataType x)
{
	assert(pc);
	//LTNode* newNode = BuyNewNode(x);
	//if (!newNode)
	//{
	//	perror("DstPushFront");
	//	return;
	//}
	//newNode->next = pc->next;
	//newNode->prev = pc;
	//pc->next->prev = newNode;
	//pc->next = newNode;

	DstInsert(pc->next, x);
	return;
}

void DstPushBack(LTNode* pc, DataType x)
{
	assert(pc);
	//LTNode* newNode = BuyNewNode(x);
	//if (!newNode)
	//{
	//	perror("DstPushBack");
	//	return;
	//}
	//newNode->prev = pc->prev;
	//newNode->next = pc;
	//pc->prev->next = newNode;
	//pc->prev = newNode;

	DstInsert(pc, x);
	return;
}

void DstPrint(LTNode* pc)
{
	assert(pc);
	if (EmptyDst(pc))
	{
		printf("链表为空!\n");
		return;
	}
	LTNode* Head = pc->next;
	while (pc != Head)
	{
		printf("%d ", Head->data);
		Head = Head->next;
	}
}

void DstPopBack(LTNode* pc)
{
	assert(pc);
	if (EmptyDst(pc))
	{
		printf("链表为空!\n");
		return;
	}
	//LTNode* pop = pc->prev;
	//pc->prev->prev->next = pc;
	//pc->prev = pc->prev->prev;
	//free(pop);

	DstErase(pc->prev);
	return;
}
void DstPopFront(LTNode* pc)
{
	assert(pc);
	if (EmptyDst(pc))
	{
		printf("链表为空!\n");
		return;
	}
	//LTNode* pop = pc->next;
	//pc->next->next->prev = pc;
	//pc->next = pc->next->next;
	//free(pop);

	DstErase(pc->next);
	return;
}

bool EmptyDst(LTNode* pc)
{
	assert(pc);
	return pc == pc->next ? true : false;
}

int DstSize(LTNode* pc)
{
	assert(pc);
	if (EmptyDst(pc))
	{
		return 0;
	}
	int i = 0;
	LTNode* Head = pc->next;
	while (pc != Head)
	{
		i++;
		Head = Head->next;
	}
	return i;
}

//在pos位置之前插入x
void DstInsert(LTNode* pos, DataType x)
{
	assert(pos);  
	LTNode* prev = pos->prev;
	LTNode* newNode = BuyNewNode(x);
	prev->next = newNode;
	newNode->prev = prev;
	newNode->next = pos;
	pos->prev = newNode;
}
void DstErase(LTNode* pos)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
	free(pos);
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include"DST.h"
void test2()
{
	LTNode list;
	DstInit(&list);
	DstPushFront(&list, 5);
	DstPrint(&list);
	DstPopFront(&list);
	DstPrint(&list);
}

void test1()
{
	LTNode list;
	DstInit(&list);
	DstPushFront(&list, 5);
	DstPushFront(&list, 8);
	DstPushBack(&list, 1);
	DstPushBack(&list, 2);
	DstPushBack(&list, 3);
	DstPushBack(&list, 4);
	DstPushBack(&list, 5);
	printf("\n******************");
	DstPrint(&list);
	printf("\n******************");
	DstPushFront(&list, 5);
	DstPrint(&list);
	printf("\n******************");
	DstPopBack(&list);
	DstPrint(&list);

	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	printf("\n******************");
	DstPopFront(&list);
	DstPrint(&list);
	return;
}
int main()
{
	test2();
	test1();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值