
1 package cn.mdj.singlelink;
2
3 class Link{
4 //链表是由节点组成,所以定义一个Node节点
5 private class Node{
6 //节点的数据分为 数据域 和指针域 所以: 每一个指针域指向下一个节点,而数据域是可以存放任何数据类型
7 private Object data;
8 private Node next;
9 //数据传入,即节点实例化后,就要将数据封装起来
10 public Node(Object data){
11 this.data = data;
12 }
13
14 public void addNode(Node newNode){
15 if(this.next == null){
16 this.next = newNode;
17 }else{
18 this.next.addNode(newNode);
19 }
20 }
21
22 public boolean containsNode(Object data){
23 if(this.data.equals(data)){
24 return true;
25 }else{
26 if(this.next == null){
27 return false;
28 }else{
29 return this.next.containsNode(data);
30 }
31 }
32 }
33
34 public void removeNode(Node pre,Object data){
35 if(data.equals(this.data)){
36 pre.next = this.next;
37 }else{
38 if(this.next != null){
39 this.next.removeNode(this, data);
40 }
41 }
42 }
43
44 public Object getNode(int index){
45 if(Link.this.foot == index){
46 return this.data;
47 }else{
48 Link.this.foot++;
49 return this.next.getNode(index);
50 }
51 }
52
53 public void toArrayNode(){
54 Link.this.restData[Link.this.foot++] = this.data;
55 if(this.next != null){
56 this.next.toArrayNode();
57 }
58 }
59 }
60
61 //节点定义好后,那么首先需要的是一个根节点
62 private Node root;
63 private int count = 0;
64 private int foot =0;
65 private Object[] restData = null;
66 //添加节点
67 public boolean add(Object data){
68 //将数据包装成节点
69 Node newNode = new Node(data);
70 if(this.root == null){
71 this.root = newNode;
72 }else{
73 this.root.addNode(newNode);
74 }
75 this.count++;
76 return true;
77 }
78
79 //既然可以添加节点,当然是可以删除节点的,但要删除,那么首先肯定要查找是否包含这个节点,所以:
80 public boolean contains(Object data){
81 if(this.root == null || data == null){
82 return false;
83 }
84 return this.root.containsNode(data);
85 }
86
87 //有了节点的查找,下面来删除节点
88 public void remove(Object data){
89 //首先查找,是否存在
90 if(this.contains(data)){
91 //首先判断是否是根节点
92 if(this.root.data.equals(data)){
93 this.root = this.root.next;
94
95 }else{
96 this.root.next.removeNode(this.root,data);
97 }
98 }
99 this.count--;
100 }
101
102 //有了增加 删除 查找,当然应该有获取指定的对象,和判断是否为空链表
103
104 //获取
105 public Object get(int index){
106
107 //而要获取指定位置的索引,索引要合法,即不能超过链表的总数,所以在上面定义一个count
108 if(this.count > index){
109 //需要一个角标,从而来去和index,判断所以定义一个foot
110 this.foot = 0;
111 return this.root.getNode(index);
112 }else{
113 return null;
114 }
115 }
116
117 public boolean emptyLink(){
118 return this.count == 0;
119 }
120
121 public int size(){
122 return this.count;
123 }
124
125 //对象数组输出
126 public Object[] toArray(){
127 if(this.root == null){
128 return null;
129 }
130 this.foot = 0;
131 this.restData = new Object[this.count];
132 this.root.toArrayNode();
133 return this.restData;
134 }
135
136 }
137
138
139
140 public class SingleLink {
141
142 /**
143 * @param args
144 */
145 public static void main(String[] args) {
146 Link all = new Link() ;
147 all.add("Hello") ;
148 all.add("World") ;
149 all.remove("World") ;
150 Object array [] = all.toArray() ; // 取得全部数据
151 for (int x = 0 ; x < array.length ; x ++ ) {
152 System.out.println(array[x]) ;
153 }
154 System.out.println(all.contains("Hello")) ;
155 System.out.println(all.get(0)) ;
156 System.out.println(all.get(1)) ;
157 }
158 }

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