-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
168 lines (144 loc) · 5.57 KB
/
LinkedList.java
File metadata and controls
168 lines (144 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
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 String addEvent(Event e) {// O(n)
if (head == null) {// O(1)
head = new Node<T>((T) e);
return "Event added successfully!";
}
if (dateTimeConflictEvent(head, e))// O(n)
return "Event Not Adeed! DateAndTime Conflict";
if (e.getEventTitle().compareToIgnoreCase(((Event) head.getData()).getEventTitle()) < 0) {// O(1)
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) {// O(n)
if (e.getEventTitle().compareToIgnoreCase(((Event) head.getData()).getEventTitle()) < 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 dateTimeConflictEvent(Node<T> head, Event event) {// cheack if there is an event that has the same DateAndTime
current = head;
while (current != null) {
Event existingEvent = (Event) current.getData();
if (existingEvent.getDataAndTime().equalsIgnoreCase(event.getDataAndTime())) {
return true; // Conflict found
}
current = current.getNext();
}
return false; // No conflict
}
public void RemoveEvent(String ContactEvent) {// O(nm)
if (head == null) {
return;
}
Node<T> current = head;
Node<T> previous = null;
while (current != null) {
if (((Event) current.data).isAppointment()) {// the delete method for Appointment
if (((Event) current.data).getcontactsName().equalsIgnoreCase(ContactEvent)) {
if (previous != null) {
previous.setNext(current.getNext());
current = current.getNext();
} else {
head = current.getNext();
current = head;
}
} else {
previous = current;
current = current.getNext();
}
} else {// delete method for Event
Contact x = (((Event) current.data).contacts.find_name(ContactEvent));
if (x == null) {
previous = current;
current = current.getNext();
} else {
((Event) current.data).Getcontacts().removeContact(x.getContactName());
((Event) current.data).removenamecontact(x.getContactName());
String contactNames = ((Event) current.data).getcontactsName();
if (contactNames.isEmpty() || contactNames.equals(",")
|| containsOnlySpacesOrCommas(contactNames)) {
// Delete the event node if the contact names contain only spaces or commas
if (previous != null) {
previous.setNext(current.getNext());
current = current.getNext();
} else {
head = current.getNext();
current = head;
}
} else {
previous = current;
current = current.getNext();
}
}
}
}
}
public void SearchEvent(Node<T> head, String name, int num) { // O(n)
if (head == null)
return;
current = head;
switch (num) { // start switch
case 1:
while (current != null) {// start while
if (((Event) current.getData()).Getcontacts().find_name(name) != null)
System.out.println(current.data.toString());
current = current.getNext();
} // end while
break;
case 2:
while (current != null) {// start while
if (current.data instanceof Event && ((Event) current.data).getEventTitle().equals(name))
System.out.println(current.data.toString());
current = current.getNext();
} // end while
break;
} // end switch
}
public boolean containsOnlySpacesOrCommas(String str) {
boolean allSpacesOrCommas = true;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ' && str.charAt(i) != ',') {
allSpacesOrCommas = false;
break;
}
}
return allSpacesOrCommas;
}
}