嵌入式学习笔记 D21:双向链表的基本操作

  • 双向链表的定义与创建
  • 双向链表的插入
  • 双向链表的查找
  • 双向链表的修改
  • 双向链表的删除
  • 双向链表的逆序
  • MakeFile工具使用

一、双向链表的定义与创建

1.双向链表的定义:

双向链表是在单链表的每个结点中,再设置一个指向其前一个结点的指针域。

复制代码
struct DOUNode
{
   struct DATATYPE data;
   struct DOUNode *prev;
   struct DOUNode *next;
   
};

2.双向链表的创建

复制代码
struct DOULinkList* CreateDouLinkList() 
{
    struct DOULinkList *dl = (struct DOULinkList*)malloc(sizeof(struct DOULinkList));
    if(NULL == dl)
    {
        fprintf(stderr,"CreateDouLinkList malloc");
        return NULL;
    }
    dl->head = NULL;
    dl->clen = 0;
    return dl;
    
}

二、双向链表的插入

1.头插法:

复制代码
int InsertHeadDouLinkList(struct DOULinkList* dl, struct DATATYPE* data) 
{
    struct DOUNode *newnode = malloc (sizeof(struct DOUNode));
    if (NULL == newnode) 
    {
        fprintf(stderr, "InsertHeadDouLinkList malloc");
        return 1;
    }
    memcpy(&newnode->data,data,sizeof(struct DATATYPE));
    newnode->next = NULL;
    newnode->prev = NULL;
    newnode->next = dl->head;
    if(dl->head)
    {
        dl->head->prev = newnode;
    }
    dl->head = newnode;
    dl->clen++;
    return 0;

}

2.尾插法:

复制代码
int InsertTailDouLinkList(struct DOULinkList* dl, struct DATATYPE* data) 
{
    if(IsEmptyDouLinkList(dl))
    {
        return InsertHeadDouLinkList(dl,data);
    }
    else
    {

        struct DOUNode *tmp = dl->head; 
        while(tmp->next)
        {
            tmp = tmp->next;
        }
        struct DOUNode *newnode =(struct DOUNode *) malloc (sizeof(struct DOUNode));
        if (NULL == newnode) 
        {
            fprintf(stderr, "InsertTailDouLinkList malloc");
            return 1;
        }
        // chu shi hua jie dian
        memcpy(&newnode->data,data,sizeof(struct DATATYPE));
        newnode->next = NULL;
        newnode->prev = NULL;
        // lian jie lian biao
        newnode->prev = tmp;
        tmp->next = newnode;
        dl->clen++;
    }
    return 0;
}

3.按指定位置插入:

复制代码
int InsertPosDouLinkList(struct DOULinkList* dl, struct DATATYPE* data,int pos)
{
    int len = GetSizeDouLinkList(dl);
    if(pos < 0 || pos > len)
    {
        return 1;
    }
    if( 0 == pos)
    {
        return InsertHeadDouLinkList(dl, data);
    }
    else if(pos == len)
    {
        return InsertTailDouLinkList(dl,data);
    }
    else 
    {
        struct DOUNode *newnode =(struct DOUNode *) malloc (sizeof(struct DOUNode));
        if (NULL == newnode) 
        {
            fprintf(stderr, "InsertPosDouLinkList malloc");
            return 1;
        }
        // chu shi hua jie dian
        memcpy(&newnode->data,data,sizeof(struct DATATYPE));
        newnode->next = NULL;
        newnode->prev = NULL;
        struct DOUNode *tmp = dl->head; 
        int i ;
        for( i = 0; i < pos ; ++i)
        {
            tmp = tmp->next;
        }
        newnode->next = tmp;
        newnode->prev = tmp->prev;
        tmp->prev = newnode;
        newnode->prev->next = newnode;
        dl->clen++;
    }
    return 0;
}

4.链表的遍历:

