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

双向链表:

节点=数据 + 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;
}
相关推荐
格鸰爱童话13 小时前
向AI学习项目技能(六)
java·人工智能·spring boot·python·学习
H_老邪13 小时前
spring boot 学习之路-1.0
spring boot·后端·学习
lcj251114 小时前
【C语言】数据在内存中的存储
c语言·数据结构
东京老树根14 小时前
SAP学习笔记 - BTP SAP Build02 - Deploy,开始URL,Approve,Reject,履历确认,Log,Context
笔记·学习
旖-旎14 小时前
哈希表(字母异位次分组)(5)
数据结构·c++·算法·leetcode·哈希算法·散列表
头疼的程序员14 小时前
计算机网络:自顶向下方法(第七版)第九章 学习分享(一)
学习·计算机网络
小墨同学boy15 小时前
越用越强不是广告语:拆解 Hermes Agent 的三层学习机制
人工智能·学习
paeamecium15 小时前
【PAT甲级真题】- All Roads Lead to Rome (30)
数据结构·c++·算法·pat考试·pat
PD我是你的真爱粉15 小时前
Redis 数据类型与底层实现:从 SDS、Quicklist 到 ZSet 跳表彻底讲透
数据结构·redis
汀、人工智能16 小时前
[特殊字符] 第100课:任务调度器
数据结构·算法·数据库架构·贪心··任务调度器