-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
188 lines (166 loc) · 5.02 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Implementation of a List as a LinkedList
*
* @author Laurent Mignot
*/
public class LinkedList implements List {
private int size;
private Node head;
private Node tail;
public LinkedList () {
this.size = 0;
this.head = null;
this.tail = null;
}
/**
* @see List#isEmpty()
*/
@Override
public boolean isEmpty () {
return this.size == 0;
}
/**
* @see List#size()
*/
@Override
public int size () {
return this.size;
}
/**
* @see List#get()
*/
@Override
public ReturnObject get (int index) {
if (this.isEmpty()) {
return new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
}
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
if (index == 0) {
return new ReturnObjectImpl(this.head.getValue());
} else if (index == (this.size - 1)) {
return new ReturnObjectImpl(this.tail.getValue());
} else {
return new ReturnObjectImpl(this.getNodeAtIndex(index).getValue());
}
}
/**
* I've chosen to not check for isEmpty in this scenario as
* the isOutOfBounds check would return true for an empty structure
* empty size: (0 - 1 = -1 = < 0)
* @see List#remove()
*/
@Override
public ReturnObject remove (int index) {
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
Node toRemove;
if (index == 0) {
toRemove = this.head;
this.head = toRemove.getNext();
} else if (index == (this.size - 1)) {
toRemove = this.tail;
this.tail = toRemove.getPrev();
} else {
toRemove = this.getNodeAtIndex(index);
}
Node before = toRemove.getPrev();
Node after = toRemove.getNext();
if (before != null) { before.setNext(after); }
if (after != null) { after.setPrev(before); }
this.size--;
return new ReturnObjectImpl(toRemove.getValue());
}
/**
* @see List#add(int, Object)
*/
@Override
public ReturnObject add (int index, Object item) {
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
if (item == null) {
return new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
}
Node newNode = new Node(item);
if (index == 0) {
Node first = this.head;
this.head = newNode;
this.head.setNext(first);
first.setPrev(this.head);
} else if (index == (this.size - 1)) {
Node last = this.tail;
this.tail = newNode;
this.tail.setPrev(last);
last.setNext(this.tail);
} else {
Node addAfter = this.getNodeAtIndex(index - 1);
newNode.setNext(addAfter.getNext());
addAfter.getNext().setPrev(newNode);
addAfter.setNext(newNode);
newNode.setPrev(addAfter);
}
this.size++;
return new ReturnObjectImpl(null);
}
/**
* @see List#add(Object)
*/
@Override
public ReturnObject add (Object item) {
if (item == null) {
return new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
}
if (this.head == null) {
this.head = new Node(item);
this.tail = this.head;
} else {
Node last = this.tail;
this.tail = new Node(item);
this.tail.setPrev(last);
last.setNext(this.tail);
}
this.size++;
return new ReturnObjectImpl(null);
}
/**
* Retrieves a Node in the list at the given index
* @param index
* @return the Node at the given index, or an empty
* Node if given index is out of bounds
*/
protected Node getNodeAtIndex (int index) {
if (this.isOutOfBounds(index)) {
return new Node(null);
}
int currentIndex = 0;
Node current;
if (index > Math.abs((this.size - 1) / 2)) {
current = this.tail;
currentIndex = this.size - 1;
while (current.getPrev() != null) {
if (currentIndex == index) { break; }
current = current.getPrev();
currentIndex--;
}
} else {
current = this.head;
while (current.getNext() != null) {
if (currentIndex == index) { break; }
current = current.getNext();
currentIndex++;
}
}
return current;
}
/**
* Check whether the given index is out of bounds of the data structure
* @param index the index to check
* @return true if out of bounds, false otherwise
*/
private boolean isOutOfBounds (int index) {
return (index < 0 || index > (this.size - 1));
}
}