-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_list.c
85 lines (80 loc) · 2.29 KB
/
mail_list.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
#include "mail_list.h"
mail_node* init_mail_list() {
mail_node *dummy_head = malloc(sizeof(mail_node));
if(dummy_head == NULL) {
perror("linkedlist: malloc error");
exit(1);
}
dummy_head->next = NULL;
return dummy_head;
}
int insert_mail(update* m, mail_node* dummy_head) {
mail_node *curr = malloc(sizeof(mail_node));
if(curr == NULL) {
perror("linkedlist: malloc error");
exit(1);
}
update* to_be_insert = malloc(sizeof(update));
if(to_be_insert == NULL) {
perror("linkedlist: malloc error");
exit(1);
}
memcpy(to_be_insert, m, sizeof(update));
curr->key = &to_be_insert->stamp;
curr->value = to_be_insert;
mail_node* head = dummy_head;
while(head->next != NULL) {
if(compare(curr, head->next) > 0) {
break;
} else if(compare(curr, head->next) == 0) {
return -1;
}
head = head->next;
}
curr->next = head->next;
head->next = curr;
return 1;
}
// Compare two mail_nodes, return -1 if the first argument is smaller, return 0 if equal, return 1 if the first argument is larger
int compare(mail_node* node1, mail_node* node2) {
if(node1->key->time_stamp < node2->key->time_stamp) {
return -1;
} else if(node1->key->time_stamp > node2->key->time_stamp) {
return 1;
} else {
if(node1->key->server_id < node2->key->server_id) {
return -1;
} else if(node1->key->server_id > node2->key->server_id) {
return 1;
} else {
return 0;
}
}
}
int insert_updatemail(update* m, mail_node* dummy_head) {
mail_node *curr = malloc(sizeof(mail_node));
if(curr == NULL) {
perror("linkedlist: malloc error");
exit(1);
}
update* to_be_insert = malloc(sizeof(update));
if(to_be_insert == NULL) {
perror("linkedlist: malloc error");
exit(1);
}
memcpy(to_be_insert, m, sizeof(update));
curr->key = &to_be_insert->stamp;
curr->value = to_be_insert;
mail_node* head = dummy_head;
while(head->next != NULL) {
if(compare(curr, head->next) < 0) {
break;
} else if(compare(curr, head->next) == 0) {
return -1;
}
head = head->next;
}
curr->next = head->next;
head->next = curr;
return 1;
}