内存泄漏检测、单向链表的操作

news/2025/2/12 16:08:16/

我要成为嵌入式高手之3月19日数据结构第二天!!
————————————————————————————

valgrind内存测试工具

让虚拟机上网、在虚拟机上下载软件,参考笔记:

我要成为嵌入式高手之2月3日Linux高编第一天!!-CSDN博客

上图表示申请了10个空间,释放了10个空间,没有内存泄漏

上图申请了11个空间,释放了10个空间,存在内存泄漏(leaked memory) 

 单向链表逆序

#include "head.h"LINK_LIST *CreateLink()
{LINK_LIST *plist = malloc(sizeof(LINK_LIST));if (NULL == plist){perror("fail to malloc");return NULL;}plist->phead = NULL;plist->clen = 0;return plist;
}int LinkSearch(LINK_LIST *plist)
{LINK_NODE *ptmp = plist->phead;while (ptmp != NULL){printf("%d\n", ptmp->data);ptmp = ptmp->pnext;}printf("len = %d\n", plist->clen);return 0;
}int PushTailLink(LINK_LIST *plist, DATA_TYPE data)
{LINK_NODE *ptmp = plist->phead;LINK_NODE *pnode = malloc(sizeof(LINK_NODE));if (NULL == pnode){perror("fail to malloc pnode");return -1;}pnode->data = data;pnode->pnext = NULL;if (plist->phead == NULL){plist->phead = pnode;}else{while (ptmp->pnext != NULL){ptmp = ptmp->pnext;}ptmp->pnext = pnode;plist->clen++;}return 0;
}int ReverseLink(LINK_LIST *plist)
{if (plist->phead == NULL){return -1;}LINK_NODE *ptmp = plist->phead;plist->phead = NULL;LINK_NODE *pinsert = NULL;while (ptmp != NULL){pinsert = ptmp;ptmp = ptmp->pnext;pinsert->pnext = plist->phead;plist->phead = pinsert;}return 0;
}int main()
{LINK_LIST *plist = NULL;plist = CreateLink();if (NULL == plist){return -1;}PushTailLink(plist, 1);PushTailLink(plist, 2);PushTailLink(plist, 3);PushTailLink(plist, 4);PushTailLink(plist, 5);LinkSearch(plist);ReverseLink(plist);printf("=========================\n");LinkSearch(plist);return 0;
}

找单向链表的中间节点

 

LINK_NODE *SearchMidNode(LINK_LIST *plist)
{LINK_NODE *pfast = plist->phead;LINK_NODE *pslow = plist->phead;while (pfast != NULL){pfast = pfast->pnext;if (NULL == pfast){break;}pfast = pfast->pnext;pslow = pslow->pnext;}return pslow;
}

寻找链表倒数第K个结点

LINK_NODE *SearchReNoKNode(LINK_LIST *plist, int k)
{LINK_NODE *pfast = plist->phead;LINK_NODE *pslow = pfast;int i = 0;for (i = 0; i < k; i++){if (NULL == pfast){return NULL;}pfast = pfast->pnext;}while (pfast != NULL){pfast = pfast->pnext;pslow = pslow->pnext;}return pslow;
}

删除指定数据的结点

int DeleteKNode(LINK_LIST *plist, int k)
{LINK_NODE *pfree = NULL;LINK_NODE *ptmp = NULL;pfree = plist->phead;ptmp = pfree;while (pfree != NULL){if (pfree->data == k){if (plist->phead == pfree){plist->phead = pfree->pnext;free(pfree);plist->clen--;break;}else{ptmp->pnext = pfree->pnext;free(pfree);plist->clen--;break;}}else{ptmp = pfree;pfree = pfree->pnext;}}return 0;
}

 链表的插入排序

