🖊作者 : 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;
}