
package cn.mdj.singlelink;
class Link{
//链表是由节点组成,所以定义一个Node节点
private class Node{
//节点的数据分为 数据域 和指针域 所以: 每一个指针域指向下一个节点,而数据域是可以存放任何数据类型
private Object data;
private Node next;
//数据传入,即节点实例化后,就要将数据封装起来
public Node(Object data){
this.data = data;
}
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode;
}else{
this.next.addNode(newNode);
}
}
public boolean containsNode(Object data){
if(this.data.equals(data)){
return true;
}else{
if(this.next == null){
return false;
}else{
return this.next.containsNode(data);
}
}
}
public void removeNode(Node pre,Object data){
if(data.equals(this.data)){
pre.next = this.next;
}else{
if(this.next != null){
this.next.removeNode(this, data);
}
}
}
public Object getNode(int index){
if(Link.this.foot == index){
return this.data;
}else{
Link.this.foot++;
return this.next.getNode(index);
}
}
public void toArrayNode(){
Link.this.restData[Link.this.foot++] = this.data;
if(this.next != null){
this.next.toArrayNode();
}
}
}
//节点定义好后,那么首先需要的是一个根节点
private Node root;
private int count = 0;
private int foot =0;
private Object[] restData = null;
//添加节点
public boolean add(Object data){
//将数据包装成节点
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
this.count++;
return true;
}
//既然可以添加节点,当然是可以删除节点的,但要删除,那么首先肯定要查找是否包含这个节点,所以:
public boolean contains(Object data){
if(this.root == null || data == null){
return false;
}
return this.root.containsNode(data);
}
//有了节点的查找,下面来删除节点
public void remove(Object data){
//首先查找,是否存在
if(this.contains(data)){
//首先判断是否是根节点
if(this.root.data.equals(data)){
this.root = this.root.next;
}else{
this.root.next.removeNode(this.root,data);
}
}
this.count--;
}
//有了增加 删除 查找,当然应该有获取指定的对象,和判断是否为空链表
//获取
public Object get(int index){
//而要获取指定位置的索引,索引要合法,即不能超过链表的总数,所以在上面定义一个count
if(this.count > index){
//需要一个角标,从而来去和index,判断所以定义一个foot
this.foot = 0;
return this.root.getNode(index);
}else{
return null;
}
}
public boolean emptyLink(){
return this.count == 0;
}
public int size(){
return this.count;
}
//对象数组输出
public Object[] toArray(){
if(this.root == null){
return null;
}
this.foot = 0;
this.restData = new Object[this.count];
this.root.toArrayNode();
return this.restData;
}
}
public class SingleLink {
/**
* @param args
*/
public static void main(String[] args) {
Link all = new Link() ;
all.add("Hello") ;
all.add("World") ;
all.remove("World") ;
Object array [] = all.toArray() ; // 取得全部数据
for (int x = 0 ; x < array.length ; x ++ ) {
System.out.println(array[x]) ;
}
System.out.println(all.contains("Hello")) ;
System.out.println(all.get(0)) ;
System.out.println(all.get(1)) ;
}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端