-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.java
213 lines (199 loc) · 3.33 KB
/
LinkList.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
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
213
public class LinkList<T>
{
private class Node
{
private T data;
private Node next;
public Node() {}
public Node(T data,Node next)
{
this.data = data;
this.next = next;
}
}
private Node head;
private Node tail;
private int size;
//构造器
public LinkList()
{
head = null;
tail = null;
}
public LinkList(T element)
{
head = new Node(element,null);
tail = head;
size++;
}
//还回链表长度
public int length()
{
return size;
}
//查询:通过索引查询节点
public Node getNodeByIndex(int index)
{
if (index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("Index is out of boundary");
}
int i = 0;
Node current = head;
while (current != null)
{
if (i == index)
{
return current;
}
current = current.next;
i++;
}
return null;
}
//正向查询
public T get(int index)
{
return getNodeByIndex(index).data;
}
//反向查询
public int locate(T element)
{
Node current = head;
int index = 0;
while (current != null)
{
if (current.data.equals(element))
{
return index;
}
index++;
current = current.next;
}
return -1;
}
//插入:指定位置插入一个元素
public void insert(T element,int index)
{
if (index < 0 || index > size)
{
throw new IndexOutOfBoundsException("Index is out of boundary");
}
if (head == null)
{
add(element);
}
else
{
if (index == 0)
{
addAtHead(element);
}
else
{
Node prev = getNodeByIndex(index - 1);
prev.next = new Node(element,prev.next);
size++;
}
}
}
//插入:尾插法
public void add(T element)
{
if (head == null)
{
head = new Node(element,null);
tail = head;
}
else
{
Node newNode = new Node(element,null);
tail.next = newNode;
tail = newNode;
}
size++;
}
//插入:头插法
public void addAtHead(T element)
{
head = new Node(element,head);
if (tail == null)
{
tail = head;
}
size++;
}
//删除:指定位置处元素
public T delete(int index)
{
if (index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("Index is out of boundary");
}
Node delNode = null;
if (index == 0)
{
delNode = head;
head = head.next;
}
else
{
Node prev = getNodeByIndex(index - 1);
delNode = prev.next;
prev.next = delNode.next;
}
delNode.next = null;
size--;
return delNode.data;
}
//删除:最后一个元素
public T remove()
{
return delete(size - 1);
}
//是否为空
public boolean empty()
{
return size == 0;
}
//清空
public void clear()
{
head = null;
tail = null;
size = 0;
}
//打印控制
public String toString()
{
if (empty())
{
return "[]";
}
else
{
StringBuilder sb = new StringBuilder("[");
for (Node current = head; current != null;
current = current.next)
{
sb.append(current.data.toString() + ", ");
}
int len = sb.length();
return sb.delete(len-2,len).append("]").toString();
}
}
public static void main(String[] args)
{
LinkList<String> list = new LinkList<>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.insert("dddd",1);
System.out.println(list);
list.delete(2);
System.out.println(list);
System.out.println("where is 'cccc': "
+ list.locate("cccc"));
System.out.println("value of index '2': " + list.get(2));
}
}