-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlist.c
78 lines (60 loc) · 1.14 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
73
74
75
76
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "list.h"
#include "hash.h"
#include "io.h"
#include "stack.h"
/*
struct node {
struct node *pNext;
int *pData;
};
typedef struct node node_t;
struct list {
struct node *pFirst;
};
*/
/*
Traversal of a singly linked list is simple,
beginning at the first node and following each next link until we come to the end:
node := list.firstNode
while node not null
(do something with node.data)
node := node.next
*/
int append(list_t *pList, void *pData) {
node_t *pTip;
node_t *pNewNode;
// Creat the new node
pNewNode = malloc(sizeof(node_t));
if (NULL == pNewNode) {
return(-1);
}
// Add the data;
pNewNode->pData = pData;
pNewNode->pNext = NULL;
if (NULL == pList->pFirst) {
pList->pFirst = pNewNode;
} else {
pTip = pList->pFirst;
// Find the last node
while(NULL != pTip->pNext ) {
pTip = pTip->pNext;
}
pTip->pNext = pNewNode;
}
return(0);
}
list_t *init_list(void) {
list_t *pList;
pList = malloc(sizeof(list_t));
if (NULL == pList) {
return(NULL);
}
pList->pFirst = NULL;
return (pList);
}
#ifdef thomas
#endif