单向循环链表接口设计

单向循环链表接口设计

//数据类型

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;

}

posted @ 2024-04-27 11:26  do泽  阅读(1)  评论(0编辑  收藏  举报