-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventlist.c
More file actions
67 lines (53 loc) · 1.38 KB
/
eventlist.c
File metadata and controls
67 lines (53 loc) · 1.38 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
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "eventlist.h"
int initEventlist(Eventlist **eventlist)
{
*eventlist = NULL;
return 0;
}
int addEvent(Eventlist **eventlist, const char *event, const char *msg, const char *sender)
{
if (event == NULL || msg == NULL || sender == NULL) {
printf("Passed NULL as argument to addEvent, check your code\n");
return -1;
}
Eventlist *new = malloc(sizeof(Eventlist));
bzero(new, sizeof(Eventlist));
new->event = event;
new->message = msg;
new->sender = sender;
new->next = NULL;
if (*eventlist == NULL) {
*eventlist = new;
return 0;
}
while ((*eventlist)->next != NULL) {
*eventlist = (*eventlist)->next;
}
(*eventlist)->next = new;
return 0;
}
int removeEvent(Eventlist **eventlist, Eventlist *removable)
{
Eventlist *prev, *cur, *next;
prev = NULL, cur = *eventlist, next = (*eventlist)->next;
while(cur != removable) {
/*
* (0: prev, prev->next == cur) ; (1: cur, cur->next == next) ; (2: next, next->next)
* (0: cur, cur->next) ; (1: next, next->next) ; (2: next->next)
*/
prev = cur;
cur = next;
next = cur->next; // next->next
}
if (prev != NULL) {
prev->next = next;
} else { /* If prev == NULL, we need to delete the first element,
we do that by updating the pointer */
*eventlist = (*eventlist)->next;
}
return 0;
}