forked from auberonedu/linked-list-livecode
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMyLL.Java
More file actions
98 lines (90 loc) · 2.38 KB
/
MyLL.Java
File metadata and controls
98 lines (90 loc) · 2.38 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
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
public class MyLL<T> {
private class Node<E> {
public T value;
public Node<E> next;
public Node<E> last;
public Node(T x, Node<E> y, Node<E> z) {
this.value = x;
this.next = y;
this.last = z;
}
}
private Node<T> head;
private Node<T> tail;
public MyLL() {
head = null;
tail = null;
}
public void printlist() {
Node<T> current = head;
while (current.next!=null) {
System.out.println(current.value);
current = current.next;
}
System.out.println(current.value);
}
public void printlistbackwards() {
Node<T> current = tail;
while (current.last!= null) {
System.out.println(current.value);
current = current.last;
}
System.out.println(current.value);
}
public boolean contains(T ToFind) {
Node<T> current = head;
while (current.next!=null) {
if (current.value==ToFind) {
return true;
}
current = current.next;
}
if (current.value==ToFind) {
return true;
}
else {
return false;
}
}
public T remove(T target) {
if (head == null) {
return null;
}
if (head.value == target) {
head = head.next;
head.last = null;
return target;
}
Node<T> current = head;
while (current.next!=null) {
if (current.next.value == target) {
current.next = current.next.next;
if (current.next !=null) {
current = current.next;
current.last = current.last.last;
}
else {
tail = current;
}
return target;
}
else {
current = current.next;
}
}
return null;
}
public void addToBack (T toAdd) {
if (head == null) {
head = new Node<T>(toAdd, null, null);
tail = head;
return;
}
Node<T> current = head;
while (current.next!=null) {
current = current.next;
}
current.next = new Node<T> (toAdd, null, current);
tail = current.next;
}
}