首页 > 其他分享 >单向循环链表接口设计

单向循环链表接口设计

时间:2024-04-27 11:34:44浏览次数:16  
标签:cll head return move 单向 接口 next 链表 new

目录

单向循环链表接口设计

//数据类型

typedef int datatype_t;

//创建结点类型

typedef struct cirlinlist

{

datatype_t data;//数据

struct cirlinlist *next;//直接后驱

}cll_t;

//输出数据

void cll_updata(datatype_t data)

{

printf("%d\n",data);

}

创建新的头结点

//新建表头

cll_t *cll_head(void)

{

cll_t *head=(cll_t *) calloc(1,sizeof(cll_t));

if(NULL==head)

{

​ perror("failed to creat header");

​ exit(-1);

}

head->next=head;

return head;

}

创建新节点并初始化该节点

//新建结点

cll_t * cll_newcode(datatype_t data)

{

cll_t new=(cll_t)calloc(1,sizeof(cll_t));

//判断是否创建成功

if(NULL==new)

{

​ perror("calloc memory for new is failed");

​ exit(-1);

}

new->data=data;

new->next=new;

return new;

}

工具函数

遍历链表

bool cll_print(cll_t *head)

{

cll_t *move=head->next;

if(head->next==head)

{

​ perror("empty list,travel failed");

​ return false;

}

do

{

​ cll_updata(move->data);

​ move=move->next;

}while(move!=head->next);

return true;

}

查找尾结点

cll_t* cll_findfail(cll_t *head)

{

cll_t *move=head->next;

//判断是否为空链表

if(head->next==head)

{

​ perror("empty list,find failed");

​ exit(-1);

}

while(move->next!=head->next)

{

​ move=move->next;

}

return move;

}

查找尾结点前置驱动

cll_t* cll_findprior(cll_t *head)

{

cll_t move=head,mv=head->next;

//判断是否为空链表

if(head->next==head)

{

​ perror("empty list,find failed");

​ exit(-1);

}

while(mv->next!=head->next)

{

​ mv=mv->next;

​ move=move->next;

}

return move;

}

找到指定结点

cll_t* cll_finddest(cll_t *head,datatype_t dest)

{

if(head->next==head)

{

​ perror("empty list,find failed");

​ exit(-1);

}

cll_t *move=head;

while(move->next!=head)

{

​ move=move->next;

​ if(move->data==dest)

​ {

​ return move;

​ }

}

perror("can't find dest");

exit(-1);

}

查找指定节点前置驱动

cll_t* cll_finddstpri(cll_t *head,datatype_t dest)

{

if(head->next==head)

{

​ perror("empty list,find failed");

​ exit(-1);

}

//判断

if(head->next->data==dest)

​ return head;

if(head->next->next==head->next)

{

​ perror("find failed");

​ exit(-1);

}

cll_t move=head->next,mv=head;

while(move->next!=head)

{

​ move=move->next;

​ mv=mv->next;

​ if(move->data==dest)

​ {

​ return mv;

​ }

}

perror("can't find dest");

exit(-1);

}

创建每一个新节点并插入到头部

//插入结点(头插)

bool cll_insthead(datatype_t data,cll_t *head)

{

cll_t *new=cll_newcode(data);

if(NULL==new)

{

​ perror("can't insert new code");

​ return false;

}

if(head->next==head)

{

​ new->next=head;

​ head->next=new;

}

cll_t *tail=cll_findfail(head);

tail->next=new;

new->next=head->next;

head->next=new;

return true;

}

新建结点并插入到尾部

bool cll_insttail(datatype_t data,cll_t *head)

{

cll_t tail=cll_findfail(head),new=cll_newcode(data);

if(NULL==new)

{

​ perror("can't insert new code");

​ return false;

}

new->next=tail->next;

tail->next=new;

return true;

}

新建结点并插入到指定节点之后

bool cll_instpont(cll_t *head,datatype_t data,datatype_t dest)

{

cll_t new=cll_newcode(data),dst=cll_finddest(head,dest);

if(NULL==new)

{

​ perror("can't insert new code");

​ return false;

}

new->next=dst->next;

dst->next=new;

return true;

}

删除头部结点

bool cll_headel(cll_t *head)

{

if(head->next==head)

{

​ perror("error:lincked is NULL");

​ return false;

}

cll_t first=head->next,tail=cll_findfail(head);

if(head->next->next==head->next)

{

​ head->next=head;

​ first->next=NULL;

​ free(first);

​ return true;

}

tail->next=first->next;

head->next=first->next;

first->next=NULL;

free ( first);

return true;

}

删除尾部结点

bool cll_taildel(cll_t *head)

