Repository navigation
Expand file tree
/
Copy pathPerformanceList.java
More file actions
233 lines (195 loc) · 7.35 KB
/
Copy pathPerformanceList.java
File metadata and controls
233 lines (195 loc) · 7.35 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import jdk.jshell.spi.ExecutionControlProvider;
/*
PerformanceList
Second part of a three-part program. Attempts to replicate LinkedList functionality
*/
public class PerformanceList {
private PerformanceNode head;
private PerformanceNode tail;
private PerformanceNode cursor;
private PerformanceNode[] list;
// New methods
public void displayAllNodes(){
if(cursor == null) System.out.println("no nodes");
else {
cursor = head;
System.out.println(cursor.toString());
while (cursor.getNext() != null || cursor != tail) { // while cursor is not at tail and not past the specified position
if(cursor == cursor.getNext()){
return;
}else {
cursor = cursor.getNext();
System.out.println(cursor.toString());
// System.out.println("Help i'm stuck");
}
}
}
}
public void addToEnd(PerformanceNode newPerformance){ // Move cursor to newly added node!!
if(tail == null){ // If no performances in list
tail = newPerformance;
head = tail;
cursor = head;
}
else{
tail.setNext(newPerformance); // Add after tail
tail.getNext().setPrev(tail); // Then, new tail's getPrev is prior tail
tail = tail.getNext(); // Then, change tail
tail.setNext(null);
cursor = tail; // Then, move cursor
}
}
// If current node is null, insert at end of list
// Move cursor to newly added node
public void addAfterCurrent(PerformanceNode newPerformance){
if(cursor == null){
addToEnd(newPerformance);
}
else{
cursor.setNext(newPerformance); // Set next node to parameter
cursor.getNext().setPrev(cursor); // Then, set next node's prev
cursor = cursor.getNext(); // Then, move cursor;
}
}
// Change cursor to node after removed node (before if no nodes after)
// If only node, cursor is null
public boolean removeCurrentNode(){
if(cursor == head && cursor == tail){ // If only node
cursor = null;
head = null;
tail = null;
return false;
}
else if(cursor == head || cursor.getPrev() == null){ // If head
head = head.getNext(); // Change head to next node
head.getNext().setPrev(null); // Then, change current head's prev to null
cursor = head;
return true;
}
else if(cursor.getNext() == null || cursor == tail){ // If tail
cursor.getPrev().setNext(null); // Set previous node's next node to null
tail = cursor.getPrev(); // Then, previous node is now tail.
cursor = tail; // Move cursor to tail.
return true;
}
else{
// tail = cursor.getPrev();
// cursor.getPrev().setNext(null);
// cursor = cursor.getPrev();
cursor.getNext().setPrev(cursor.getPrev()); // Change next node's prevNode to two node before
cursor.getPrev().setNext(cursor.getNext()); // Change prev node's next Node to two nodes after
cursor = cursor.getNext();
return true;
}
}
public void displayCurrentPerformance(){
if (cursor == null) System.out.println("No current performance");
else System.out.println(cursor);
}
// Throw exception if no current node
public boolean moveCursorForward() throws Exception {
if(cursor == null) throw new Exception("No current node");
else if(cursor.getNext() == null || cursor == tail) return false; // Cursor is already at tail
cursor = cursor.getNext();
return true;
}
public boolean moveCursorBackward() throws Exception {
if(cursor == null) throw new Exception("No current node");
else if(cursor.getPrev() == null || cursor == head) return false; // Cursor is already at head
cursor = cursor.getPrev();
return true;
}
// Assume first performance is pos 1
// If pos does not exist, return false
public boolean jumpToPosition(int position){
int ct = 1 ;
cursor = head;
if (head == null) return false;
while(cursor != tail && ct != position){ // while cursor is not at tail and not past the specified position
cursor = cursor.getNext();
ct++;
}
if(cursor == tail && ct != position) return false; // If reached end of list, but ct != pos
else return true;
}
public PerformanceNode sortedMerge(PerformanceNode a, PerformanceNode b){
PerformanceNode result = null;
if(a == null) return b;
if(b == null) return a;
if(a.getStartTime() <= b.getStartTime()){
result = a;
result.setNext(sortedMerge(a.getNext(),b));
result.setPrev(sortedMerge(a.getPrev(), b));
}
else{
result = b;
result.setNext(sortedMerge(a,b.getNext()));
result.setPrev(sortedMerge(a, b.getPrev()));
}
return result;
}
public PerformanceNode mergeSort(PerformanceNode h){
if(h == null || h.getNext() == null) return h;
PerformanceNode middle = getMiddle(h);
PerformanceNode nextofmiddle = middle.getNext();
middle.setNext(null);
PerformanceNode left = mergeSort(h);
PerformanceNode right = mergeSort(nextofmiddle);
PerformanceNode sortedlist = sortedMerge(left, right);
return sortedlist;
}
public static PerformanceNode getMiddle(PerformanceNode head){
if(head == null) return head;
PerformanceNode slow = head, fast = head;
while(fast.getNext() != null && fast.getNext().getNext() != null){
slow = slow.getNext();
fast = fast.getNext().getNext();
}
return slow;
}
// Regular methods
public PerformanceList() {
head = null;
tail = null;
cursor = null;
list = null;
}
public PerformanceList(PerformanceNode head, PerformanceNode tail, PerformanceNode cursor, PerformanceNode[] list) {
this.head = head;
this.tail = tail;
this.cursor = cursor;
this.list = list;
}
public PerformanceNode[] getList() {
return list;
}
public void setList(PerformanceNode[] list) {
this.list = list;
}
public PerformanceNode getHead() {
return head;
}
public void setHead(PerformanceNode head) {
this.head = head;
}
public PerformanceNode getTail() {
return tail;
}
public void setTail(PerformanceNode tail) {
this.tail = tail;
}
public PerformanceNode getCursor() {
return cursor;
}
public void setCursor(PerformanceNode cursor) {
this.cursor = cursor;
}
@Override
public String toString() {
return "PerformanceList{" +
"head=" + head +
", tail=" + tail +
", cursor=" + cursor +
'}';
}
}