/
list-2.c
96 lines (81 loc) · 2.19 KB
/
list-2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _lnode {
char *str;
struct _lnode *next;
} lnode;
lnode *lnode_construct(char *str)
{
lnode *ret_node = (lnode *)malloc(sizeof(lnode));
if (ret_node == NULL) {
fprintf(stderr, "malloc fail!!!!\n");
return NULL;
}
ret_node->str = str;
ret_node->next = NULL;
return ret_node;
}
// insert in front of the list
lnode *lnode_insert(lnode **list, char *str)
{
if (str == NULL) {
return NULL;
}
lnode *new_node = lnode_construct(str);
if (new_node == NULL) {
fprintf(stderr, "malloc fail!!!!\n");
return NULL;
}
new_node->next = *list;
*list = new_node;
return new_node;
}
// remove the first item in the list
lnode *lnode_remove(lnode **list)
{
if (*list == NULL) {
return NULL;
}
lnode *ret_node = *list; // ret_node stores the address to the first item
*list = ret_node->next; // *list updated to the address of the 2nd item
ret_node->next = NULL; // the removed first item is a single-node list
return ret_node;
}
void lnode_destroy(lnode *head)
{
while (head != NULL) {
// whatever we need from head, we have to gather them first
// should not access anything in head after we free it
lnode *tmp = head->next;
free(head->str);
free(head);
head = tmp;
}
}
int main(int argc, char **argv)
{
lnode *head = NULL;
char *new_string = (char *)malloc(sizeof(char)*(strlen("abcd")+1));
strcpy(new_string, "abcd");
lnode *new_node = lnode_insert(&head, new_string);
new_string = (char *)malloc(sizeof(char)*(strlen("efgh")+1));
strcpy(new_string, "efgh");
new_node = lnode_insert(&head, new_string);
new_string = (char *)malloc(sizeof(char)*(strlen("wxyz")+1));
strcpy(new_string, "wxyz");
new_node = lnode_insert(&head, new_string);
new_node = head;
while (new_node != NULL) {
printf("%s->", new_node->str);
new_node = new_node->next;
}
printf("NULL\n");
while (head != NULL) {
lnode *removed_node = lnode_remove(&head);
printf("%s\n", removed_node->str);
lnode_destroy(removed_node);
}
lnode_destroy(head);
return EXIT_SUCCESS;
}