当前位置: 首页 > news >正文

盐城市住房城乡建设网站动漫设计培训班收费

盐城市住房城乡建设网站,动漫设计培训班收费,成都市住房和城乡建设局,企业网站建设需要多钱双向链表的接口实现(附图解和源码) 文章目录双向链表的接口实现(附图解和源码)前言一、定义结构体二、接口实现(附图解源码)1.初始化双向链表2.开辟新空间3.尾插数据4.尾删数据5.打印双向链表中数据6.头插数…

双向链表的接口实现(附图解和源码)


文章目录

  • 双向链表的接口实现(附图解和源码)
  • 前言
  • 一、定义结构体
  • 二、接口实现(附图解+源码)
    • 1.初始化双向链表
    • 2.开辟新空间
    • 3.尾插数据
    • 4.尾删数据
    • 5.打印双向链表中数据
    • 6.头插数据
    • 7.头删数据
    • 8.查找结点位置
    • 9.在pos位置之前插入
    • 10.删除pos位置
    • 11.销毁双向链表
  • 三、源代码展示
    • 1.test.c(测试+主函数)
    • 2.List.h(接口函数的声明)
    • 3.List.c(接口函数的实现)
  • 总结


前言

本文主要介绍双向链表中增删查改等接口实现,结尾附总源码


一、定义结构体

在这里插入图片描述

代码如下(示例):

typedef int LTDataType;
typedef struct ListNode
{LTDataType data;struct ListNode* prev;struct ListNode* next;
}ListNode;

二、接口实现(附图解+源码)

在这里插入图片描述

这里一共10个接口,我会我都会一 一为大家讲解(图解+源码


1.初始化双向链表

初始化出一个哨兵位的头结点(用malloc开辟),next和prev都指向自己。具体如下图!
在这里插入图片描述
在这里插入图片描述

代码如下(示例):

ListNode* ListInit()
{ListNode* p = (ListNode*)malloc(sizeof(ListNode));if (p == NULL){perror(errno);}ListNode* phead = p;phead->next = phead;phead->prev = phead;return phead;
}

2.开辟新空间

(1)开辟一个链表类型的动态空间,将地址给newnode;
(2)将值放入 newnode 的 data 数据内;
(3)将 newnode 的 next 和 prev 都置为NULL;
注意:1.将malloc开辟空间存到 newnode 里面时,参数为结构体所占的字节大小!2.对 newnode 进行 NULL 判断!

代码如下(示例):

ListNode* BuyListNode(LTDataType x)
{ListNode* p = (ListNode*)malloc(sizeof(ListNode));if (p == NULL){perror(errno);}ListNode* newNode = p;newNode->data = x;newNode->next = NULL;newNode->prev = NULL;return newNode;
}

3.尾插数据

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListPushBack(ListNode* phead, LTDataType x)//尾插数据
{ListNode* tail = phead->prev;ListNode* newnode = BuyListNode(x);//第一个链接tail->next = newnode;newnode->prev = tail;//第二个链接phead->prev = newnode;newnode->next = phead;
}

4.尾删数据

这里需要两次进行断言:
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListPopBack(ListNode* phead)//尾删
{assert(phead);assert(phead->next != phead);ListNode* tail = phead->prev;ListNode* tailprev = tail->prev;free(tail);//连接tailprev->next = phead;phead->prev = tailprev;
}

5.打印双向链表中数据

在这里插入图片描述


代码如下(示例):

void ListPrint(ListNode* phead)//打印数据
{assert(phead);ListNode* cur = phead->next;while (cur != phead){printf("%d ", cur->data);cur = cur->next;}printf("\n");
}

6.头插数据

头插数据也是很简单的一个接口,链接两次即可,如下图
在这里插入图片描述


代码如下(示例):

void ListPushFront(ListNode* phead, LTDataType x)//头插
{assert(phead);ListNode* newNode = BuyListNode(x);ListNode* next = phead->next;phead->next = newNode;newNode->prev = phead;newNode->next = next;next->prev = newNode;
}

7.头删数据

在头删数据时和尾删数据一样都需要进行两次断言如下图!
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListPopFront(ListNode* phead)//头删
{assert(phead);assert(phead->next != phead);ListNode* next = phead->next;ListNode* nextNext = next->next;phead->next = nextNext;nextNext->prev = phead;free(next);
}

8.查找结点位置

这个比较简单,和单链表一样,直接用cur遍历即可,直接上代码

代码如下(示例):

ListNode* ListFind(ListNode* phead, LTDataType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}

9.在pos位置之前插入

在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListInsert(ListNode* pos, LTDataType x)//pos位置之前插入
{assert(pos);ListNode* newNode = BuyListNode(x);ListNode* posPrev = pos->prev;// posPrev  newnode posposPrev->next = newNode;newNode->prev = posPrev;newNode->next = pos;pos->prev = newNode;
}

根据ListInsert接口我们可以把头插和尾插进行改版,如下图!
在这里插入图片描述


在这里插入图片描述


10.删除pos位置

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListErase(ListNode* pos)//删除pos位置
{assert(pos);ListNode* prev = pos->prev;ListNode* next = pos->next;free(pos);pos = NULL;prev->next = next;next->prev = prev;
}

11.销毁双向链表

在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void ListDestory(ListNode* phead)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){ListNode* next = cur->next;free(cur);cur = next;}free(phead);phead = NULL;
}

