-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
185 lines (143 loc) · 3.37 KB
/
List.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef LIST
#define LIST value
// This is more of a proity_queue rather then a list but fuck it
#include <assert.h>
//Forward decaleration
typedef struct LinkType Link;
/*!
* \brief The main list structure
*
*/
typedef struct
{
Link * mHead;
Link * mTail;
int mLength;
}List;
/*!
* \brief Links/Nodes in the list
*
*/
struct LinkType
{
Link * mNext;
void * mData;
};
//LinkedList Member methods
Link * ListNext(Link * currentLink)
{
assert(currentLink->mNext != 0);
return currentLink->mNext;
}
Link * ListHead(List * list)
{
assert(list->mHead != 0);
return list->mHead;
}
void ListPush(List * list,void * data,int (* cmp)(void *, void *))
{
Link * link = (Link*)malloc(sizeof(Link*));
link->mData = data;
link->mNext = 0;
// if the list is empty
if(list->mLength == 0)
{
list->mTail = link;
list->mHead = link;
//printf("%s\n", "First element");
}else{
//printf("%s\n", "More element");
//Push onto the end of the list
list->mTail->mNext = link;
list->mTail = link;
}
//Adding new link so increment
list->mLength++;
if(cmp != 0)
{
ListSort(list,cmp);
}
}
// Only for test
typedef struct
{
int x;
}SomeDataType;
void ListSort(List * list, int (* cmp)(void *, void *))
{
int i = 0;
int notOrder = 1;
Link * tmp = ListHead(list);
while(notOrder)
{
notOrder = 0;
i = 0;
tmp = ListHead(list);
while( i < list->mLength-1 && tmp->mNext != 0)
{
Link * dataToBeSwapped = tmp;
Link * otherDataToBeSwapped = ListNext(tmp);
SomeDataType * dataA = dataToBeSwapped->mData;
SomeDataType * dataB = otherDataToBeSwapped->mData;
if( cmp( dataA, dataB ) )
{
//printf("Index %i - Swap %i with %i \n", i, dataA->x,dataB->x );
void * tempData = dataToBeSwapped->mData;
dataToBeSwapped->mData = otherDataToBeSwapped->mData;
otherDataToBeSwapped->mData = tempData;
notOrder = 1;
}
tmp = ListNext(tmp);
i++;
}
}
// printf("%s\n", "Ordered");
// {
// Link * iter = list->mHead;
// while(iter!= 0)
// {
// Node * p = iter->mData;
// printf(" Item %i Node %i \n", p->actualCost + p->estimatedCost, p->index);
// iter = iter->mNext;
// }
// }
}
Link * ListPop(List * list)
{
Link * tmp = list->mHead;
list->mHead = list->mHead->mNext;
list->mLength--;
//Important notice!!
// User is responaible for freeing the memory
// of this Links mData and the link itself
return tmp;
}
// Constructor method
List * ListAllocate()
{
List * list = (List*)malloc(sizeof(List*));
list->mHead = 0;
list->mTail = 0;
list->mLength = 0;
return list;
}
Link * ListFind(List * list, void * nodeName, int (* cmp)(void *, void *))
{
Link * iter = list->mHead;
while(iter != 0)
{
if(cmp(iter->mData,nodeName))
{
return iter;
}
iter = iter->mNext;
}
return iter;
}
// Destructor method
void ListDeallocate(List * list)
{
// assert(true && " Deallocate bitch!! ")
//TODO :)
}
#endif