java数据结构单向链表的操作有哪些
来源:亿速云
时间:2024-04-11 17:18:36 433浏览 收藏
珍惜时间,勤奋学习!今天给大家带来《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删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
426 收藏
-
241 收藏
-
294 收藏
-
342 收藏
-
403 收藏
-
397 收藏
-
357 收藏
-
158 收藏
-
486 收藏
-
459 收藏
-
296 收藏
-
491 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习