-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked-list.c
66 lines (60 loc) · 1.12 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
#include <stdlib.h>
#include <stdint.h>
typedef struct node {
double value;
struct node *next;
} Node;
typedef struct list {
size_t count;
Node *head;
Node *tail;
} List;
int push(List *list, double value) {
Node *node = malloc(sizeof(Node));
if (node == NULL) {
return -1;
}
node->value = value;
node->next = NULL;
if (list->count == 0) {
list->head = node;
list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
list->count++;
return 0;
}
void display(List *list) {
Node *node = list->head;
while (node != NULL) {
printf("%g ", node->value);
node = node->next;
}
printf("\n");
}
double mean(List *list) {
double sum = 0;
Node *node = list->head;
while (node != NULL) {
sum += node->value;
node = node->next;
}
return sum / list->count;
}
int compare(List *a, List *b) {
if (a->count != b->count) {
return -1;
}
Node *node_a = a->head;
Node *node_b = b->head;
while (node_a != NULL) {
if (node_a->value != node_b->value) {
return -1;
}
node_a = node_a->next;
node_b = node_b->next;
}
return 0;
}