双向循环链表

🖊作者 : Djx_hmbb
📘专栏 : 数据结构
😆今日分享 : 婚礼传统的黑暗起源:
订婚戒指最早是罗马人用戒指来表明对女方的所有权的;女方伴娘穿着相似是为了让前来袭击的恶灵分辨不出谁是新娘;伴郎是新郎的护卫,当新郎因为女方家不同意婚事而必须“绑架”新娘时,伴郎会陪伴着他以抵挡女方家的反击;新郎抱着新娘跨门槛有着新娘不情愿离开娘家的象征,如果新娘太急切会显得失礼;新娘的捧花是用来掩盖其体味的;新娘的父亲亲手将女儿交给丈夫,只是因为婚姻曾是一种好比资产移交的交易。
请添加图片描述

✔头文件声明:

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

#define Datatype int 
typedef struct pqSeqlist
{
	struct pqSeqlist* next;
	struct pqSeqlist* prev;
	int data;
}SL;

//申请一个结点
SL* BuyNode(Datatype x);

//初始化
SL* InitList();

//打印
void PrintSL(SL* phead);

//尾插
void PushBack(SL* phead,Datatype x);

//尾删
void PopBack(SL* phead);

//头插
void PushFront(SL* phead, Datatype x);

//头删
void PopFront(SL* phead);

🔎基本功能实现:

  • 创建一个结点
  • 初始化
  • 打印
  • 尾插
  • 尾删
  • 头插
  • 头删
  • 找到x的位置pos
  • 在pos位置插入一个数
  • 删除pos位置
#define _CRT_SECURE_NO_WARNINGS
#include"pqSeqlist.h"

//申请一个结点
SL* BuyNode(Datatype x)
{
	SL* node = (SL*)malloc(sizeof(SL));
	if (node == NULL)
	{
	   perror("malloc fail:");
	   exit(-1);
	}
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
}

//初始化
SL* InitList()
{
	SL* phead = BuyNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

//打印
void PrintSL(SL* phead)
{
	assert(phead);
	SL* cur = phead->next;

	while (cur != phead)
	{
		printf("%d ",cur->data);
		cur = cur->next;
	}
	puts("");
}

//尾插
void PushBack(SL* phead, Datatype x)
{
	assert(phead);//断言:判断phead是否为空
	
	SL* newNode = BuyNode(x);
	SL* tail = phead->prev;

	//尾插  
	tail->next = newNode;
	newNode->next = phead;
	phead->prev = newNode;
	newNode->prev = tail;

}

//尾删
void PopBack(SL* phead)
{
	assert(phead);//断言:判断phead是否为空

	SL* tail = phead->prev;
	SL* tailprev = tail->prev;
	
	//把尾结点断掉
	phead->prev = tailprev;
	tailprev->next = phead;
	
	free(tail);//释放尾结点

}


//头插
void PushFront(SL* phead, Datatype x)
{
	assert(phead);//断言:判断phead是否为空

	SL* newNode = BuyNode(x);
	SL* head = phead->next;

	//头插  
	phead->next = newNode;
	newNode->next = head;
	head->prev = newNode;
	newNode->prev = phead;

}

//头删
void PopFront(SL* phead)
{
	assert(phead);//断言:判断phead是否为空
	assert(phead->next);
	SL* head = phead->next;
	SL* headnext = head->prev;

	//把头结点断掉
	phead->next = headnext;
	headnext->prev = phead;

	free(head);//释放头结点

}

//找到x并返回pos位置
SL* FindX(SL* phead, Datatype x)
{
	assert(phead);
	assert(phead->next);

	SL* cur = phead->next;
	//未找到,后移
	while (cur != phead)
	{
		if (cur->data != x)
			cur = cur->next;
		//找到后返回
		else return cur;
	}
	//未找到,返回空
	return NULL;
}

//删除pos位置
void PopPos(SL* phead, SL* pos)
{
	assert(pos);

	SL* nex = pos->next;
	SL* pre = pos->prev;

	pre->next = nex;
	nex->prev = pre;

	free(pos);//释放掉pos位置

}

//在pos位置插入一个数
void PushPos(SL* phead, SL* pos, Datatype x)
{
	assert(pos);

	SL* newNode = BuyNode(x);
	SL* pre = pos->prev;
	
	pre->next = newNode;
	newNode->prev = pre;
	newNode->next = pos;
	pos->prev = newNode;

}

✔测试文件:

#include"pqSeqlist.h"

test01()
{
	SL* head = InitList();
	PushBack(head, 1);
	PushBack(head, 2);
	PushBack(head, 3);
	PushBack(head, 4);
	PrintSL(head);

	PopBack(head);
	PopBack(head);
	PopBack(head);
	PrintSL(head);

}

test02()
{
	SL* head = InitList();
	PushFront(head, 1);
	PushFront(head, 2);
	PushFront(head, 3);
	PushFront(head, 4);
	PrintSL(head);
}
//双向循环单链表
main()
{
	//test01();
	test02();

	system("pause");
	return 0;
}
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

D. Star.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值