-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisty.c
More file actions
198 lines (159 loc) · 3.82 KB
/
Copy pathlisty.c
File metadata and controls
198 lines (159 loc) · 3.82 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
/*
* linkedlist.c
*
* Based on the implementation approach described in "The Practice
* of Programming" by Kernighan and Pike (Addison-Wesley, 1999).
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ics.h"
#include "emalloc.h"
#include "listy.h"
///////////////implementation of mergesort//////////////////////
void merge_sort(node_t** headRef)
{
node_t *head = *headRef;
node_t *a, *b;
//best case: length is 0 or 1
if ((head == NULL) || (head->next == NULL)) {
return;
}
//split head into a & b sublists
front_back_split(head, &a, &b);
//recursively sort the sublists
merge_sort(&a);
merge_sort(&b);
//merge the two lists together
*headRef = sorted_merge(a,b);
}
void front_back_split(node_t *source, node_t **front_ref, node_t **back_ref)
{
node_t *fast, *slow;
slow = source;
fast = source->next;
// slow will be positioned before mid point
while (fast != NULL) {
fast = fast->next;
if (fast != NULL) {
slow = slow->next;
fast = fast->next;
}
}
*front_ref = source;
*back_ref = slow->next;
slow->next = NULL;
}
node_t *sorted_merge(node_t *a, node_t *b) {
node_t *final = NULL;
//best cases:
if (a==NULL) return b;
else if (b==NULL) return a;
if (difftime(a->val.dtstart, b->val.dtstart)>=0) {
final = b;
final-> next = sorted_merge(a, b->next);
} else {
final = a;
final->next = sorted_merge(a->next, b);
}
return final;
}
////////////////////////////////////////////////////////////////
node_t* get_tail(node_t *head) {
node_t *curr = NULL;
if (head != NULL) {
for (curr = head; curr->next !=NULL; curr = curr->next) {
continue;
}
}
return curr;
}
void deallocate_mem_line_list(node_line *head) {
node_line *curr = NULL;
while (head != NULL)
{
curr = head;
head = head->next;
free(curr);
}
}
node_line *new_node1(char* fline) {
//assert( val != NULL);
node_line *temp = (node_line *)emalloc(sizeof(node_line));
strcpy(temp->fline, fline);
temp->next = NULL;
return temp;
}
node_t *new_node(event_t val) {
//assert( val != NULL);
node_t *temp = (node_t *)emalloc(sizeof(node_t));
temp->val = val;
temp->next = NULL;
return temp;
}
node_line *add_front1(node_line *list, node_line *new) {
new->next = list;
return new;
}
node_t *add_front(node_t *list, node_t *new) {
new->next = list;
return new;
}
node_line *add_end1(node_line *list, node_line *new) {
node_line *curr;
if (list == NULL) {
new->next = NULL;
//list = new;
return new;
}
for (curr = list; curr->next != NULL; curr = curr->next);
curr->next = new;
new->next = NULL;
return list;
}
node_t *add_end(node_t *list, node_t *new) {
node_t *curr;
if (list == NULL) {
new->next = NULL;
return new;
}
for (curr = list; curr->next != NULL; curr = curr->next);
curr->next = new;
new->next = NULL;
return list;
}
node_line *peek_front1(node_line *list) {
return list;
}
node_t *peek_front(node_t *list) {
return list;
}
node_line *remove_front1(node_line *list) {
if (list == NULL) {
return NULL;
}
return list->next;
}
node_t *remove_front(node_t *list) {
if (list == NULL) {
return NULL;
}
return list->next;
}
void apply1(node_line *list,
void (*fn)(node_line *list, void *),
void *arg)
{
for ( ; list != NULL; list = list->next) {
(*fn)(list, arg);
}
}
void apply(node_t *list,
void (*fn)(node_t *list, void *),
void *arg)
{
for ( ; list != NULL; list = list->next) {
(*fn)(list, arg);
}
}