-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassenger.c
140 lines (117 loc) · 2.37 KB
/
passenger.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
/*
#@*@&* ECF KILLED MY USB!
*/
#include "ttc.h"
#include<stdio.h>
#include<stdlib.h>
struct passenger* make_passenger()
{
//Creates a passenger structure, and returns a pointer to the struct
//PRE: none
//POST: returns a pointer to the passenger
struct passenger *dude = malloc(sizeof (struct passenger));
dude->waiting = 0;
dude->next = NULL;
return dude;
}
void insert_passenger_after(struct passenger *node, struct passenger *new_node)
{
//Inserts a passenger in the linked list after the passenger pointed to by node
//PRE: node != NULL and new_node != NULL
//POST: none
assert ((node != NULL) && (new_node != NULL));
node->next = new_node;
}
void print_passenger(struct passenger *node)
{
//Prints a passenger's waiting time. If node == NULL, prints NULL.
//PRE: none
//POST: none
if (node != NULL)
{
printf("%d",node->waiting);
}
else
{
printf("NULL");
}
}
void print_passenger_list(struct passenger *first)
{
//Prints the waiting times for all passengers in the linked list.
//PRE: none
//POST: none
struct passenger *curr;
printf("Waiting times: \n");
for(curr = first; curr != NULL; curr = curr->next)
{
printf("%d\n", curr->waiting);
}
}
void remove_first_passenger(struct passenger **first)
{
//The node MUST be the first node, else null pointer exception!
assert (*first != NULL);
//store target node so we can delete it
struct passenger *terminate = *first;
*first = (*first)->next;
free(terminate);
}
void clear_passenger_list(struct passenger **first)
{
struct passenger *curr;
struct passenger *deleteme;
for(curr = *first; curr != NULL;)
{
deleteme = curr;
curr = curr->next;
free(deleteme);
}
*first = NULL;
}
void increment_passenger_list(struct passenger *first)
{
struct passenger *curr;
assert (first != NULL);
for(curr = first; curr != NULL; curr = curr->next)
{
(curr->waiting)++;
}
}
double average_passenger_list(struct passenger *first)
{
struct passenger *curr;
double sum = 0;
double counter = 0;
if (first != NULL)
{
for(curr = first; curr != NULL; curr = curr->next)
{
sum += curr->waiting;
counter++;
}
sum = sum / counter;
return sum;
}
else
{
return -1;
}
}
int num_passengers(struct passenger *first)
{
struct passenger *curr;
int counter = 0;
if (first != NULL)
{
for(curr = first; curr != NULL; curr = curr->next)
{
counter++;
}
return counter;
}
else
{
return -0;
}
}