-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
174 lines (155 loc) · 4.8 KB
/
List.java
File metadata and controls
174 lines (155 loc) · 4.8 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
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
/**
* A linked implementation of the List data structure.
* @author Samuel Heath 21725083, Bryan Trac 21704976
*/
public class List {
/** The first element in the list */
private Link before;
/** The last element in the list */
private Link after;
private int size;
public WindowLinked wl;
/**
* Initialises the before and after links.
*/
public List() {
after = new Link(null, null);
before = new Link(null, after);
wl = new WindowLinked();
size = 0;
this.beforeFirst();
}
/**
*
* @param w
* @return
* @throws OutOfBounds
* @throws Underflow
*/
public Object delete() throws Exception {
if (!isBeforeFirst() && !isAfterLast()) {
Object o = wl.link.item;
wl.link.item = wl.link.successor.item;
if (wl.link.successor == this.after) {
this.after = wl.link;
}
wl.link.successor = wl.link.successor.successor;
size--;
return o;
} else throw new Exception("Window is outside of list.");
}
/**
*
* @param e
* @param w
* @return
* @throws OutOfBounds
* @throws Underflow
*/
public Object replace(Object e) throws Exception {
if (!isBeforeFirst() && !isAfterLast()) {
Object o = wl.link.item;
wl.link.item = e;
return o;
} else throw new Exception("Window is outside of list.");
}
public void append(Object o) {
try {
insertAfter(o);
next();
} catch (Exception E) {
E.printStackTrace();
}
}
/**
*
* @param w
* @return
* @throws OutOfBounds
* @throws Underflow
*/
public Object examine() throws Exception {
if (!isBeforeFirst() && !isAfterLast()) {
return wl.link.item;
} else throw new Exception("Window is outside of list.");
}
/**
* Inserts an Object in front of the Window's current link.
* @param e The Object being inserted into the List.
* @param w The Window object.
* @throws OutOfBounds If the Window's Link references the 'before' Link.
*/
public void insertBefore (Object e) throws Exception {
if (!isBeforeFirst()) {
this.size++;
wl.link.successor = new Link(wl.link.item, wl.link.successor);
if (isAfterLast()) after = wl.link.successor;
wl.link.item = e;
wl.link = wl.link.successor;
} else throw new Exception ("Inserting before start of list");
}
/**
*
* @param e The object being inserted into the List.
* @param w The Window object.
* @throws OutOfBounds If the Window's Link references the 'after' Link.
*/
public void insertAfter(Object e) throws Exception {
if (!isAfterLast()) {
this.size++;
wl.link.successor = new Link(e,wl.link.successor);
} else throw new Exception("Inserting after the end of list");
}
/**
* Sets the Window's Link, as the Link which holds the current link as
* successor.
* @param w The Window object.
* @throws OutOfBounds if the Window is before the first element.
*/
public void previous () throws Exception {
if (!isBeforeFirst()) {
Link current = before.successor;
Link previous = before;
while (current != wl.link) {
previous = current;
current = current.successor;
}
wl.link = previous;
} else throw new Exception ("Calling previous before start of list.");
}
/**
* Sets the Window's Link as the successor of the current Link.
* @param w The Window object.
* @throws OutOfBounds if the Window is already after the last element.
*/
public void next () throws Exception {
if (!isAfterLast()) wl.link = wl.link.successor;
else throw new Exception("Calling next after list end.");
}
public void afterLast() {
wl.link = after;
}
public void beforeFirst() {
wl.link = before;
}
public boolean isAfterLast() { return wl.link == after;}
public boolean isBeforeFirst() {return wl.link == before;}
public boolean isEmpty() {return before.successor == after;}
public int getSize() {
return this.size;
}
private class Link {
private Object item;
private Link successor;
public Link(Object item, Link successor) {
this.item = item;
this.successor = successor;
}
}
private class WindowLinked {
public Link link;
public WindowLinked() {
link = null;
}
}
}