{

if(head->next==head)

{

​ perror("error:lincked is NULL");

​ return false;

}

if(head->next->next==head->next)

{

​ cll_t * first=head->next;

​ head->next=head;

​ first->next=NULL;

​ free(first);

​ return true;

}

cll_t prior,last;

prior =cll_findprior(head);

last=prior->next;

prior->next=last->next;

last->next=NULL;

free(last);

return true;

}

删除指定结点

bool cll_destdel(datatype_t dest,cll_t *head)

{

if(head->next==head)

{

​ perror("empty list,find failed");

​ return false;

}

cll_t * first=head->next;

if(head->next->nexthead->next&&head->next->datadest)

{

​ head->next=head;

​ first->next=NULL;

​ free(first);

}

cll_t dstpri=cll_finddstpri(head,dest),dst=dstpri->next;

if(dest==head->next->data)

{

​ return cll_headel(head);

}

dstpri->next=dst->next;

dst->next=NULL;

free(dst);

}

调试函数

int main(int argc, char const *argv[])

{

cll_t *T=cll_head();

cll_insthead(2,T);

cll_insttail(4,T);

cll_instpont(T,3,2);

cll_insthead(1,T);

cll_headel(T);

cll_taildel(T);

cll_destdel(1,T);

cll_print(T);

return 0;

}

标签:cll,head,return,move,单向,接口,next,链表,new
From: https://www.cnblogs.com/8866880c/p/18161873

相关文章

  • 强大的企业四要素验证接口,保障企业安全
      企业安全一直是各个企业关注的焦点,尤其是在合作伙伴选择和交易过程中,我们更需要确保对方企业的真实性和合法性。为了解决这个问题,挖数据平台提供了一项强大的企业四要素验证接口,以确保企业信息的真实性和完整性。这个接口非常简单易懂,只需要输入企业名称、法人名称、社会......
  • 双向循环链表的头插法的实现
    include<stdio.h>include<stdlib.h>typedefstructslik{intdata;structslik*next;structslik*prev;}sli;voidcreatesli(sli**head,inta[],intsize){for(inti=0;i<size;i++){sli*s=(sli*)malloc(sizeof(sli));s->data=a[i];......
  • 单向循环链表的头插法实现
    ``#include<stdio.h>``````include<stdlib.h>typedefstructslik{intdata;structslik*next;}sli;voidcreatesli(sli**head,inta[],intsize){for(inti=0;i<size;i++){sli*s=(sli*)malloc(sizeof(sli));s->data=a[i];s->......
  • 单向循环链表的尾插法实现
    `#include<stdio.h>include<stdlib.h>typedefstructslik{intdata;structslik*next;}sli;voidcreatesli(sli**head,inta[],intsize){for(inti=0;i<size;i++){sli*s=(sli*)malloc(sizeof(sli));s->data=a[i];s->next=NU......
  • 单向链表队列程序接口设计
    目录目录单向链表构建队列的接口函数库函数的调用指的是链表队列中的元素的数据类型,用户可以根据需要进行修改构造记录链表队列LinkQueueNode各项参数(链表队列结点的指针指向的下一个结点地址+链表队列的结点数据)的结构体构造记录链表队列LinkQueue各项参数(链表队列的队首地......
  • 数据结构_链表_双向循环链表的初始化、插入、删除、修改、查询打印(基于C语言实现)
    版本:2024年4月26日V1.0发布于博客园/***@filename:DoubleLinkedList.c*@brief:实现双向循环链表的相关功能*@author:[email protected]*@date:2024/04/26*@version:1.0*@note:*CopyRight(c)2023-2024RISE_AND......
  • 以链表为基础实现链式队列
    以链表为基础实现链式队列如果打算以链表作为基础来实现队列的操作,可以避免内存浪费以及避免内存成片移动,只需要确定队头和队尾即可,一般把链表头部作为队头,可以实现头删,把链表尾部作为队尾,可以实现尾插。#include<stdio.h>#include<stdbool.h>#include<stdlib.h>//对输入......
  • 双向循环链表
    双向循链表双向循环链表我是在双向循环链表上进行升级,就是双向循环链表首结点和尾结点相链接,首结点的prev链接尾结点本身,尾结点的next链接首结点本身,在对头部或者尾部操作的时候与双向链表有区别,具体代码写在下面。希望看完代码的你发现错误,请评论批评指正,非常感谢。目录双......
  • 自定义单链表队列的基本接口函数(非循环队列)
    单链表构建队列的接口函数/********************************************************************文件名称: 单链表构建队列的接口函数文件作者:[email protected]创建日期:2024/04/26文件功能:对单链表循环队列的增删改查功能的定义注意事项:NoneCop......
  • 单链表根据尾插法建立
    include<stdio.h>include<stdlib.h>typedefstructslik{intdata;structslik*next;}sli;voidcreatesli(sli**head,inta[],intsize){for(inti=0;i<size;i++){sli*s=(sli*)malloc(sizeof(sli));s->data=a[i];s->next=NULL......