双链螺旋链表的头插中插尾插 头删中删尾删
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
/********************************************************************************
*
* 函数名称: CircLList_NewNode
* 函数功能: 创建一个新节点,实现头插中插尾插,头删中删尾删
* 函数参数: None
* 返回结果: true
* 注意事项: None
* 函数作者: [email protected]
* 创建日期: 2024/04/25
* 修改历史:
* 函数版本:
********************************************************************************/
// 指的是双向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造双向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleCirLList
{
DataType_t data; // 结点的数据域
struct DoubleCirLList *prev; // 直接前驱的指针域
struct DoubleCirLList *next; // 直接后继的指针域
} DoubleCirLList_t;
// 创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
DoubleCirLList_t *DoubleCirLList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
DoubleCirLList_t *Head = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
Head->prev = Head;
Head->next = Head;
// 3.把头结点的地址返回即可
return Head;
}
// 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
DoubleCirLList_t *DoubleCirLList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
DoubleCirLList_t *New = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
// 2.对新结点的数据域和指针域(2个)进行初始化,指针域指向结点自身,体现“循环”
New->data = data;
New->prev = New;
New->next = New;
return New;
}
// 头插
bool DoubleCirLList_Headinsert(DoubleCirLList *head, typedef Data)
{
// 对首地址进行备份
DoubleCirLList_t *Phead = head->next;
// 创造一个新节点对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_t * NewNode(data);
if (NULL = New)
{
printf("can not inesrt NewNode\n")
}
// 判断链表是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
Phead->prev->next = New;
New->prev = Phead->prev;
New->next = phead;
Phead->prev = New;
head->next = New;
return turn;
}
// 尾巴插
bool DoubleCirLList_Headinsert(DoubleCirLList *head, typedef Data)
{
// 对首地址进行备份
DoubleCirLList_t *Phead = head->next;
// 创造一个新节点对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_t * NewNode(data);
if (NULL = New)
{
printf("can not inesrt NewNode\n")
}
// 判断链表是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
New->prev = Phead->prev;
Phead->prev->next = New;
New->next = head->next;
head->next->prev = New;
return true;
}
// 中间插(在目标节点后面插入)
bool DoubleCirLList_Headinsert(DoubleCirLList *head, typedef Data)
{
// 对首地址进行备份
DoubleCirLList_t *Phead = head->next;
// 创造一个新节点对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_t * NewNode(data);
if (NULL = New)
{
printf("can not inesrt NewNode\n")
}
// 判断链表是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
// 对链表进行遍历,判断链表中是否有目标值
while (Phead->next)
{
Phead = head->next;
if (Phead->data == Destval)
{
New->next = Phead->next;
Phead->next->prev = New;
New->prev = Phead;
Phead->next = New;
return true;
}
else
{
printf("Destval is no in here\n") return false
}
}
}
// 头删
bool DoubleCirLList_Headinsert(DoubleCirLList *head, typedef Data)
{
// 对首地址进行备份
DoubleCirLList_t *Phead = head->next;
// 创造一个新节点对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_t * NewNode(data);
if (NULL = New)
{
printf("can not inesrt NewNode\n")
}
// 判断链表是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
Phead->prev->next = Phead->next;
Phead->next->prev = Phead->prev;
Phead->prev = Null;
head->next = Phead->next;
Phead->next = NULL;
free(Phead)
reutrn true;
}
// 尾删
bool DoubleCirLList_Headinsert(DoubleCirLList *head, typedef Data)
{
// 对首地址进行备份
DoubleCirLList_t *Phead = head->next;
// 创造一个新节点对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_t * NewNode(data);
if (NULL = New)
{
printf("can not inesrt NewNode\n")
}
// 判断链表是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
Phead->prev->prev = Phead;
Phead->next = Phead->prev->prev;
Phead->next = NULL;
Phead->prev = NULL;
free(NULL) return true;
}
// 中删
bool DoubleCirLList_DestDel(DoubleCirLList_t *Head, DataType_t destval, DatatType_t data)
{
// 备份头结点和首结点
DoubleCirLList_t *phead = Head;
DoubleCirLList_t *p = Head->next;
// 创建一个新的结点,并对新节点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 判断是否为空链表
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
while (phead->next)
{
phead = phead->next;
if (phead->data = destvel)
{
Phead->prev->next = Phead->next;
Phead->next->prev = Phead->prev;
Phead->next = NULL;
phead->prev = Null;
free(Phead) return true;
}
else
{
printf("There is no destval");
return false;
}
}
}
int main()
{
return 0;
}
`
标签:Head,prev,DoubleCirLList,next,链表,Phead,New,删中,删尾
From: https://www.cnblogs.com/fzhyy/p/18157635