三、源代码展示

1.test.c(测试+主函数)

代码如下(示例):

#include "list.h"
void Testlist1()
{ListNode* n1 = ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插//ListPushBack(n1, 3);//尾插//ListPushBack(n1, 4);//尾插//ListPushBack(n1, 5);//尾插//ListPopFront(n1);//头删//ListPopBack(n1);//尾删//ListPushFront(n1, 0);//头插ListPrint(n1);
}
void Testlist2()
{ListNode* n1 = ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插ListPushBack(n1, 3);//尾插ListPushBack(n1, 4);//尾插ListPushBack(n1, 5);//尾插ListNode* p = ListFind(n1, 5);printf("%d \n", p->data);
}
void Testlist3()
{ListNode* n1 = ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插ListPushBack(n1, 3);//尾插ListPushBack(n1, 4);//尾插ListPushBack(n1, 5);//尾插//ListNode* p = ListFind(n1, 5);//ListInsert(p, 0);ListDestory(n1);ListPrint(n1);
}
int main()
{//Testlist1();//Testlist2();Testlist3();return 0;
}

2.List.h(接口函数的声明)

代码如下(示例):

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
typedef int LTDataType;
typedef struct ListNode
{LTDataType data;struct ListNode* prev;struct ListNode* next;
}ListNode;
ListNode* ListInit();//初始化双向链表
void ListPushBack(ListNode* phead, LTDataType x);//尾插
void ListPopBack(ListNode* phead);//尾删
void ListPushFront(ListNode* phead, LTDataType x);//头插
void ListPopFront(ListNode* phead);//头删
void ListPrint(ListNode* phead);//打印
ListNode* ListFind(ListNode* phead, LTDataType x);//查找数据
void ListInsert(ListNode* pos, LTDataType x);//pos位置之前插入
void ListErase(ListNode* pos);//删除pos位置
void ListDestory(ListNode* phead);//销毁双向链表

3.List.c(接口函数的实现)

代码如下(示例):

