-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayUnorderedList.java
executable file
·82 lines (69 loc) · 1.87 KB
/
ArrayUnorderedList.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.arrayList;
import com.example.exceptions.*;
import com.example.interfaces.UnorderedListADT;
/**
* @author Micael
*/
public class ArrayUnorderedList<T> extends AbstractArrayList<T> implements UnorderedListADT<T> {
public ArrayUnorderedList() {
super();
}
public ArrayUnorderedList(int initialCapacity) {
super(initialCapacity);
}
/**
* {@inheritDoc }
*/
@Override
public void addToFront(T element) {
if (this.rear == this.list.length) {
super.expandCapacity();
}
shiftRight(this.front);
this.list[front] = element;
this.rear++;
this.count++;
this.modCount++;
}
/**
* {@inheritDoc }
*/
@Override
public void addToRear(T element) {
if (this.rear == this.list.length) {
super.expandCapacity();
}
this.list[this.rear] = element;
this.rear++;
this.count++;
this.modCount++;
}
/**
* {@inheritDoc }
*/
@Override
public void addAfter(T element, T target) throws EmptyCollectionException, ElementNotFoundException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
boolean found = false;
int indexToAdd = 0;
while (indexToAdd < this.rear && !found) {
if (this.list[indexToAdd].equals(target)) {
found = true;
}
indexToAdd++;
}
if (!found) {
throw new ElementNotFoundException(ElementNotFoundException.TARGET_NOT_FOUND);
}
if (this.rear == this.list.length) {
super.expandCapacity();
}
shiftRight(indexToAdd);
this.list[indexToAdd] = element;
this.rear++;
this.count++;
this.modCount++;
}
}