-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLinkedUnorderedList.java
executable file
·82 lines (67 loc) · 2.13 KB
/
DoubleLinkedUnorderedList.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
package com.example.doubleLinkedList;
import com.example.exceptions.*;
import com.example.interfaces.UnorderedListADT;
public class DoubleLinkedUnorderedList<T> extends AbstractDoubleLinkedList<T> implements UnorderedListADT<T> {
public DoubleLinkedUnorderedList() {
super();
}
/**
* {@inheritDoc }
*/
@Override
public void addToRear(T element) {
fastAdd(element, this.rear.getPrevious());
}
/**
* {@inheritDoc }
*/
@Override
public void addToFront(T element) {
fastAdd(element, this.front);
}
private void fastAdd(T element, DoubleNode<T> targetNode) {
DoubleNode<T> newNode = new DoubleNode<>(element);
newNode.setNext(targetNode.getNext());
newNode.setPrevious(targetNode);
targetNode.getNext().setPrevious(newNode);
targetNode.setNext(newNode);
this.modCount++;
this.count++;
}
/**
* {@inheritDoc }
*/
@Override
public void addAfter(T element, T target) throws EmptyCollectionException, ElementNotFoundException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
boolean found = false;
DoubleNode<T> targetNode = new DoubleNode<>();
DoubleNode<T> current = this.front.getNext();
while (current != this.rear && !found) {
if (current.getElement().equals(target)) {
targetNode = current;
found = true;
}
current = current.getNext();
}
if (!found) {
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
fastAdd(element, targetNode);
}
@Override
protected String print() {
return super.print();
}
protected String printBack() {
StringBuilder s = new StringBuilder();
DoubleNode<T> current = this.rear.getPrevious();
while (current != this.front) {
s.append(current.getElement()).append(" ");
current = current.getPrevious();
}
return s.toString();
}
}