-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventList.c
296 lines (253 loc) · 7.59 KB
/
eventList.c
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
/**
* @Author: Izhar Shaikh
* @Date: 2017-02-08T03:15:44-05:00
* @Email: [email protected]
* @Filename: eventList.c
* @Last modified by: Izhar Shaikh
* @Last modified time: 2017-02-08T04:34:34-05:00
*/
#include <stdlib.h>
#include <string.h>
#include "calenderFilter.h"
#include "debugMacros.h"
/* returns the list of the length */
int listLength(node_t *head)
{
node_t *current = head;
int count = 0;
while(current != NULL)
{
++count;
current = current->next;
}
return count;
}
/* displays the contents of the list */
void displayList(node_t *head)
{
node_t *current = head;
//print_output("\t%s\n", "***List contents:");
while(current != NULL)
{
dbg_trace("\tMode: %c, Title: %s, Date: %s, Time: %s, Location: %s\n",
current->event.mode, current->event.title, current->event.date,
current->event.time, current->event.location);
current = current->next;
}
//print_output("\t%s\n","***End!");
}
/* insert elements in the list */
int insertNode(node_t **head, int position, event_t *event)
{
node_t *newNode, *previousNode, *positionNode;
int count=1;
// get the current head of the list
positionNode = *head;
previousNode = *head;
// Error checking
if(position < 1){
dbg_trace("Invalid position: %d. Must be greater than or equal to 1.\n", position);
return FAIL;
}
// Get the new node and store the data in it
newNode = (node_t *) calloc(1, sizeof(node_t));
if(newNode == NULL){
dbg_info("Can't allocate memory for new node! Exiting!\n");
return FAIL;
}
memcpy(&newNode->event, event, sizeof(event_t));
newNode->next = NULL;
// Inserting at the beginning of the list
if(position == 1)
{
newNode->next = positionNode; // newNode points to positionNode now
*head = newNode; // New node is head now
}
else
{
// Traverse until we find the desired position of the node or reach the end,
// insert the node at the position of whichever earliest
while((positionNode != NULL) && (count < position))
{
count++;
previousNode = positionNode;
positionNode = positionNode->next;
}
previousNode->next = newNode;
newNode->next = positionNode;
}
dbg_trace("inserted '%s:%s' at position %d in '%s' list.\n",
newNode->event.title, newNode->event.time, position, newNode->event.date);
return SUCCESS;
}
/* Insert events into the list in an sorted timed order for some date */
int sortedInsert(node_t **head, event_t *event)
{
int position = -1;
// Get the new node and store the data in it
node_t *newNode = (node_t *) calloc(1, sizeof(node_t));
if(newNode == NULL){
dbg_info("Can't allocate memory for new node! Exiting!\n");
return FAIL;
}
memcpy(&newNode->event, event, sizeof(event_t));
newNode->next = NULL;
// Special case for the head end
if (*head == NULL || isEarlierInTime(&newNode->event, &(*head)->event)) {
newNode->next = *head;
*head = newNode;
position = 1;
return position;
}
else {
// Locate the node before the point of insertion
node_t *current = *head;
position = 1;
while (current->next != NULL && isEarlierInTime(¤t->next->event, &newNode->event)) {
current = current->next;
position++;
}
newNode->next = current->next;
current->next = newNode;
position++;
}
return position;
}
// Helper function for sortEventList(): adds events into the list in an sorted order
static void sortEventListHelper(node_t **head, node_t *newNode)
{
// Special case for the head end
if (*head == NULL || isEarlierInTime(&newNode->event, &(*head)->event)) {
newNode->next = *head;
*head = newNode;
}
else {
// Locate the node before the point of insertion
node_t *current = *head;
while (current->next != NULL && isEarlierInTime(¤t->next->event, &newNode->event)) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
// Given a list, change it to be in sorted order (using sortEventListHelper()).
void sortEventList(node_t **head)
{
node_t *result = NULL; // build the answer here
node_t *current = *head; // iterate over the original list
node_t *next;
while (current != NULL) {
next = current->next; // tricky - note the next pointer before we change it
sortEventListHelper(&result, current);
current = next;
}
// Update the list head in the caller
*head = result;
}
/* deletes elements from the list */
int deleteNode(node_t **head, int position)
{
node_t *previousNode, *positionNode;
int count=1;
// get the current head of the list
positionNode = *head;
previousNode = *head;
// Error checking
if(positionNode == NULL){
dbg_info("List is already empty!\n");
return FAIL;
}
if(position < 1){
dbg_trace("Invalid position: %d. Must be greater than or equal to 1.\n", position);
return FAIL;
}
// Deleting the node from the beginning of the list
if(position == 1)
{
*head = positionNode->next; // Update the head to next node
dbg_trace("deleted '%s:%s' at position %d in '%s' list.\n",
positionNode->event.title, positionNode->event.time, position, positionNode->event.date);
free(positionNode);
positionNode->next = NULL; // Remove the first node
}
else
{
// Traverse until we find the desired position of the node or reach the end,
// delete the node at the position of whichever earliest
while((positionNode != NULL) && (count < position))
{
count++;
previousNode = positionNode;
positionNode = positionNode->next;
}
if(positionNode == NULL)
{
dbg_info("Reached the end of the list. No node to delete! Returning.\n");
return FAIL;
}
previousNode->next = positionNode->next;
dbg_trace("deleted '%s:%s' at position %d in '%s' list.\n",
positionNode->event.title, positionNode->event.time, position, positionNode->event.date);
free(positionNode);
positionNode->next = NULL;
}
return SUCCESS;
}
/* get the event data from the position (1 to listLength(list), inclusive) */
int getNode(node_t *head, int position, event_t *outEvent)
{
node_t *current = head;
if((position < 1) || (position > listLength(head))){
dbg_trace("Invalid position: %d, must be between 1 to %d (listLength)\n",
position, listLength(head));
return FAIL;
}
if(current == NULL){
dbg_info("Empty List!\n");
return FAIL;
}
while(--position){
current = current->next;
}
// Copy the event data into requested struct
if(current != NULL){
memset(outEvent, 0 , sizeof(event_t));
memcpy(outEvent, ¤t->event, sizeof(event_t));
}
return SUCCESS;
}
/* UPDATES only "time" and "location" fields of the node at position (range: 1 to listLength(list)) */
int setNode(node_t *head, int position, event_t *srcEvent)
{
node_t *current = head;
if((position < 1) || (position > listLength(head))){
dbg_trace("Invalid position: %d, must be between 1 to %d (listLength)\n",
position, listLength(head));
return FAIL;
}
if(current == NULL){
dbg_info("Empty List!\n");
return FAIL;
}
while(--position){
current = current->next;
}
// Copy the event data into requested struct
return updateEvent(¤t->event, srcEvent);
}
/* Deletes the entire linked list */
void deleteList(node_t **head)
{
node_t *auxNode, *current;
// make the current one point to the head
current = *head;
while(current != NULL)
{
auxNode = current->next;
free(current);
current = auxNode;
}
// Make the change visible to the caller
*head = NULL;
}