-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
84 lines (73 loc) · 1.94 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
public class LinkedList<T> {
private Node<T> head;
private Node<T> current;
private int size;
public LinkedList () {
head = current = null;
size = 0;
}
public boolean empty () {
return head == null;
}
public boolean last () {
return current.next == null;
}
public boolean full () {
return false;
}
public void findFirst () {
current = head;
}
public void findNext () {
current = current.next;
}
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;
}
size++;
}
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;
size -- ;
}
public void print ()
{
Node <T> p = head;
while ( p != null)
{
System.out.print(p.data + " ");
p = p .next;
}
System.out.println("");
}
public int getsize()
{
return size;
}
}