-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellList.java
More file actions
332 lines (254 loc) · 5.57 KB
/
CellList.java
File metadata and controls
332 lines (254 loc) · 5.57 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//---------------------------------
//assignment 3
//Written by Jiemin Liang 40262509
//----------------------------------
/**
* Name and ID: Jiemin liang 40262509
* COMP 249
* Assignment 3
* Due Date: Dec 2,2024
*/
import java.util.NoSuchElementException;
public class CellList {
/**
* Name and ID: Jiemin liang 40262509
* COMP 249 Assignment 3
* Due Date: Dec 2,2024
*
*
*/
// inner class
private static class CellNode implements Cloneable {
// attributes
private CellPhone c;
private CellNode next;
// constructor
public CellNode() {
c = null;
next = null;
}
public CellNode(CellPhone c, CellList.CellNode x) {
this.c = c;
this.next = x;
}
public CellNode(CellNode temp) throws CloneNotSupportedException {
long n = temp.c.getSerialNum()+1;
c = temp.c.clone(n);
next = null;
}
// clone method
public CellNode clone() throws CloneNotSupportedException {
CellNode temp = (CellList.CellNode) super.clone();
long n = temp.c.getSerialNum()+1;
temp.c = this.c.clone(n);
temp.next = null;
return temp;
}
// getter and setter
public CellPhone getC() {
return c;
}
public void setC(CellPhone c) {
this.c = c;
}
public CellNode getX() {
return next;
}
public void setX(CellNode x) {
this.next = x;
}
}
// attributes
private CellNode head;
private int size = 0;
// constructor
public CellList() {
head = null;
}
// copy constructor
public CellList(CellList cl1) {
if (cl1.head == null) {
head = null;
}
CellPhone c1=new CellPhone(cl1.head.c);
c1.serialNum=c1.serialNum+1;
head = new CellNode(c1, null);
size = 1;
CellNode t2 = head;
CellNode t1 = cl1.head.next;
while (t1 != null) {
CellPhone c2=new CellPhone(t1.c);
c2.serialNum=c2.serialNum+1;
t2.next = new CellNode(c2, null);
t1 = t1.next;
t2 = t2.next;
size++;
}
}
// getter and setter
public CellNode getHead() {
return head;
}
public void setHead(CellNode head) {
this.head = head;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
// add created node at the head of the list
public void addToStart(CellPhone c) {
head = new CellNode(c, head);
size++;
}
// create a node and insert it at the given index
public void insertAtIndex(CellPhone c, int n) {
try {
if (n < 0 || n > size - 1)
throw new NoSuchElementException();
} catch (NoSuchElementException e) {
System.out.println("No Such Element Exists. \nThe Program Terminate.");
System.exit(-1);
}
if (n == 0) {
addToStart(c);
return;
}
CellNode t = head;
int i=0;
while(i< n-1) {
t=t.next;
i++;
}
CellNode temp = new CellNode(c, t.next);
t.next=temp;
t=null;
size++;
}
// delete the node at given index
public void deleteFromIndex(int n) {
try {
if (n < 0 || n > size - 1)
throw new NoSuchElementException();
} catch (NoSuchElementException e) {
System.out.println("No Such Element Exists. \nThe Program Terminate.");
System.exit(-1);
}
if (n == 0) {
head = head.next;
size--;
return;
}
CellNode t = head;
CellNode prev = null;
int i =0;
while(i<n) {
prev=t;
t=t.next;
i++;
}
prev.next=t.next;
size--;
}
// delete the node at the head of the list
public void deleteFromStart() {
if (head == null) {
System.out.println("No Such Element Exists. \nThe Program Terminate.");
System.exit(-1);
}
head = head.next;
size--;
}
// replace the cellphone info at given index
public void replaceAtIndex(CellPhone c, int n) {
try {
if (n < 0 || n > size - 1)
throw new NoSuchElementException();
} catch (NoSuchElementException e) {
System.out.println("No Such Element Exists. \nThe Program Terminate.");
System.exit(-1);
}
if (n == 0) {
CellNode temp = new CellNode(c, head.next);
head = temp;
return;
}
CellNode t = head;
int i=0;
while(i!=n) {
t=t.next;
i++;
}
t.setC(c);
}
/**
* find the pointer of the node by given serial number
* possible have privacy leak, will be better if do a password check before run the method
* need to make sure the pointor is given to who
* @param n
* @return cellnode
*/
public CellNode find(long n) {
CellNode t = head;
for (int i = 0; i < size; i++) {
if (t.c.getSerialNum() == n) {
t.next = null;
return t;
}
t = t.next;
}
System.out.println("The serialnumber is not found.");
return null;
}
// return boolean depends on the given serial number
public boolean contains(long n) {
CellNode t = head;
for (int i = 0; i < size; i++) {
if (t.c.getSerialNum() == n) {
return true;
}
t = t.next;
}
return false;
}
@Override
// equals method
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CellList other = (CellList) obj;
if (other.size != size) {
return false;
}
CellNode t1 = other.head;
CellNode t2 = head;
while (t1 != null) {
if (!t1.c.equals(t2.c)) {
return false;
}
t1 = t1.next;
t2 = t2.next;
}
return t1 == null && t2 == null;
}
@Override
// toString method
public String toString() {
if (head == null) {
return "CellList: []";
} else {
System.out.println("The current size of the list is " + size + ". Here are the contents of the list");
System.out.println("===========================================================================");
CellNode t = head;
while (t != null) {
System.out.print(t.c + " ---> ");
t = t.next;
}
}
return "X ";
}
}