#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;
}DoubleLList_t;
DoubleLList_t * DoubleLList_Create(void)
{
DoubleLList_t *Head = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
Head->prev = NULL;
Head->next = NULL;
return Head;
}
DoubleLList_t * DoubleLList_NewNode(DataType_t data)
{
DoubleLList_t *New = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
New->data = data;
New->prev = NULL;
New->next = NULL;
return New;
}
bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
DoubleLList_t *Phead=Head;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next==NULL)
{
Head->next=New;
return true;
}
New->next=Phead->next;
Phead->next->prev=New;
Head->next=New;
return true;
}
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
DoubleLList_t *Phead = Head;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
if(Phead->next==NULL)
{
Head->next=New;
return true;
}
while(Phead->next!=NULL)
{
Phead = Phead->next;
}
Phead->next = New;
New->prev = Phead;
return true;
}
bool DoubleLList_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{
DoubleLList_t *Phead = Head;
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
while(Phead->data != destval)
{
if(Phead->next==NULL)
{
printf("dest node is not found\n");
return false;
}
Phead = Phead->next;
}
if(Phead->next ==NULL)
{
New->prev = Phead;
Phead->next = New;
}
else
{
New->next = Phead->next;
New->prev = Phead;
Phead->next = New;
New->next->prev = New;
}
return true;
}
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
DoubleLList_t *Phead=Head->next;
if(Head->next =NULL)
{
printf("双链表为空\n");
return false;
}
Head->next = Phead->next;
Phead->next=NULL;
Phead->prev = NULL;
free(Phead);
return true;
}
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
DoubleLList_t *Phead=Head;
if(Head->next==NULL)
{
printf("双链表为空\n");
return false;
}
while(Phead->next!=NULL)
{
Phead=Phead->next;
}
if(Head->next==Phead)
{
Head->next = NULL;
}
else
{
Phead->prev->next=NULL;
Phead->prev = NULL;
}
free(Phead);
return true;
}
bool DoubleLList_Del(DoubleLList_t *Head, DataType_t dest)
{
DoubleLList_t *Phead = Head;
if (Phead == NULL)
{
printf("双链表为空\n");
return false;
}
while (Phead->data != dest)
{
if (Phead->next == NULL)
{
printf("未找到目标节点\n");
return false;
}
Phead = Phead->next;
}
if (Phead == Head->next)
{
Head->next=Phead->next;
if(Phead->next!=NULL)
{
Phead->next->prev=NULL;
Phead->next = NULL;
}
}
else if(Phead->next == NULL)
{
Phead->prev->next = NULL;
Phead->prev = NULL;
}
else
{
Phead->next->prev=Phead->prev;
Phead->prev->next=Phead->next;
Phead->prev = NULL;
Phead->next = NULL;
}
free(Phead);
return true;
}
bool DoubleLList_Print(DoubleLList_t *Head)
{
DoubleLList_t *Phead = Head;
if (Head->next == NULL)
{
printf("current linkeflist is empty!\n");
return false;
}
while(Phead->next)
{
Phead = Phead->next;
printf("data = %d\n",Phead->data);
if (Phead->next == NULL)
{
break;
}
}
return true;
}
int main(int argc, char const *argv[])
{
DoubleLList_t * Manager = DoubleLList_Create();
printf("*********************************尾插********************************\n");
DoubleLList_TailInsert(Manager,5);
DoubleLList_TailInsert(Manager,2);
DoubleLList_TailInsert(Manager,1);
DoubleLList_TailInsert(Manager,4);
DoubleLList_TailInsert(Manager,6);
DoubleLList_Print(Manager);
printf("\n");
printf("*********************************头插********************************\n");
DoubleLList_HeadInsert(Manager,9);
DoubleLList_HeadInsert(Manager,7);
DoubleLList_HeadInsert(Manager,8);
DoubleLList_HeadInsert(Manager,0);
DoubleLList_HeadInsert(Manager,10);
DoubleLList_Print(Manager);
printf("\n");
printf("*********************************头删********************************\n");
DoubleLList_HeadDel(Manager);
DoubleLList_HeadDel(Manager);
DoubleLList_HeadDel(Manager);
DoubleLList_HeadDel(Manager);
DoubleLList_HeadDel(Manager);
DoubleLList_Print(Manager);
printf("*********************************指定插入********************************\n");
DoubleLList_DestInsert(Manager,5,1);
DoubleLList_DestInsert(Manager,2,3);
DoubleLList_DestInsert(Manager,6,7);
DoubleLList_Print(Manager);
printf("\n");
printf("*********************************指定删除********************************\n");
DoubleLList_Del(Manager,5);
DoubleLList_Del(Manager,7);
DoubleLList_Del(Manager,3);
DoubleLList_Print(Manager);
printf("\n");
printf("*********************************尾删********************************\n");
DoubleLList_TailDel(Manager);
DoubleLList_TailDel(Manager);
DoubleLList_TailDel(Manager);
DoubleLList_TailDel(Manager);
DoubleLList_Print(Manager);
printf("\n");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步