-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedMngr.java
More file actions
305 lines (273 loc) · 8.49 KB
/
Copy pathlinkedMngr.java
File metadata and controls
305 lines (273 loc) · 8.49 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//CS 145
//Duncan Jackson
//Linked List Lab
//Extra credit:
// linkedMngr() constructor passing array of linked list objects into array
//Extra credit: implementation of doubly linked list at the following methods
// addEnd() doubly linked implementation
// addFirst() doubly linked implementation
// addN() doubly linked implementation
// removeFirst() doubly linked implementation
// removeLast() doubly linked implementation
// removeN() doubly linked implementation
// get() doubly linked implementation, traverses front-to-mid or back-to-mid based on n
public class linkedMngr {
linkedObj head;
linkedObj tail;
int size;
/////////////////
//constructors//
///////////////
public linkedMngr(){
//empty linked list
}
public linkedMngr(linkedObj first){
//linked list with 1 node
addFirst(first);
}
public linkedMngr(linkedObj first, linkedObj last){
//linkedlist with 2 nodes
addFirst(first);
addEnd(last);
}
//EC passing array of linked list objects into array
public linkedMngr(linkedObj[] listArr){
for(int i = 0; i <= listArr.length - 1; i++){
if(i == 0 ){
addFirst(listArr[i]);
}
else{
addEnd(listArr[i]);
}
}
}
/////////////////////////////////////
//linked list manipulation methods//
///////////////////////////////////
//Add to end
//EC addEnd() doubly linked implementation
private void addEnd(linkedObj obj){
if (size == 0){
this.head = obj;
this.head.last = null;
this.head.next = null;
size++;
return;
} else if (size == 1){
this.tail = obj;
this.tail.last = this.head;
this.head.next = this.tail;
this.tail.next = null;
size++;
return;
}
obj.last = this.tail;
this.tail.next = obj;
this.tail = obj;
this.tail.next = null;
size++;
}
//Add to first
//EC addFirst() doubly linked implementation
private void addFirst(linkedObj obj){
if (!(this.head == null)) this.head.last = obj;
obj.next = this.head;
obj.last = null;
if (size == 1) this.tail = this.head;
this.head = obj;
size++;
}
//Inserts object at place n, first node of list is n = 0
//EC addN() doubly linked implementation
private void addN(linkedObj obj, int n){
if (n==0) {
addFirst(obj);
return;
} else if (n == size){
addEnd(obj);
return;
}
linkedObj target;
target = get(n-1);
obj.next = target.next;
if (!(obj.next == null)) obj.next.last = obj;
obj.last = target;
target.next = obj;
size++;
}
//remove first
//EC removeFirst() doubly linked implementation
private void removeFirst(){
linkedObj oldHead = this.head;
if (!(this.head.next == null)){
this.head = this.head.next;
this.head.last = null;
if (size == 2) this.tail = null;
}else{
this.head = null;
}
delete(oldHead);
size--;
}
//remove end
//EC removeLast() doubly linked implementation
private void removeLast(){
linkedObj oldTail = this.tail;
if (size==2) {
this.head.next = null;
this.tail = null;
} else if(!(this.tail.last == null)){
this.tail = this.tail.last;
this.tail.next = null;
}
delete(oldTail);
size--;
}
//remove at object n
//EC removeN() doubly linked implementation
private void removeN(int n){
if(n==0){
removeFirst();
}else if(n == size-1){
removeLast();
}else {
linkedObj target = get(n);
linkedObj tHead = target.last;
linkedObj tTail = target.next;
tHead.next = tTail;
tTail.last = tHead;
delete(target);
size--; }
}
//appends to end
public void add(linkedObj obj){
addEnd(obj);
}
//public insert method
public void insert(linkedObj obj, int n){
addN(obj, n);
}
//public remove method
public void remove(int n){
removeN(n);
}
/////////////////////
//instance methods//
///////////////////
//gets object at place n, list starts at n = 0
//EC get() doubly linked implementation, traverses front-to-mid or back-to-mid based on n
public linkedObj get(int n) {
linkedObj iter;
int i;
if (n <= size/2){ //traversal from start to middle
i = 0;
iter = this.head;
while (i < n){
iter = iter.next;
i++; }
} else { //traversal from end to middle + 1
i = size-1;
iter =this.tail;
while(i > n){
iter = iter.last;
i--;
}
}
return iter;
}
//instance method of print
public void print(){
linkedMngr.print(this);
}
//instance method of print node n
public void print(int n){
linkedMngr.print(this.get(n));
}
//instance method of size
public int size(){
assert size == linkedMngr.size(this.head);
return size;
}
//instance method of clear
public void clear(){
linkedMngr.clear(this);
}
//instance method of toString
@Override
public String toString(){
return linkedMngr.toString(this.head);
}
//instance method of indexOf
public int indexOf(String targetFName){
return linkedMngr.indexOf(this.head, targetFName, 0);
}
//instance method for contains, returns true if !(indexOf == -1)
public boolean contains(String targetFName){
return !(this.indexOf(targetFName) == -1);
}
///////////////////
//Static methods//
/////////////////
//print linkedMngr, starts at head and prints each node until end of list is reached
private static void print(linkedMngr obj){
assert obj.head != null;
linkedObj iter = obj.head;
int i = 0;
System.out.println("Node #: " + i + " First Name: " + iter.fName + " Last Name: " + iter.lName + " City: " + iter.city + " Address: " + iter.address + " Phone Number: " + iter.phoneNumber);
i++;
while(!(iter.next == null)){
iter = iter.next;
System.out.println("Node #: " + i + " First Name: " + iter.fName + " Last Name: " + iter.lName + " City: " + iter.city + " Address: " + iter.address + " Phone Number: " + iter.phoneNumber);
i++;
}
}
//Print linkedObj
private static void print(linkedObj obj){
System.out.println(obj.fName + " " + obj.lName + " " + obj.city + " " + obj.address + " " + obj.phoneNumber);
}
//recursize implementation of size when passing in a node goes from front --> end
private static int size(linkedObj obj){
if(obj == null){
return 0;
}
return size(obj.next)+ 1;
}
//clear(), clears each link between nodes and clears out the head and tail of the list
private static void clear(linkedMngr obj){
linkedMngr.clear(obj.head);
obj.head = null;
obj.tail = null;
obj.size = 0;
}
//recursively clear each link at in each node, should not be called directly
private static void clear(linkedObj obj){
if (obj == null){
return;
}
clear(obj.next);
obj.next = null;
obj.last = null;
}
//deletes list links in an object
private static void delete(linkedObj obj){
obj.next = null;
obj.last = null;
}
//to string, builds a string recursively with each node mapped out from front to back
private static String toString(linkedObj obj){
if (obj == null){
return "null";
}
return obj.fName + " <--> " + toString(obj.next);
}
//index of returns the index of an object based on it's first name value, traverses from front to back and returns first match
private static int indexOf(linkedObj obj, String target, int index){
if(obj == null){
return -1;
}
if(obj.fName.equals(target)){
return index;
}
return indexOf(obj.next, target, index +1);
}
}