-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
207 lines (177 loc) · 3.66 KB
/
linked_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
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
#include <stdlib.h>
#include <stdio.h>
#define GOVERNOR_FIRST 0
#define GOVERNOR_LAST 1
#define GOVERNOR_RND 2
void print_events();
struct Event {
double time;
int type;
struct Event * next;
struct Event * prev;
double arrival_time;
int path;
double wasted_time;
};
struct Event * events = NULL;
struct Event * last = NULL;
struct Event * push_event(struct Event *event){
int added = 0;
if(events != NULL)
{
if(event->time < events->time) // check the first element
{
event->next = events;
event->next->prev = event;
event->prev = NULL;
events = event;
added = 1;
}
struct Event * prev = events;
struct Event * actual = events->next;
while(actual != NULL && added == 0) // check from second to last one
{
if(event->time < actual->time)
{
prev->next = event;
event->prev = prev;
event->next = actual;
event->next->prev = event;
added = 1;
}
prev = prev->next;
actual = actual->next;
}
if(added == 0) // append to list after last one
{
prev->next = event;
event->next = NULL;
event->prev = prev;
last = event;
added = 1;
}
}
else
{
events = event;
events->next = NULL;
events->prev = NULL;
last = event;
}
return event;
}
struct Event * remove_event(struct Event *event){
if(event == NULL){
return NULL;
}
struct Event * after = event->next;
struct Event * before = event->prev;
if(before != NULL){
before->next = before->next->next;
}
else{
events = after;
}
if(after != NULL){
after->prev = after->prev->prev;
}
else{
last = before;
}
event->next = NULL;
event->prev = NULL;
return event;
}
struct Event * pop_event()
{
if(events == NULL){
return NULL;
}
if(events->next == NULL){
struct Event * event = events;
events = NULL;
last = NULL;
return event;
}
struct Event * first = events;
events = events->next;
events->prev = NULL;
first->prev = NULL;
first->next = NULL;
return first;
}
struct Event * remove_last_event_of_type(int type)
{
if(events != NULL){
struct Event * actual = last;
while(actual != NULL){
if(actual->type == type){
return remove_event(actual);
}
actual = actual->prev;
}
fprintf(stderr, "Warning, event type %d not found\n", type);
return NULL;
}
else{
return NULL;
}
}
struct Event * remove_first_event_of_type(int type)
{
if(events != NULL){
struct Event * actual = events;
while(actual != NULL){
if(actual->type == type){
return remove_event(actual);
}
actual = actual->next;
}
fprintf(stderr, "Warning, event type %d not found\n", type);
return NULL;
}
else{
return NULL;
}
}
struct Event * remove_a_event_of_type(int type)
{
fprintf(stderr, "TO IMPLEMENT\n");
return NULL;
}
struct Event * remove_event_of_type(int gov, int type)
{
switch (gov) {
case GOVERNOR_FIRST:
return remove_first_event_of_type(type);
break;
case GOVERNOR_LAST:
return remove_last_event_of_type(type);
break;
case GOVERNOR_RND:
return remove_a_event_of_type(type);
break;
}
return NULL;
}
void print_events()
{
if(events != NULL){
struct Event * actual = events;
while(actual != NULL){
printf(" %f:%d,",actual->time,actual->type);
actual = actual->next;
}
printf("\n");
}
}
void print_events_inverse(){
if(events != NULL){
struct Event * actual = last;
while(actual != NULL){
printf(" %f,",actual->time);
actual = actual->prev;
}
printf("\n");
}
}