-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
72 lines (64 loc) · 1.18 KB
/
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
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
list* list_init()
{
list* l;
l = malloc(sizeof(*l));
l->next = NULL;
l->word = NULL;
return l;
}
void list_print(const list* head)
{
if (head == NULL)
return;
printf("%s\n", head->word);
list_print(head->next);
}
void list_free(list* head)
{
list* next;
if (head == NULL)
return;
next = head->next;
free(head->word);
free(head);
list_free(next);
}
void list_free_no_words(list* head)
{
list* next;
if (head == NULL)
return;
next = head->next;
free(head);
list_free_no_words(next);
}
int _list_size_tailrec(list* head, int acc)
{
if (head == NULL)
return acc;
return _list_size_tailrec(head->next, acc + 1);
}
int list_size(list* head)
{
return _list_size_tailrec(head, 0);
}
list* list_insert(list* tail)
{
list* child;
child = list_init();
if (tail != NULL)
tail->next = child;
return child;
}
int list_has(list* head, char* value)
{
if (head == NULL)
return 0;
if (strcmp(head->word, value) == 0)
return 1;
return list_has(head->next, value);
}