单向链表接口设计

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//数据类型
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->next==head->next&&head->next->data==dest)
    {
        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[])
posted @ 2024-04-27 00:33  do泽  阅读(8)  评论(0编辑  收藏  举报