#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int DataType_t;
typedef struct DoubleLinkedList
{
DataType_t data;
struct DoubleLinkedList *prev;
struct DoubleLinkedList *next;
}DoubleCirLList_t;
DoubleCirLList_t * DoubleCirLList_Create(void)
{
DoubleCirLList_t *Head = (DoubleCirLList_t *)calloc(1,sizeof(DoubleCirLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
Head->prev = Head;
Head->next = Head;
return Head;
}
DoubleCirLList_t * DoubleCirLList_NewNode(DataType_t data)
{
DoubleCirLList_t *New = (DoubleCirLList_t *)calloc(1,sizeof(DoubleCirLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
New->data = data;
New->prev = New;
New->next = New;
return New;
}
bool DoubleCirLList_HeadInsert(DoubleCirLList_t *Head,DataType_t data)
{
DoubleCirLList_t *Phead=Head->next;
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next==Head)
{
Head->next=New;
return true;
}
New->prev=Phead->prev;
New->next=Phead;
Phead->prev->next=New;
Phead->prev=New;
Head->next=New;
return true;
}
bool DoubleCirLList_tailInsert(DoubleCirLList_t *Head,DataType_t data)
{
DoubleCirLList_t *Phead = Head->next;
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return false;
}
if(Phead==Head)
{
Head->next=New;
return true;
}
New->prev = Phead->prev;
New->next = Phead;
Phead->prev->next = New;
Phead->prev=New;
return true;
}
bool DoubleCirLList_DestInsert(DoubleCirLList_t *Head,DataType_t destval,DataType_t data)
{
DoubleCirLList_t *Phead = Head->next;
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead==Head)
{
Head->next=New;
return true;
}
while(Phead->data != destval)
{
if(Phead->next==Head->next)
{
printf("dest node is not found\n");
return false;
}
Phead = Phead->next;
}
New->next=Phead->next;
New->prev=Phead;
Phead->next->prev=New;
Phead->next=New;
return true;
}
bool DoubleCirLList_HeadDel(DoubleCirLList_t *Head)
{
DoubleCirLList_t *Phead=Head->next;
if(Head==Phead)
{
printf("双链表为空\n");
return false;
}
if(Phead->next==Phead)
{
Head->next=Head;
}
else
{
Head->next = Phead->next;
Phead->prev->next=Phead->next;
Phead->next->prev=Phead->prev;
}
Phead->next=NULL;
Phead->prev = NULL;
free(Phead);
return true;
}
bool DoubleCirLList_TailDel(DoubleCirLList_t *Head)
{
DoubleCirLList_t *Phead=Head->next;
DoubleCirLList_t *Bhead=Phead->prev;
if(Head==Phead)
{
printf("双链表为空\n");
return false;
}
if(Phead==Head)
{
Head->next=Head;
}
else
{
Bhead->prev->next=Phead;
Phead->prev=Bhead->prev;
}
Bhead->next=NULL;
Bhead->prev = NULL;
free(Bhead);
return true;
}
bool DoubleCirLList_Del(DoubleCirLList_t *Head, DataType_t dest)
{
DoubleCirLList_t *Phead = Head->next;
if (Phead == NULL)
{
printf("指定双链表为空\n");
return false;
}
while (Phead->data != dest)
{
if (Phead->next == Head->next)
{
printf("指删未找到目标节点\n");
return false;
}
Phead = Phead->next;
}
if(Phead->next==Phead)
{
Head->next=Head;
}
else
{
if (Phead == Head->next)
{
Head->next=Phead->next;
}
Phead->prev->next=Phead->next;
Phead->next->prev=Phead->prev;
}
Phead->next = NULL;
Phead->prev = NULL;
free(Phead);
return true;
}
bool DoubleCirLList_Print(DoubleCirLList_t *Head)
{
DoubleCirLList_t *Phead = Head;
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
while(Phead->next)
{
Phead = Phead->next;
printf("data = %d\n",Phead->data);
if (Phead->next == Head->next)
{
break;
}
}
return true;
}
int main(int argc, char const *argv[])
{
DoubleCirLList_t * Manager = DoubleCirLList_Create();
printf("*********************************尾插********************************\n");
DoubleCirLList_tailInsert(Manager,5);
DoubleCirLList_tailInsert(Manager,2);
DoubleCirLList_tailInsert(Manager,1);
DoubleCirLList_tailInsert(Manager,4);
DoubleCirLList_tailInsert(Manager,6);
DoubleCirLList_Print(Manager);
printf("\n");
printf("*********************************头插********************************\n");
DoubleCirLList_HeadInsert(Manager,9);
DoubleCirLList_HeadInsert(Manager,7);
DoubleCirLList_HeadInsert(Manager,8);
DoubleCirLList_HeadInsert(Manager,0);
DoubleCirLList_HeadInsert(Manager,10);
DoubleCirLList_Print(Manager);
printf("\n");
printf("*********************************头删********************************\n");
DoubleCirLList_HeadDel(Manager);
DoubleCirLList_HeadDel(Manager);
DoubleCirLList_HeadDel(Manager);
DoubleCirLList_HeadDel(Manager);
DoubleCirLList_HeadDel(Manager);
DoubleCirLList_Print(Manager);
printf("*********************************指定插入********************************\n");
DoubleCirLList_DestInsert(Manager,5,1);
DoubleCirLList_DestInsert(Manager,2,3);
DoubleCirLList_DestInsert(Manager,6,7);
DoubleCirLList_Print(Manager);
printf("\n");
printf("*********************************指定删除********************************\n");
DoubleCirLList_Del(Manager,5);
DoubleCirLList_Del(Manager,7);
DoubleCirLList_Del(Manager,3);
DoubleCirLList_Print(Manager);
printf("\n");
printf("*********************************尾删********************************\n");
DoubleCirLList_TailDel(Manager);
DoubleCirLList_TailDel(Manager);
DoubleCirLList_TailDel(Manager);
DoubleCirLList_TailDel(Manager);
DoubleCirLList_Print(Manager);
printf("\n");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术