-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlnklist.c
157 lines (130 loc) · 4.04 KB
/
dlnklist.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "precomp.h"
#include "dlnklist.h"
#ifdef UNIT_TESTING
extern void* _test_malloc(const size_t size, const char* file, const int line);
#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
extern void* _test_calloc(const size_t num, const size_t size, const char* file,
const int line);
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
#endif
void DoubleLinkList_Init(LPDOUBLELINKLIST pDoubleLinkList, DOUBLELINKLISTSORTFUNC pfnSort)
{
pDoubleLinkList->pBegin = NULL;
pDoubleLinkList->pEnd = NULL;
pDoubleLinkList->pHead = NULL;
pDoubleLinkList->pfnSort = pfnSort;
}
LPDOUBLELINKLISTNODE DoubleLinkList_CreateNode(void)
{
LPDOUBLELINKLISTNODE pNode = (LPDOUBLELINKLISTNODE) malloc(sizeof(DOUBLELINKLISTNODE));
if (pNode)
{
pNode->pNext = NULL;
pNode->pPrev = NULL;
pNode->pValue = NULL;
pNode->valueSize = 0;
return pNode;
}
return NULL;
}
/*
* DoubleLinkList_AppendBack
*
* Insert an element at the beginning of double linked list
* `pValue` any data passed by void pointer to be copied
* `valueSize` size of the data to be copied
* `bSeek` - set the pHead pointer to the inserted node
* */
void DoubleLinkList_AppendBack(LPDOUBLELINKLIST pDoubleLinkList, const void* pValue, size_t valueSize, BOOL bSeek)
{
LPDOUBLELINKLISTNODE pNode = DoubleLinkList_CreateNode();
assert(pNode);
pNode->pValue = malloc(valueSize);
if (pNode->pValue) {
pNode->valueSize = valueSize;
memcpy(pNode->pValue, pValue, valueSize);
if (!pDoubleLinkList->pEnd) {
pDoubleLinkList->pEnd = pNode;
}
if (bSeek) {
pDoubleLinkList->pHead = pNode;
}
if (pDoubleLinkList->pBegin) {
pDoubleLinkList->pBegin->pPrev = pNode;
pNode->pNext = pDoubleLinkList->pBegin;
}
pDoubleLinkList->pBegin = pNode;
}
}
/*
* DoubleLinkList_AppendFront
*
* Insert an element at the end of double linked list
* `pValue` any data passed by void pointer to be copied
* `valueSize` size of the data to be copied
* `bSeek` - set the pHead pointer to the inserted node
* */
void DoubleLinkList_AppendFront(LPDOUBLELINKLIST pDoubleLinkList, const void* pValue, size_t valueSize, BOOL bSeek)
{
LPDOUBLELINKLISTNODE pNode = DoubleLinkList_CreateNode();
if (pNode) {
pNode->valueSize = valueSize;
pNode->pValue = malloc(valueSize);
if (pNode->pValue) {
memcpy(pNode->pValue, pValue, valueSize);
if (!pDoubleLinkList->pBegin) {
pDoubleLinkList->pBegin = pNode;
}
if (bSeek) {
pDoubleLinkList->pHead = pNode;
}
if (pDoubleLinkList->pEnd) {
pDoubleLinkList->pEnd->pNext = pNode;
pNode->pPrev = pDoubleLinkList->pEnd;
}
pDoubleLinkList->pEnd = pNode;
}
}
}
/*
* DoubleLinkList_Sort
*
* Sort the provided double linked list by an algorithm provided in pfnSort
* callback function
*
* Note: if pfnSort is not provided, the function will fail
* */
void DoubleLinkList_Sort(LPDOUBLELINKLIST pDoubleLinkList) {
if (!pDoubleLinkList->pfnSort) {
fprintf(stderr, "Error: Sorting function not provided.\n");
return;
}
/* List is empty, nothing to sort */
if (!pDoubleLinkList->pBegin) {
return;
}
for (LPDOUBLELINKLISTNODE pNode1 = pDoubleLinkList->pBegin; pNode1; pNode1 = pNode1->pNext) {
for (LPDOUBLELINKLISTNODE pNode2 = pNode1->pNext; pNode2; pNode2 = pNode2->pNext) {
if (pDoubleLinkList->pfnSort(pNode1->pValue, pNode1->valueSize, pNode2->pValue, pNode2->valueSize) < 0) {
/* Swap data pointers */
void* temp = pNode1->pValue;
pNode1->pValue = pNode2->pValue;
pNode2->pValue = temp;
}
}
}
}
/*
* Function to free memory allocated for the list
*/
void DoubleLinkList_Free(LPDOUBLELINKLIST pDoubleLinkList) {
LPDOUBLELINKLISTNODE pCurrent = pDoubleLinkList->pBegin;
while (pCurrent) {
LPDOUBLELINKLISTNODE pNext = pCurrent->pNext;
free(pCurrent->pValue);
free(pCurrent);
pCurrent = pNext;
}
/* Reset list pointers */
pDoubleLinkList->pBegin = pDoubleLinkList->pEnd = pDoubleLinkList->pHead = NULL;
}