Repository navigation
Expand file tree
/
Copy pathMyLink.java
More file actions
212 lines (187 loc) · 4.12 KB
/
Copy pathMyLink.java
File metadata and controls
212 lines (187 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.java.demo05;
/**
* 自定义链表的实现类
*
* @author jack w
*
*/
public class MyLink implements Link {
/*****************Node內部类开始*******************/
class Node {
private Object data ;
private Node next;
public Node(Object data) {
this.data = data;
}
/**
* 递归增加节点数据的方法
*/
public void add(Node newNode) {
if (this.next == null) {
this.next = newNode;
}else {
this.next.add(newNode);
}
}
/**
* 递归获取当前节点数据放入对象数组中
*/
public void toArrayNode() {
MyLink.this.datas[MyLink.this.foot++] = this.data;
if (this.next != null) {
this.next.toArrayNode();
}
}
/**
* 递归判断当前数据是否包含在链表中
* @param data
* @return
*/
public boolean containsNode(Object search) {
if (search.equals(this.data)) {
return true ;
}else {
if (this.next != null) {
return this.next.containsNode(search);
}else {
return false;
}
}
}
/**
* 根据索引获取数据
* @return
*/
public Object getNode(int index) {
if (MyLink.this.foot ++ == index) {
return this.data ;
}else {
return this.next.getNode(index);
}
}
public boolean setNode(int index , Object newData){
if (MyLink.this.foot++ == index) {
this.data = newData;
return true;
}else {
if (this.next != null) {
return this.next.setNode(index, newData);
}else {
return false;
}
}
}
public boolean removeNode(Node pro , Object removeData) {
if (removeData.equals(this.data)) {
pro.next = this.next ;
MyLink.this.count -- ;
return true;
}else {
if (this.next != null) {
return this.next.removeNode(this,removeData);
}else {
return false;
}
}
}
}
/*****************Node內部类结束*******************/
/**
* 存放根节点
*/
private Node root;
/**
* 链表大小
*/
private int count = 0;
/**
* 链表的对象数组
*/
private Object[] datas ;
/**
* 链表对象数组下标
*/
private int foot = 0 ;
@Override
public void add(Object data) {
//若data数据为null,则结束程序
if (data == null) {
return ;
}
//包装数据到Node节点中
Node newNode = new Node(data);
//若根节点为空,则直接保存在根节点;否则就调用Node节点本身增加节点的方法(递归操作)
if (this.root == null) {
this.root = newNode;
}else {
this.root.add(newNode);
}
//记录节点个数
count += 1;
}
@Override
public int size() {
return count;
}
@Override
public Object[] toArray() {
if (this.count == 0) {
return null;
}
//重置对象数组下标
this.datas = new Object[this.count];
this.foot = 0;
this.root.toArrayNode();
return datas;
}
@Override
public boolean isEmpty() {
return this.root==null && this.count == 0;
}
@Override
public boolean contains(Object search) {
if (search == null || this.root == null) {
return false ;
}
return this.root.containsNode(search);
}
@Override
public Object get(int index) {
if (index < 0 || index > count-1) {
throw new IndexOutOfBoundsException();
}
if (this.root == null) {
return null;
}
//置0脚标,判断索引index和脚标相等并返回
this.foot = 0;
return this.root.getNode(index);
}
@Override
public boolean set(int index, Object newData) {
if (index < 0 || index > this.count - 1) {
throw new IndexOutOfBoundsException();
}
if (this.root == null) {
return false;
}
this.foot = 0 ;
return this.root.setNode(index,newData);
}
@Override
public boolean remove(Object removeData) {
if (removeData == null) {
return false;
}
if (this.root == null) {
return false;
}
if (removeData.equals(this.root.data)) {
this.root = this.root.next;
this.count --;
return true ;
}else {
return this.root.next.removeNode(this.root,removeData);
}
}
}