java数据结构单向链表的操作有哪些
来源:亿速云
2024-04-11 17:18:36
0浏览
收藏
珍惜时间,勤奋学习!今天给大家带来《java数据结构单向链表的操作有哪些》,正文内容主要涉及到等等,如果你正在学习文章,或者是对文章有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!
关于节点数据添加:
尾添加
最核心的是定义一个头指针和一个尾指针(尾指针可以不定义但是会增加代码的重复性,增加程序运行时间);
关于尾添加:(注意区分有节点和无节点的情况)
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; endadd(&phead,&pend,4); ...... return 0; } void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata) { struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct)); if(NULL == pt) return; pt->data = adddata; pt->pnext = NULL; if(NULL == *phead) { *phead = pt; } else { (*pend)->pnext = pt; } *pend= pt; }
头添加
关于代码思路与尾添加基本一致,注意区分节点的链接:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; head_add(&phead,&pend,4); ...... return 0; } void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata) { struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct)); if(NULL == pt) return; pt->data = adddata; pt->pnext = NULL; if(NULL == *phead) { *pend = pt; } else { pt->pnext = (*phead); } *phead= pt; }
一次性添加n个x数据节点:
利用循坏,直接调用头添加或者尾添加:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; circulate_add(&phead,&pend,4,5); ...... return 0; } void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata); { for(int i = 0;i<count;i++) { endadd(phead, pend, adddata); } }
关于查找:
根据指定数据:
核心就是通过头指针一个一个往下走找到指定节点的数据与所找数据是否匹配,最重要的是要使用中间变量记录头指针,否则就无法找到头指针了(因为是单项链表):
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void data_find(struct Mystruct *phead, int designated_data); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; middle_data_find(phead,4); ...... return 0; } void data_find(struct Mystruct* phead, int designated_data) { if (NULL == phead) return; struct Mystruct* ptemp = phead; while (ptemp != NULL) { if (ptemp->data == designated_data) { printf("找到了"); break; } ptemp = ptemp->pnext; } return; }
根据下标查找:
思路基本不变;区别传入指定下标;内部定义一个计数器,当下标和计数器数值相等;表示链表存在这个节点;可以选择传出或者提醒;大家思考一下,动手实践一下。
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; struct Mystruct *index_find(struct Mystruct *phead, int index); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; middle_data_find(phead,4); ...... return 0; } struct Mystruct* index_find(struct Mystruct* phead, int index) { if (NULL == phead||index<0) return NULL; struct Mystruct* ptemp = phead; int i = 0; for (i = 0; i < index; i++) { ptemp = ptemp->pnext; } return ptemp; }
删除头节点:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_head(struct Mystruct **phead,struct Mystruct **pend); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_head(struct Mystruct** phead, struct Mystruct** pend) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if ((*phead)->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } else { *phead = (*phead)->pnext; free(pt); } } void deleat_end(struct My
删除尾节点:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_end(struct Mystruct**phead,struct Mystruct**pend); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_end(struct Mystruct** phead, struct Mystruct** pend) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if (pt->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } else { while (pt->pnext != (*pend)) { if (pt->pnext == (*pend)) { free(*pend); *pend = pt; pt->pnext = NULL; pt = pt->pnext; } } } }
删除中间节点:
这里思路改变一下:根据数据或者下标找到前一个节点,改变前一个节点的pnext指针的指向,直接指向下一个节点,也就是这个节点的pnext;简单示范一下删除中间指定数据:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata); int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_head(&phead) ...... return 0; } void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata) { if (NULL == *phead) return; struct Mystruct* pt = *phead; if (pt->pnext == NULL) { free(pt); *phead = NULL; *pend = NULL; } }
删除全部节点:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Mystruct { int data; struct Mystruct *pnext; }; void deleat_all(struct Mystruct** phead, struct Mystruct** pend) int main(void) { struct Mystruct *phead = NULL; struct Mystruct *pend= NULL; deleat_all(&phead,&pend) ...... return 0; } void deleat_all(struct Mystruct** phead, struct Mystruct** pend) { while (*phead!= NULL) { struct Mystruct* pt = *phead; *phead = (*phead)->pnext; free(pt); } *phead = NULL; *pend = NULL; }
本篇关于《java数据结构单向链表的操作有哪些》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!
版本声明
本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- WIN10无线网络受限制的处理方法

- 下一篇
- PHP 函数陷阱:识别并避免潜在错误
查看更多
最新文章
-
- 文章 · java教程 | 5分钟前 | 线程安全
- ConcurrentHashMap线程安全机制解析
- 152浏览 收藏
-
- 文章 · java教程 | 25分钟前 |
- Java实现AWSLambda:Serverless实战教程
- 325浏览 收藏
-
- 文章 · java教程 | 29分钟前 |
- Java中main方法为何是void类型
- 315浏览 收藏
-
- 文章 · java教程 | 2小时前 | ClassNotFoundException JPMS 模块路径 模块依赖 模块导出
- JPMS解决ClassNotFoundException新方案
- 500浏览 收藏
-
- 文章 · java教程 | 3小时前 | maven gradle JAR文件 MANIFEST.MF Java打包
- Java打包应用方法,JAR文件创建步骤详解
- 146浏览 收藏
-
- 文章 · java教程 | 3小时前 |
- ArrayList和LinkedList区别详解
- 278浏览 收藏
-
- 文章 · java教程 | 4小时前 |
- ServiceLoader加载错误处理方法
- 371浏览 收藏
-
- 文章 · java教程 | 4小时前 | java 线程池
- Java线程池创建方法与参数优化
- 485浏览 收藏
-
- 文章 · java教程 | 5小时前 |
- JavaMap键值对操作全攻略
- 160浏览 收藏
-
- 文章 · java教程 | 5小时前 | 服务发现 Eureka
- Java服务发现实现方法,Eureka实战教程
- 320浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 114次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 131次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 133次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 121次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 129次使用
查看更多
相关文章
-
- 提升Java功能开发效率的有力工具:微服务架构
- 2023-10-06 501浏览
-
- 掌握Java海康SDK二次开发的必备技巧
- 2023-10-01 501浏览
-
- 如何使用java实现桶排序算法
- 2023-10-03 501浏览
-
- Java开发实战经验:如何优化开发逻辑
- 2023-10-31 501浏览
-
- 如何使用Java中的Math.max()方法比较两个数的大小?
- 2023-11-18 501浏览