-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUnorderedList.java
More file actions
87 lines (76 loc) · 1.98 KB
/
ArrayUnorderedList.java
File metadata and controls
87 lines (76 loc) · 1.98 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
/**
* ArrayUnorderedList represents an array implementation of an unordered list.
*
* @author Dr. Lewis
* @author Dr. Chase
* @version 1.0, 08/12/08
*/
public class ArrayUnorderedList<T> extends ArrayList<T>
implements UnorderedListADT<T>
{
/**
* Creates an empty list using the default capacity.
*/
public ArrayUnorderedList()
{
super();
}
/**
* Creates an empty list using the specified capacity.
*
* @param initialCapacity the integer intial size of the list
*/
public ArrayUnorderedList (int initialCapacity)
{
super(initialCapacity);
}
/**
* Adds the specified element to the front of this list.
*
* @param element the element to be added to the front of the list
*/
public void addToFront (T element)
{
if (size() == list.length)
expandCapacity() ;
for (int i = rear; i > 0; i--) {
list[i] = list[i-1] ;
}
list[0] = element;
rear++;
}
/**
* Adds the specified element to the rear of this list.
*
* @param element the element to be added to the list
*/
public void addToRear (T element)
{
if (size() == list.length)
expandCapacity();
list[rear] = element ;
rear++ ;
}
/**
* Adds the specified element after the specified target element.
* Throws an ElementNotFoundException if the target is not found.
*
* @param element the element to be added after the target element
* @param target the target that the element is to be added after
*/
public void addAfter (T element, T target)
{
if (size() == list.length)
expandCapacity();
int scan = 0;
while (scan < rear && !target.equals(list[scan]))
scan++;
if (scan == rear)
throw new ElementNotFoundException ("list");
scan++;
for (int scan2=rear; scan2 > scan; scan2--)
list[scan2] = list[scan2-1];
list[scan] = element;
rear++;
}
}