学习嵌入式的第二十二天——数据结构——双向链表

双向链表:

节点=数据 + NEXT +PREV

手撕代码(增加+删除)

增加,删除的操作, 需要tmp 停止待操作节点的前一节点上。

查找操作进行了扩展,回调函数(函数指针)。解耦合,扩展功能。

相关的操作代码:

cs 复制代码
#include "DouLink.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

DouLinkList *CreateDouLinkList()
{
  DouLinkList *dl = malloc(sizeof(DouLinkList));
  if (NULL == dl)
  {
    perror("CreateDouLinkList malloc");
    return NULL;
  }
  dl->head = NULL;
  dl->clen = 0;
  return dl;
}
int InsertHeadDouLinkList(DouLinkList *dl, DATATYPE *data)
{
  DouLinkNode *newnode = malloc(sizeof(DouLinkNode));
  if (NULL == newnode)
  {
    perror("InsertHeadDouLinkList malloc");
    return 1;
  }
  memcpy(&newnode->data, data, sizeof(DATATYPE));
  newnode->next = NULL;
  newnode->prev = NULL;

  if (IsEmptyDouLinkList(dl))
  {
    dl->head = newnode;
  }
  else
  {
    newnode->next = dl->head;
    dl->head->prev = newnode;
    dl->head = newnode;
  }
  dl->clen++;
  return 0;
}

int ShowDouLinkList(DouLinkList *dl, DIRECT direct)
{
  DouLinkNode *tmp = dl->head;
  if (DIR_FORWARD == direct)
  {
    while (tmp)
    {
      printf("name:%s sex:%c age:%d score:%d\n", tmp->data.name, tmp->data.age,
             tmp->data.age, tmp->data.score);
      tmp = tmp->next;
    }
  }
  else  //逆向显示
  {
    // tmp 会停在最后一个有效元素上
    while (tmp->next)
    {
      tmp = tmp->next;
    }

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

int IsEmptyDouLinkList(DouLinkList *dl)
{
  return 0 == dl->clen;
}

int InsertTailDouLinkList(DouLinkList *dl, DATATYPE *data)
{
  if (IsEmptyDouLinkList(dl))
  {
    return InsertHeadDouLinkList(dl, data);
  }
  else
  {
    DouLinkNode *newnode = malloc(sizeof(DouLinkNode));
    if (NULL == newnode)
    {
      perror("InsertTailDouLinkList malloc");
      return 1;
    }
    memcpy(&newnode->data, data, sizeof(DATATYPE));
    newnode->next = NULL;
    newnode->prev = NULL;

    DouLinkNode *tmp = dl->head;
    while (tmp->next)
    {
      tmp = tmp->next;
    }

    newnode->prev = tmp;
    tmp->next = newnode;
  }
  dl->clen++;
  return 0;
}

int InsertPosDouLinkList(DouLinkList *dl, DATATYPE *data, int pos)
{
  int size = GetSizeDouLinkList(dl);
  if (pos < 0 || pos > size)
  {
    printf("InsertPosDouLinkList pos error\n");
    return 1;
  }
  if (0 == pos)
  {
    return InsertHeadDouLinkList(dl, data);
  }
  else if (size == pos)
  {
    return InsertTailDouLinkList(dl, data);
  }
  else
  {
    DouLinkNode *newnode = malloc(sizeof(DouLinkNode));
    if (NULL == newnode)
    {
      perror("InsertPosDouLinkList malloc");
      return 1;
    }
    memcpy(&newnode->data, data, sizeof(DATATYPE));
    newnode->next = NULL;
    newnode->prev = NULL;

    DouLinkNode *tmp = dl->head;
    while (pos--)
    {
      tmp = tmp->next;
    }

    newnode->next = tmp;
    newnode->prev = tmp->prev;
    tmp->prev = newnode;
    newnode->prev->next = newnode;
    dl->clen++;
  }
  return 0;
}

//DouLinkNode *FindDouLinkList(DouLinkList *dl, char *name)
DouLinkNode *FindDouLinkList(DouLinkList *dl,PFUN fun, void*arg)
{
    DouLinkNode* tmp = dl->head;
    while(tmp)
    {
        //if(0==strcmp(tmp->data.name,name))
        if(fun(&tmp->data,arg))
        {
            return tmp;
        }
        tmp=tmp->next;
    }
    return NULL;
}
int ModifyDouLinkList(DouLinkList *dl, char *name, DATATYPE *newdata);
int DeleteDouLinkList(DouLinkList *dl, char *name);
int GetSizeDouLinkList(DouLinkList *dl)
{
  return dl->clen;
}

int DestroyDouLinkList(DouLinkList *dl);

双向链表的逆序:

三个指针分别代表前一个,当前,后一个

cs 复制代码
int ReverDouLinkList(DouLinkList *dl)
{
  if (NULL == dl->head || NULL == dl->head->next)
  {
    printf("Rever error\n");
    return -1;
  }
  DouLinkNode *prev = NULL;
  DouLinkNode *tmp = dl->head;
  DouLinkNode *next = NULL;

  while (tmp)
  {
    next = tmp->next;
    tmp->next = prev;
    tmp->prev = next;
    prev = tmp;
    tmp = next;
  }
  dl->head = prev;
  return 0;
}
相关推荐
爱吃泡芙的小白白3 分钟前
Agent学习——路由链
学习·agent·路由链
wdfk_prog27 分钟前
[Linux]学习笔记系列 -- [fs]buffer
linux·笔记·学习
Darken0339 分钟前
基于C语言的学习---循环
学习·for循环·while循环·do-while循环·循环的嵌套
海奥华21 小时前
进程调度算法 笔记总结
linux·运维·服务器·笔记·学习
Mr.朱鹏2 小时前
大模型入门学习路径(Java开发者版)下
java·python·学习·微服务·langchain·大模型·llm
xiaolang_8616_wjl3 小时前
c++超级细致的基本框架
开发语言·数据结构·c++·算法
YJlio4 小时前
ProcessExplorer_17.09_x64-Chs 新版本升级:我看到的区别与优势(含升级思路与注意点)
人工智能·笔记·学习
-木槿昔年-4 小时前
【米尔-安路MYD-YM90X创意秀】飞龙派学习和PS串口实践
学习·fpga开发
LYFlied4 小时前
【每日算法】LeetCode124. 二叉树中的最大路径和
数据结构·算法·leetcode·面试·职场和发展
车载测试工程师4 小时前
CAPL学习-SOME/IP交互层-符号数据库访问类函数
学习·tcp/ip·以太网·capl·canoe