双向链表实现与应用详解

Makefile 的使用

双向链表

typedef struct person
{
char name32;
int age;
char sex;
int score;
} DATATYPE;

typedef struct dounode
{
DATATYPE data;
struct dounode *prev;
struct dounode *next; // 多了一个向前的指针
} DouLinkNode;

typedef struct list
{
DouLinkNode *head;
int clen;
} DouLinkList;

typedef enum
{
DIR_FORWARD,
DIR_BACKWARD
} DIRECT;

概念图:

函数基本类型:

DouLinkList *CreateDouLinkList()
{
}

头插函数

int InsertHeadDouLinkList(DouLinkList *dl, DATATYPE *data)

bash 复制代码
int InsertHeadDouLinkList(DouLinkList *dl, DATATYPE *data)
{
  DouLinkNode *newnode = malloc(sizeof(DouLinkNode));
  if (NULL == newnode)
  {
    printf("error");
    DouLinkNode *next = NULL;
    DouLinkNode *prev = NULL;
    if (!IsEmptyDouLinkList(dl))
    {
      newnode->next = dl->head;   // 新的指向头节点 
      dl->head->prev = newnode;   // 头节点的前一个指向新节点的本身。
    }
    dl->head = newnode;   // 如果为空的时候,头节点指向新的节点,也就实现了插入头节点的概念。
    dl->clen++;
    return 0;
  }
}

int ShowDouLinkList(DouLinkList *dl, DIRECT dir)

cpp 复制代码
int ShowDouLinkList(DouLinkList *dl, DIRECT dir)
{
  DouLinkNode *tmp = dl->head;
  if (DIR_FORWARD == dir)   // 判断遍历方向是否为正向
  {
    while (tmp)  // 
    {
      printf("%s %d %c %d", tmp->data.name, tmp->data.age, tmp->data.sex, tmp->data.score);
      tmp = tmp->next;  // 向后移动一个节点,(最终的顺序和插入的顺序是相反的)
    }
  }
  else
  {
    while (tmp->next)
    {
      tmp = tmp->next;   // 循环到这里之后 ,tmp 已经指向最后一个节点。
    }
    while (tmp)  //
    {
      printf("%s %d %c %d", tmp->data.name, tmp->data.age, tmp->data.sex, tmp->data.score);
      tmp = tmp->prev; //开始到推,从末尾到头进行输出。
    }
  }
  return 0;
}

int InserTailDouLinkList(DouLinkList *dl, DATATYPE *data)
{
}
DouLinkNode *FindDouLinkList(DouLinkList *dl, char *name)
{
}
int ModifyDouLinkList(DouLinkList *dl, char *name, DATATYPE *newdata)
{
}

int DeleteDouLinkList(DouLinkList *dl, char *name)
{


}
int IsEmptyDouLinkList(DouLinkList *dl)
{
}
int GetSizeDouLinkList(DouLinkList *dl)
{
}
int DestroyDouLinkList(DouLinkList *dl)
{
}

相关推荐
m0_547486662 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr2 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_32 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.2 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.2 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣2 天前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.9922 天前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
无敌贵点大王3 天前
RTThread学习记录11——RT-Thread 设备模型吃透:UART/ADC/PWM/PIN 到底有什么区别?
c语言·stm32·学习·链表
淡海水3 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1013 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质