int InsertSort(LINK_LIST *plist)
{LINK_NODE *ptmp = NULL;LINK_NODE *pinsert = NULL;LINK_NODE *p = NULL;if (p == NULL || p->pnext == NULL){return -1;}ptmp = plist->phead->pnext;plist->phead->pnext = NULL;pinsert = ptmp;while (ptmp != NULL){pinsert = ptmp;ptmp = ptmp->pnext;if (pinsert->data <= plist->phead->data){pinsert->pnext = plist->phead;plist->phead = pinsert;}else{LINK_NODE *p = plist->phead;while (p->pnext != NULL && p->pnext->data < pinsert->data){p = p->pnext;}pinsert->pnext = p->pnext;p->pnext = pinsert;}}return 0;
}

 

                                                                                                                                                                                      


http://www.ppmy.cn/news/1389009.html

相关文章

计算机网络:数据交换方式

计算机网络&#xff1a;数据交换方式 电路交换分组交换报文交换传输对比 本博客介绍计算机之间数据交换的三种方式&#xff0c;分别是电路交换、分组交换以及报文交换。 电路交换 我们首先来看电路交换&#xff0c;在电话问世后不久&#xff0c;人们就发现要让所有的电话机都…

ES代替品:轻量级搜索引擎MeiliSearch

痛点 虽然Elasticsearch足够灵活强大、扩展性和实时性也较好。但是对于中小型项目来说&#xff0c;Elasticsearch还是显得有些庞大&#xff0c;对硬件设备的要求也较高。那么&#xff0c;在要求不是很高的情况下&#xff0c;我们可以考虑另一种搜索引擎方案&#xff1a;MeiliSe…

gitlab runner没有内网的访问权限应该怎么解决

如果你的GitLab Runner没有内网访问权限&#xff0c;但你需要访问内部资源&#xff08;如私有仓库或其他服务&#xff09;&#xff0c;你可以考虑以下几种方法&#xff1a; VPN 或 SSH 隧道&#xff1a; 在允许的情况下&#xff0c;通过VPN或SSH隧道连接到内部网络。这将允许Gi…

HarmonyOS NEXT应用开发之适配挖孔屏案例

介绍 本示例介绍使用屏幕属性getDefaultDisplaySync、getCutoutInfo接口实现适配挖孔屏。该场景多用于沉浸式场景下。 效果图预览 使用说明 加载完成后顶部状态栏时间和电量显示位置规避了不可用区域。 实现思路 通过setWindowLayoutFullScreen、setWindowSystemBarEnable…

界面组件DevExpress WinForms v23.2 - 数据可视化功能升级

DevExpress WinForms拥有180组件和UI库&#xff0c;能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序&#xff0c;无论是Office风格的界面&#xff0c;还是分析处理大批量的业务数据&#xff0c;它都能轻松胜…

xAI开发的一款巨大型语言模型(HLM)--Grok 1

在xAI发布Grok的权重和架构之后&#xff0c;很明显大型语言模型&#xff08;LLM&#xff09;的时代已经过去&#xff0c;现在是巨大型语言模型&#xff08;HLM&#xff09;的时代。这个混合专家模型发布了3140亿个参数&#xff0c;并且在Apache 2.0许可下发布。这个模型没有针对…

L1-039 古风排版(PTA)

文章目录 L1-039 古风排版题目描述模拟 L1-039 古风排版 题目描述 中国的古人写文字&#xff0c;是从右向左竖向排版的。本题就请你编写程序&#xff0c;把一段文字按古风排版。 输入格式&#xff1a; 输入在第一行给出一个正整数N&#xff08;<100&#xff09;&#xff…

【Mysql数据库基础03】分组函数(聚合函数)、分组查询

分组函数(聚合函数&#xff09;、分组查询 1 分组函数1.1 简单的使用1.2 是否忽略null值1.3 和关键字搭配使用1.4 count函数的详细介绍1.5 练习 2 分组查询Group by2.1 简单的分组查询2.2 练习 3 格式投票:yum: 1 分组函数 1.1 简单的使用 COUNT(expression)&#xff1a;计算符…