單向連結串列介面設計

do泽發表於2024-04-27
#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[])

相關文章