-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
298 lines (261 loc) · 10.6 KB
/
LinkedList.java
File metadata and controls
298 lines (261 loc) · 10.6 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
public class LinkedList<T> { // start LinkedList
private Node<T> head;
private Node<T> current;
public boolean empty() {
return head == null;
}
public boolean last() {
return current.getNext() == null;
}
public void findfirst() {
current = head;
}
public void findnext() {
current = current.getNext();
}
public T retrieve() {
return current.getData();
}
public Node<T> getHead() {
return head;
}
public void add(T val) {
if (val instanceof Contact) {
System.out.println(addContact((Contact) val));
return;
}
if (val instanceof Event) {
System.out.println(addEvent((Event) val));
return;
}
System.out.println("Can't add this type.");
}
private String addContact(Contact c) {
if (head == null) {
head = new Node<T>((T) c);
return "Contact added successfully!";
}
boolean contactExists = existForNumAndName(head, c.getContactName(), c.getPhoneNumber());
if (contactExists)
return "The contact already exists.";
if (head.getData() instanceof Contact && c.compareTo(((Contact) head.getData())) < 0) {
Node<T> temp = head;
head = new Node<T>((T) c);
head.setNext(temp);
return "Contact added successfully!";
}
Node<T> newNode = new Node<T>((T) c);
current = head;
Node<T> prev = null;
while (current != null) {
if (current.getData() instanceof Contact) {
if (c.compareTo(((Contact) current.getData())) < 0) {
newNode.setNext(current);
prev.setNext(newNode);
current = newNode;
return "Contact added successfully!";
}
}
prev = current;
current = current.getNext();
}
if (current == null) {
prev.setNext(newNode);
current = head;
return "Contact added successfully!";
}
return "Contact Not added successfully!";
}
private String addEvent(Event e) {
if (head == null) {
head = new Node<T>((T) e);
return "Event added successfully!";
}
if (dateTimeConflict(head, e)) // new edit
return "Event Not Adeed! DateAndTime Conflict";
if (head.getData() instanceof Event && e.compareTo(((Event) head.getData())) < 0) {
Node<T> temp = head;
head = new Node<T>((T) e);
head.setNext(temp);
return "Event added successfully!";
}
Node<T> newNode = new Node<T>((T) e);
current = head;
Node<T> prev = null;
while (current != null) {
if (current.getData() instanceof Event && e.compareTo((Event) current.getData()) < 0) {
newNode.setNext(current);
prev.setNext(newNode);
current = newNode;
return "Event added successfully!";
}
prev = current;
current = current.getNext();
}
if (current == null) {
prev.setNext(newNode);
current = head;
return "Event added successfully!";
}
return "Event Not Adeed!";
}
private boolean existForNumAndName(Node<T> head, String contactName, String phoneNumber) {// check if number or name
// have been used before
// in contact.
if (SearchContact(head, contactName, 1) == null && SearchContact(head, phoneNumber, 2) == null)
return false;
else
return true;
}
private boolean dateTimeConflict(Node<T> head, Event event) {// cheack if there is an event that has the same
// DateAndTime and the same name, Assuming the name of
// contact is unique.
current = head;
while (current != null) {
if (current.getData() instanceof Event) {
Event existingEvent = (Event) current.getData();
if (existingEvent.getContactName().equalsIgnoreCase(event.getContactName())
&& existingEvent.getDataAndTime().equalsIgnoreCase(event.getDataAndTime()))
return true;
}
current = current.getNext();
}
return false;
}
public void RemoveContact(String phone_number) {
if (head == null) {
System.out.println("Contact not found");
return;
}
if (head.data instanceof Contact && ((Contact) head.data).getPhoneNumber().equalsIgnoreCase(phone_number)) {
head = head.getNext();
System.out.println("Contact deleted");
return;
}
current = head;
while (current.getNext() != null) {
if (current.getNext().data instanceof Contact) {
if (((Contact) current.getNext().data).getPhoneNumber().equalsIgnoreCase(phone_number)) {
if (current.getNext().getNext() != null) {
current.setNext(current.getNext().getNext());
} else {
current.setNext(null);
}
System.out.println("Contact deleted");
return;
}
}
current = current.getNext();
}
System.out.println("Contact not found");
}// end remove
public void RemoveEvent(String ContactEvent) {
if (head == null) {
return;
}
while (head != null && head.data instanceof Event
&& ((Event) head.data).getContactName().equalsIgnoreCase(ContactEvent)) {
head = head.getNext();
}
Node<T> current = head;
Node<T> prev = null;
while (current != null) {
if (current.data instanceof Event
&& ((Event) current.data).getContactName().equalsIgnoreCase(ContactEvent)) {
if (prev != null) {
prev.setNext(current.getNext());
current = prev.getNext();
} else {
head = current.getNext();
current = head;
}
} else {
prev = current;
current = current.getNext();
}
}
}
public T SearchContact(Node<T> head, String name, int num) { // start SearchContact
if (head == null) {
return null;
}
current = head;
switch (num) { // start switch
case 1:
while (current != null) {// start while
if (current.data instanceof Contact
&& ((Contact) current.data).getContactName().equalsIgnoreCase(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
case 2:
while (current != null) {// start while
if (current.data instanceof Contact && ((Contact) current.data).getPhoneNumber().equals(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
case 3:
while (current != null) {// start while
if (current.data instanceof Contact && ((Contact) current.data).getEmail().equals(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
case 4:
while (current != null) {// start while
if (current.data instanceof Contact && ((Contact) current.data).getAddress().equals(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
case 5:
while (current != null) {// start while
if (current.data instanceof Contact && ((Contact) current.data).getBirthday().equals(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
} // end switch
return null;
}// end Search
public T SearchEvent(Node<T> head, String name, int num) { // start PrintEvent
if (head == null) { // start if
return null;
} // end if
current = head;
switch (num) { // start switch
case 1:
while (current != null) {// start while
if (current.data instanceof Event && ((Event) current.data).getContactName().equals(name)) {// start
// if
return current.data;
} // end if
current = current.getNext();
} // end while
break;
case 2:
while (current != null) {// start while
if (current.data instanceof Event && ((Event) current.data).getEventTitle().equals(name)) {// start
// if
System.out.println("Event found!");
return current.data;
} // end if
current = current.getNext();
} // end while
break;
} // end switch
return null;
}
} // end LinkedList