-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
89 lines (68 loc) · 1.25 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
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "List.h"
#include <stdlib.h>
typedef struct tListNode ListNode;
struct tList
{
ListNode * head;
ListNode * tail;
};
struct tListNode
{
void * data;
ListNode * next;
};
List * list_create()
{
List * list = (List *)malloc(sizeof(List));
list->head = NULL;
list->tail = NULL;
return list;
}
void list_destroy(List * pList)
{
while (pList->head)
list_remove_head(pList);
free(pList);
}
void list_add_head(List * pList, void * data)
{
ListNode * node = (ListNode*)malloc(sizeof(ListNode));
node->data = data;
node->next = pList->head;
pList->head = node;
if (!pList->tail)
pList->tail = node;
}
void list_add_tail(List * pList, void * data)
{
ListNode * node = (ListNode*)malloc(sizeof(ListNode));
node->data = data;
node->next = NULL;
if (pList->tail)
pList->tail->next = node;
else
pList->head = node;
pList->tail = node;
}
void list_remove_head(List * pList)
{
if (!pList->head)
return;
ListNode * oldHead = pList->head;
pList->head = oldHead->next;
if (!pList->head)
pList->tail = NULL;
free(oldHead);
}
void * list_get_head_data(List * pList)
{
if (!pList->head)
return NULL;
return pList->head->data;
}
void * list_get_tail_data(List * pList)
{
if (!pList->tail)
return NULL;
return pList->tail->data;
}