-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathLinkList.java
More file actions
68 lines (60 loc) · 1.11 KB
/
LinkList.java
File metadata and controls
68 lines (60 loc) · 1.11 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
package ch17;
/*
* 链表,相当于火车
*/
public class LinkList {
//头结点
private Node first;
public LinkList() {
first = null;
}
/**
* 插入一个结点,在头结点后进行插入
*/
public void insertFirst(Info info) {
Node node = new Node(info);
node.next = first;
first = node;
}
/**
* 删除一个结点,在头结点后进行删除
*/
public Node deleteFirst() {
Node tmp = first;
first = tmp.next;
return tmp;
}
/**
* 查找方法
*/
public Node find(String key) {
Node current = first;
while(!key.equals(current.info.getKey())) {
if(current.next == null) {
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法,根据数据域来进行删除
*/
public Node delete(String key) {
Node current = first;
Node previous = first;
while(!key.equals(current.info.getKey())) {
if(current.next == null) {
return null;
}
previous = current;
current = current.next;
}
if(current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
}