-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
103 lines (84 loc) · 1.96 KB
/
LinkedList.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
class Node<T> {
public T data;
public Node<T> next;
public Node(T val) {
data = val;
next = null;
}
}
public class LinkedList<T> {
private Node<T> head;
private Node<T> current;
public LinkedList() {
head = current = null;
}
public boolean empty() {
return head == null;
}
public boolean last() {
return current.next == null;
}
public boolean isThereNext() {
return current != null;
}
public boolean full() {
return false;
}
public void findFirst() {
current = head;
}
public void findNext() {
current = current.next;
}
public boolean find(T val) {
Node<T> tmp = head;
if (tmp == null)
return false;
while (tmp != null) {
boolean f = tmp.data == val;
if (val.equals(tmp.data))
return true;
tmp = tmp.next;
}
return false;
}
public T retrieve() {
return current.data;
}
public void update(T val) {
current.data = val;
}
public void insert(T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T>(val);
} else {
tmp = current.next;
current.next = new Node<T>(val);
current = current.next;
current.next = tmp;
}
}
public void remove() {
if (current == head) {
head = head.next;
} else {
Node<T> tmp = head;
while (tmp.next != current)
tmp = tmp.next;
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
@Override
public String toString() {
String s = "[";
for (Node<T> c = head; c != null; c = c.next) {
s += c.data + " ";
}
return s + "]";
}
}