-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList.java
More file actions
137 lines (125 loc) · 2.9 KB
/
LinkList.java
File metadata and controls
137 lines (125 loc) · 2.9 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
public class LinkList {
class Node{
private Object dataObject;//节点数据
private Node nextNode;//节点的下一个节点
public Node(Object dataObject){
this.dataObject = dataObject;
}
public void addNode(Node node){
if(this.nextNode == null){//判断this的下一个节点是否为空,即该节点是否是最后一个节点
this.nextNode = node;
}else{
this.nextNode.addNode(node);
}
}
public Boolean containNode(Object dataObject){//判断是否含有该节点数据
if(dataObject.equals(this.dataObject)){
return true;
}else{
if(this.nextNode == null){
return false;
}else{
return this.nextNode.containNode(dataObject);
}
}
}
public Object getNode(int index){
if(LinkList.this.foot++ == index){
return this.dataObject;
}else{
return this.nextNode.getNode(index);
}
}
public void setNode(int index, Object dataObject){
if(LinkList.this.foot++ == index){
this.dataObject = dataObject;
}else{
this.nextNode.setNode(index, dataObject);
}
}
public void removeNode(Node previous, Object dataObject){
if(dataObject.equals(this.dataObject)){
previous.nextNode = this.nextNode;
}else{
this.nextNode.removeNode(this, dataObject);
}
}
public void toArrayNode(){
LinkList.this.retArray[LinkList.this.foot++] = this.dataObject;
if(this.nextNode != null){
this.nextNode.toArrayNode();
}
}
}
private Node rootNode;//根目录
private int foot = 0;//索引
private int count = 0;//保存元素的数据
private Object []retArray;//返回的节点数据数组
public void add(Object newdata){
if(newdata == null){
return;
}
Node newNode = new Node(newdata);
if(this.rootNode == null){
this.rootNode = newNode;
}else{
this.rootNode.addNode(newNode);
}
this.count ++;
}
public Boolean contain(Object dataObject){
if(dataObject == null || this.rootNode == null){
return false;
}else{
return this.rootNode.containNode(dataObject);
}
}
public Object get(int index){
if(index > this.count){
return null;
}
this.foot = 0;
return this.rootNode.getNode(index);
}
public void set(int index, Object dataObject){
if(index > this.count){
return;
}else{
this.foot = 0;
this.rootNode.setNode(index, dataObject);
}
}
public void remove(Object dataObject){
if(contain(dataObject)){
if(dataObject.equals(this.rootNode.dataObject)){
this.rootNode = this.rootNode.nextNode;
}else{
this.rootNode.nextNode.removeNode(this.rootNode, dataObject);
}
this.count --;
}
}
public Object[] toArray(){
if(this.rootNode == null){
return null;
}else{
this.foot = 0;
this.retArray = new Object[this.count];
this.rootNode.toArrayNode();
return this.retArray;
}
}
public int size(){
return this.count;
}
public Boolean isEmpty(){
if(this.count == 0){
return true;
}else{
return false;
}
}
public void clear(){
this.rootNode = null;
this.count = 0; }
}