java链表

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
interface <E> {		
public void add (E e); //添加数据
public int size(); //获取数据个数
public boolean isEmpty(); //节点是否为空
public Object [] toArray(); //返回数据集合
public E get(int index); //索引
public void set(int index,E data); //修改索引数据
public boolean contains(E data); //判断数据是否存在
public void remove(E e); //删除数据
public void clean(); //清空链表
}

class LinkImpl<E> implements <E> {
private class Node { //内部类,保存节点数据关系
private E data ; //保存数据
private Node next; //下一个节点

public Node (E data) { //有数据才有意义
this.data =data;
}
/*第一次调用this=root.addNode
* 第二次调用this=root.next.addNode
*第三次调用this=root.next.next.addNode
*/
public void addNode(Node newNode) { //保存新得Node数据
if(this.next == null) { //当前节点的下一个节点为空
this.next =newNode; //保存
}else {
this.next.addNode(newNode); //递归
}
}
public void toArrayNode() {
LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data;
if(this.next != null) { //不为空
this.next.toArrayNode(); //递归
}
}
public E getNode(int index) {
if(LinkImpl.this.foot++ == index) {
return this.data; //返回数据
}else {
return this.next.getNode(index);
}
}
public void setNode(int index,E data) {
if(LinkImpl.this.foot++ == index) {
this.data=data; //修改数据
}else {
this.next.setNode(index,data); //递归
}
}
public boolean containsNode(E data) {
if(data.equals(this.data)) { //比较数据
return true;
}else {
if(this.next==null) { //判断下一个是否为空
return false;
}else{
return this.next.containsNode(data); //递归
}
}
}
public void removeNode(Node previous,E data) {
if(this.data.equals(data)) {
previous.next =this.next; //跳过当前节点
}else {
if(this.next != null) {
this.next.removeNode(this,data); //向后删除
}
}
}
}
//-------------------------Link类属性--------------------------------------
private Node root; //保存根
private int count; //个数
private int foot; //角标
private Object [] returnData; //返回数据
//-------------------------Link类方法--------------------------------------
public void add(E e) {
if(e == null) { //判断数据是否为空
return; //为空直接结束
}
//将数据封装到Node类中
Node newNode = new Node(e); //创建一个新节点
if(this.root == null) { //没有根节点情况
this.root = newNode;
}else { //存在根节点
this.root.addNode(newNode);
}
this.count++;
}
public int size() { //统计个数
return this.count;
}
public boolean isEmpty() { //判断是否为空
return this.count==0;
}
public Object [] toArray() {
if(this.isEmpty()) { //判断是否为空
return null;
}
this.foot=0; //脚本清零
this.returnData=new Object[this.count]; //设置长度
this.root.toArrayNode();
return this.returnData;
}
public E get(int index) {
if(index >= count) { //索引长度
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public void set(int index,E data) {
if(index >= count) { //索引长度
return;
}
this.foot=0;
this.root.setNode(index,data);
}
public boolean contains(E data) {
if(data == null) { //判断是否为空
return false;
}
return this.root.containsNode(data);
}
public void remove(E 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 void clean() {
this.root=null;
this.count=0;
}
}
public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Ilink<String> all = new LinkImpl<String>();
System.out.println(all.size());
System.out.println(all.isEmpty());
System.out.println("--------------------------------------------");
all.add("hello");
all.add("hell1");
all.add("hell2");
all.set(2,"11111");
all.remove("hell1");
all.clean();
System.out.println(all.isEmpty());
System.out.println(all.size());
System.out.println("--------------------------------------------");
Object result[] = all.toArray();
if(result !=null) {
for(Object obj : result) {
System.out.println(obj);
}
}
}

}