-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListImpl.java
160 lines (133 loc) · 4.07 KB
/
LinkedListImpl.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
package me.koply.test.linkedlist;
import java.util.Iterator;
public class LinkedListImpl<T> extends LinkedList<T> implements Iterable<T> {
public static class Node<T> {
protected Node<T> prev;
protected T data;
protected Node<T> next;
Node(Node<T> prev, T data, Node<T> next) {
this.prev = prev;
this.data = data;
this.next = next;
}
}
private Node<T> head;
private Node<T> tail;
private int size = 0;
@Override
public int size() {
return size;
}
@Override
public void add(T data) {
if (data == null) throw new IllegalArgumentException("Data cannot be null.");
addLast(data);
}
@Override
public void add(int index, T data) {
// [0-11] -> 12 possible
if (index > size || index < 0) throw new IndexOutOfBoundsException("Range is 0-" + (size-1) + " but your request is " + index);
if (data == null) throw new IllegalArgumentException("Data cannot be null.");
var node = new Node<>(null, data, null);
if (index == size) {
node.prev = tail;
tail.next = node;
} else {
var old = getNode(index);
node.next = old;
node.prev = old.prev;
old.prev.next = node;
old.prev = node;
}
size++;
}
@Override
public void addLast(T data) {
if (data == null) throw new IllegalArgumentException("Data cannot be null.");
if (size != 0) {
tail.next = new Node<>(tail, data, null);
tail = tail.next;
} else {
head = new Node<>(null, data, null);
tail = head;
}
size++;
}
@Override
public void addFirst(T data) {
if (data == null) throw new IllegalArgumentException("Data cannot be null.");
if (head != null) {
var old = head;
head = new Node<>(null, data, old);
} else {
head = new Node<>(null, data, null);
}
size++;
}
@Override
public void remove(T data) {
Node<T> n = head;
boolean found = false;
do {
n = n.next;
if (n != null && data.equals(n.data)) {
found = true;
break;
}
} while (n != null && !data.equals(n.data));
if (found) {
n.prev.next = n.next;
n.next.prev = n.prev;
size--;
}
}
@Override
public T remove(int index) {
if (index >= size || index < 0) throw new IndexOutOfBoundsException("Range is 0-" + (size-1) + " but your request is " + index);
var n = getNode(index);
n.prev.next = n.next;
n.next.prev = n.prev;
size--;
return n.data;
}
@Override
public T get(int index) {
if (index >= size || index < 0) throw new IndexOutOfBoundsException("Range is 0-" + (size-1) + " but your request is " + index);
var node = getNode(index);
return node.data;
}
@Override
public T getFirst() {
return head.data;
}
@Override
public T getLast() {
return tail.data;
}
private Node<T> getNode(int index) {
// if size-index < index that means that index closer to tail
boolean fromLast = size-index <= index;
// size-1 because we need last index
int currentIndex = fromLast ? size-1 : 0;
Node<T> n = fromLast ? tail : head;
for (int i = currentIndex; fromLast ? i > index : i < index; i = fromLast ? i-1 : i+1) {
n = fromLast ? n.prev : n.next;
}
return n;
}
@Override
public Iterator<T> iterator() {
return new Iterator<>() {
// for bypass the (h=h.next) part at the next()
Node<T> h = new Node<>(null,null,head);
@Override
public boolean hasNext() {
return h.next != null;
}
@Override
public T next() {
return (h=h.next).data;
}
};
}
}