链表学习记录
设计单向循环链表的接口
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int DataType_t;
typedef struct CircularLinkedList
{
DataType_t data;
struct CircularLinkedList *next;
}CircLList_t;
CircLList_t * CircLList_Create(void)
{
CircLList_t *Head = (CircLList_t *)calloc(1,sizeof(CircLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
Head->next = Head;
return Head;
}
CircLList_t * CircLList_NewNode(DataType_t data)
{
CircLList_t* New = (CircLList_t *)calloc(1,sizeof(CircLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
New->data = data;
New->next = NULL;
return New;
}
bool CircLList_HeadInsert(CircLList_t *Head,DataType_t data)
{
CircLList_t* New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
Head->next = New;
New->next = New;
return true;
}
CircLList_t* Phead = Head;
while (Phead->next)
{
Phead = Phead->next;
if (Head->next == Phead->next )
{
break;
}
}
New->next = Head->next;
Head->next = New;
Phead->next = New;
return true;
}
bool CircLList_TailInsert(CircLList_t *Head,DataType_t data)
{
CircLList_t* New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
Head->next = New;
New->next = New;
return true;
}
CircLList_t* Phead = Head;
while (Pheaad->next)
{
Phead = Phead->next;
if (Head->next == Phead->next)
{
break;
}
}
Phead->next = New;
New->next = Head->next;
return ture;
}
bool CircLList_DestInsert(CircLList_t *Head,DataType_t destval,DataType_t data)
{
CircLList_t* New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
return false;
}
CircLList_t* Phead = Head;
while (Phead->next)
{
Phead = Phead->next;
if (destval != Phead->data && Head->next == Phead->next)
{
printf("can not find dest\n");
return false;
}
if (destval == Phead->data && Head->next == Phead->next)
{
CircLList_TailInsert(CircLList_t * Head, DataType_t data)
return true;
}
if (Head->next == Phead->next && Head->next != Phead->next)
{
break;
}
}
New->next= Phead->next;
Phead->next = New;
return true;
}
bool CircLList_Print(CircLList_t *Head)
{
CircLList_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;
}
bool CircLList_HeadDel(CircLList_t *Head)
{
if (NULL == Head->next)
{
printf("CircLList is empty , can not dele");
return false;
}
CircLList_t *Phead1 = Head;
CircLList_t *Phead2 = Head->next;
while (Phead1->next)
{
Phead1 = Phead1->next;
if (Head->next == Phead1->next)
{
break;
}
}
Phead1->next = Head->next->next;
Head->next = Head->next->next;
Phead2->next = NULL;
free(Phead2);
return true;
}
bool CircLList_TailDele(CircLList* Head)
{
if (NULL == Head->next)
{
printf("CircLList is empty , can not dele");
return false;
}
CircLList_t* Phead1 = Head;
CircLList_t* Phead2 = Head;
while (Phead1->next)
{
Phead1 = Phead1->next;
if (Head->next == Phead1->next)
{
break;
}
Phead2 = Phead2->next;
}
Phead2->next = Head->next;
Phead1->next = NULL;
free(Phead1);
return true;
}
bool CircLList_t_DestDele(CircLList_t* Head, CircLList_t data)
{
if (NULL == Head->next)
{
printf("CircLList is empty , can not dele");
return false;
}
CircLList_t* Phead1 = Head->next;
CircLList_t* Phead2 = Head;
while (Phead1->next)
{
Phead1 = Phead1->next;
if (data != Phead1->data && Head->next == Phead1->next)
{
printf("can not find dest\n");
return false;
}
if (Head->next == Phead1->next && Head->next != Phead1->next)
{
break;
}
Phead2 = Phead2->next;
}
Phead2->next = Phead2->next->next;
Phead1->next = NULL;
free(Phead1);
return true;
}
int main(int argc, char const *argv[])
{
return 0;
}
设计双向链表的接口
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* New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
Head->next = New;
return true;
}
DoubleLList_t* Phead = Head;
New->next = Head->next;
Head->next->prev = New;
head->next = New;
return true;
}
bool DoubleLList_TailInsert(DoubleLList_t* Head, DataType_t data)
{
DoubleLList_t* New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
Head->next = New;
return true;
}
DoubleLList_t* Phead = Head;
while (Phead->next)
{
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* New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
if (NULL == Head->next)
{
Head->next = New;
return true;
}
DoubleLList_t* Phead = Head;
while (Phead->next)
{
Phead = Phead->next;
if (destval != Phead->data )
{
printf("can not find dest\n");
return false;
}
if (destval == Phead->data && NULL == Phead->next)
{
DoubleLList_TailInsert(DoubleLList_t * Head, DataType_t data)
return true;
}
if (Head->next == Phead->next)
{
break;
}
}
New->next = Phead->next;
Phead->next->prev = New;
New->prev = Phead;
Phead->next = New;
return true;
}
bool DoubleLList_HeadDel(DoubleLList_t* Head)
{
if (NULL == Head->next)
{
printf("DoubleLList is empty , can not dele");
return false;
}
DoubleLList_t* Phead1 = Head;
DoubleLList_t* Phead2 = Head->next;
Phead1->next = Head->next->next;
Phead2->next->prev = NULL;
Head->next->prev = NULL;
free(Phead2);
return true;
}
bool DoubleLList_TailDele(DoubleLList* Head)
{
if (NULL == Head->next)
{
printf("DoubleLList is empty , can not dele");
return false;
}
DoubleLList_t* Phead = Head;
while (Phead->next)
{
Phead = Phead->next;
}
Phead->prev->next = NULL;
Phead->prev = NULL;
Phead->next = NULL;
free(Phead);
return true;
}
bool DoubleLList_t_DestDele(DoubleLList_t* Head, DataType_t data)
{
if (NULL == Head->next)
{
printf("DoubleLList is empty , can not dele");
return false;
}
DoubleLList_t* Phead1 = Head;
while (Phead1->next)
{
Phead1 = Phead1->next;
if (data == Phead1->data)
{
break;
}
if (data != Phead1->data && NULL = Phead1->next)
{
printf("can not find dest\n");
return false;
}
}
Phead1->prev->next = Phead1->next;
Phead1->next->prev = Phead1->prev;
Phead1->prev = NULL;
Phead1->next = NULL;
return true;
}
int main(int argc, char const *argv[])
{
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现