复制代码
int ShowDouLinkList(struct DOULinkList* dl, DIR dir) 
{
    struct DOUNode *tmp = dl->head;
    if(FORWADR == dir)
    {
        while(tmp)
        {
            printf("%s %c %d %d\n", tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
            tmp = tmp->next;
        }


    }
    else if( BACKWADR == dir)
    {
        while(tmp->next)
        {
            tmp = tmp->next;
        }
        while(tmp)
        {
            printf("%s %c %d %d\n", tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
            tmp = tmp->prev;
        }
        
    }
    return 0;
}

5.判断链表是否为空

复制代码
int IsEmptyDouLinkList(struct DOULinkList *dl)
{
    return 0 == dl->clen;
}

6.获取链表的长度

复制代码
int GetSizeDouLinkList(struct DOULinkList *dl)
{
    return dl->clen;
}

三、双向链表的查找

复制代码
struct DOUNode *FindDouLinkList(struct DOULinkList *dl,char *name)
{
    if(IsEmptyDouLinkList(dl))
    {
        return NULL;
    }
    struct DOUNode *tmp = dl->head;
    while(tmp)
    {
        if(0 == strcmp(tmp->data.name,name))
        {
            return tmp;
        }
        tmp = tmp->next;
    }
    return NULL;
}

四、双向链表的修改

复制代码
int ModifyDouLinkList(struct DOULinkList *dl,char *name, struct DATATYPE* data)
{
    struct DOUNode *tmp = FindDouLinkList(dl,name);
    if(NULL == tmp)
    {
        return 1;
    }

    memcpy(&tmp->data, data, sizeof(struct DATATYPE));

    return 0;

}

五、双向链表的删除

1.链表的清空:

复制代码
int DestoryDouLinkList(struct DOULinkList **dl)
{
    struct DOUNode *tmp = (*dl)->head;
    while(tmp)
    {
        if(NULL == tmp)
        {
            break;
        }
        (*dl)->head = (*dl)->head->next;
        free(tmp);
        tmp = (*dl)->head;
    }
    free(*dl);
    *dl = NULL;
    return 0;
}

2.链表的删除:

复制代码
int DeleteDouLinkList(struct DOULinkList *dl, char *name)
{
    if(IsEmptyDouLinkList(dl))
    {
        return 1;
    }
    struct DOUNode *tmp = FindDouLinkList(dl,name);
    if(dl->head == tmp)
    {
        dl->head = dl->head->next;
        dl->head->prev = NULL;
        free(tmp);
        dl->clen--;
    }
    else if(tmp->next == NULL)
    {
        tmp->prev->next = NULL;
        tmp->prev = NULL;
        free(tmp);
        dl->clen--;
    }
    else
    {
        tmp->prev->next = tmp->next;
        tmp->next->prev = tmp->prev;
        tmp->prev = NULL;
        tmp->next = NULL;
        dl->clen--;
    }
    return 0;
}

六、双向链表的逆序

复制代码
int InverseDouLinkList(struct DOULinkList *dl)
{
    if(IsEmptyDouLinkList(dl))
    {
        return 1;
    }
    int len = GetSizeDouLinkList(dl);
    if( 1 == len)
    {
        return 0;
    }
    struct DOUNode *prev = NULL;
    struct DOUNode *tmp = dl->head;
    struct DOUNode *next = dl->head->next;
    while(1)
    {
        tmp->next = prev;
        tmp->prev = next;
        prev = tmp;
        tmp = next;
        if(NULL == tmp)
        {
            break;
        }
        next = next->next;
    }
    dl->head = prev;
    return 0;
}

七、MakeFile工具使用

三个.c以上可以使用。

1.方法一:

复制代码
a.out(目标):main.c ./doulink (依赖)
    gcc main.c doulink.c//前面空一个Tab键
clean:
    rm a.out

在终端敲make,进行编译,若要指定makefile ,加-f再加指定makefile。

2.方法二:

复制代码
#代表源文件
SRC += main.c(变量名任取)//指定变量
SRC += doulink.c
DST = app(可执行文件)
 
CC = gcc//编译器
FLAG = -g
LIB = -lm
 
$(DST):$(SRC)
    $(CC) $(SRC) $(FLAG) $(LIB)-o(指定名字) $(DST)
clean:
    rm $(DST)
相关推荐
阿絮~11 分钟前
Apache RocketMQ进阶之路阅读笔记和疑问
笔记·apache·rocketmq
天水幼麟5 小时前
动手学深度学习-学习笔记(总)
笔记·深度学习·学习
天水幼麟7 小时前
动手学深度学习-学习笔记【二】(基础知识)
笔记·深度学习·学习
绿皮的猪猪侠8 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
沧海一笑-dj8 小时前
【51单片机】51单片机学习笔记-课程简介
笔记·学习·51单片机·江科大·江科大学习笔记·江科大单片机·江科大51单片机
老虎06278 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
杰克尼9 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
大苏打seven9 小时前
Docker学习笔记:Docker网络
笔记·学习·docker
Green1Leaves11 小时前
pytorch学习-9.多分类问题
人工智能·pytorch·学习
慕y27411 小时前
Java学习第十五部分——MyBatis
java·学习·mybatis