#include "list.h"
ListNode* ListInit()
{ListNode* p = (ListNode*)malloc(sizeof(ListNode));if (p == NULL){perror(errno);}ListNode* phead = p;phead->next = phead;phead->prev = phead;return phead;
}
ListNode* BuyListNode(LTDataType x)
{ListNode* p = (ListNode*)malloc(sizeof(ListNode));if (p == NULL){perror(errno);}ListNode* newNode = p;newNode->data = x;newNode->next = NULL;newNode->prev = NULL;return newNode;
}
void ListPushBack(ListNode* phead, LTDataType x)//尾插数据
{ListNode* tail = phead->prev;ListNode* newnode = BuyListNode(x);//第一个链接tail->next = newnode;newnode->prev = tail;//第二个链接phead->prev = newnode;newnode->next = phead;//ListInsert(phead, x);
}
void ListPrint(ListNode* phead)//打印数据
{assert(phead);ListNode* cur = phead->next;while (cur != phead){printf("%d ", cur->data);cur = cur->next;}printf("\n");
}
void ListPopBack(ListNode* phead)//尾删
{assert(phead);assert(phead->next != phead);ListNode* tail = phead->prev;ListNode* tailprev = tail->prev;free(tail);//连接tailprev->next = phead;phead->prev = tailprev;
}
void ListPushFront(ListNode* phead, LTDataType x)//头插
{assert(phead);ListNode* newNode = BuyListNode(x);ListNode* next = phead->next;phead->next = newNode;newNode->prev = phead;newNode->next = next;next->prev = newNode;//ListInsert(phead->next, x);
}
void ListPopFront(ListNode* phead)//头删
{assert(phead);assert(phead->next != phead);ListNode* next = phead->next;ListNode* nextNext = next->next;phead->next = nextNext;nextNext->prev = phead;free(next);
}
ListNode* ListFind(ListNode* phead, LTDataType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)//pos位置之前插入
{assert(pos);ListNode* newNode = BuyListNode(x);ListNode* posPrev = pos->prev;// posPrev  newnode posposPrev->next = newNode;newNode->prev = posPrev;newNode->next = pos;pos->prev = newNode;
}
void ListErase(ListNode* pos)//删除pos位置
{assert(pos);ListNode* prev = pos->prev;ListNode* next = pos->next;free(pos);pos = NULL;prev->next = next;next->prev = prev;
}
void ListDestory(ListNode* phead)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){ListNode* next = cur->next;free(cur);cur = next;}free(phead);phead = NULL;
}

总结

以上就是今天要讲的内容,本文介绍双向链表的接口实现(附图解和源码)。
如果我的博客对你有所帮助记得三连支持一下,感谢大家的支持!
在这里插入图片描述

http://www.yayakq.cn/news/42126/

相关文章:

  • 雅安市建设网站河北网站搜索排名优化方案
  • 成都网站建设推荐安徽秒搜科技南开网站建设公司
  • 帝国cms怎么生成网站地图网站建设服务合约
  • 建设银行乾县支行网站优秀中文网页设计
  • 成都论坛网站建设设计学类专业包括什么
  • 做网站好还是网页好网站一次性建设
  • 网站被收录 但搜索不到主页wordpress页脚小工具栏
  • 图书馆网站建设贵州省建设银行网站
  • 网站开发struts网站基础风格创建
  • 杭州开发区网站建设岳阳优化公司
  • 怎么建设大型商务网站小程序开发制作哪家好
  • 哪个网站做h5好用280地图导航下载
  • 北京企业做网站手机网站开发下崽
  • 在贵州省住房和城乡建设厅网站查询30多了学网站建设晚吗
  • 滨江道做网站公司南京马鞍山网站建设
  • 哪个网站做黄金交易最好wordpress add option
  • 汽车专业网站网站后台如何上传文件
  • 茶文化网站设计免费企业微信app下载安装不了
  • 惠州市+网站开发公司阿里云服务器开源做几个网站
  • 国内外网站建设比较wordpress 评论 邮件
  • 小学校园门户网站建设腾讯企业邮箱登录入口手机版
  • 一个企业建设网站的目的安装下载应用
  • 网站公司网站定制wordpress游戏插件
  • 网站建设需要多少钱网站建设的用途是什么意思
  • 如何设计营销型网站建设wordpress 外贸企业模板
  • wordpress ai手机seo网站推广
  • 成都网站建设 3e网站建设视频直播软件有哪些
  • 网站建设费属于广告费用吗宣传商务型的网站
  • 长沙网站设计我选刻营销软文500字
  • 做网站需要跟客户了解什么百度推广费用一年多少钱