-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistops.c
83 lines (69 loc) · 1.32 KB
/
listops.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
#include <assert.h>
#include <stdlib.h>
//#define MAXVAL 99
//#define MINVAL 1
#define MAXID 7
typedef struct cus_rec_t data_t;
typedef struct node node_t;
struct cus_rec_t {
char cus_ID_rep[MAXID];
int* visits;
};
struct node {
data_t data;
node_t *next;
};
typedef struct {
node_t *head;
node_t *foot;
} list_t;
list_t
*make_empty_list(void) {
list_t *list;
list = (list_t*)malloc(sizeof(*list));
assert(list!=NULL);
list->head = list->foot = NULL;
return list;
}
void
free_list(list_t *list) {
node_t *curr, *prev;
assert(list!=NULL);
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
free(prev);
}
free(list);
}
list_t*
insert_at_head(list_t *list, data_t value) {
node_t *new;
new = (node_t*)malloc(sizeof(*new));
assert(list!=NULL && new!=NULL);
new->data = value;
new->next = list->head;
list->head = new;
if (list->foot==NULL) {
/* this is the first insertion into the list */
list->foot = new;
}
return list;
}
list_t*
insert_at_foot(list_t *list, data_t value) {
node_t *new;
new = (node_t*)malloc(sizeof(*new));
assert(list!=NULL && new!=NULL);
new->data = value;
new->next = NULL;
if (list->foot==NULL) {
/* this is the first insertion into the list */
list->head = list->foot = new;
} else {
list->foot->next = new;
list->foot = new;
}
return list;
}