-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.c
More file actions
82 lines (69 loc) · 2.23 KB
/
Copy patharraylist.c
File metadata and controls
82 lines (69 loc) · 2.23 KB
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
#include "arraylist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ArrayList* createArrayList(int maxElementCount)
{
ArrayList *tmp = (ArrayList *)calloc(1, sizeof(ArrayList));
tmp->maxElementCount = maxElementCount;
tmp->currentElementCount = 0;
tmp->pElement = (ArrayListNode *)calloc(maxElementCount, sizeof(ArrayListNode));
return tmp;
}
void deleteArrayList(ArrayList *pList)
{
free(pList->pElement);
free(pList);
pList = NULL;
}
int isArrayListFull(ArrayList* pList)
{
return pList->maxElementCount == pList->currentElementCount ? TRUE : FALSE;
}
int addALElement(ArrayList *pList, int position, ArrayListNode element)
{
ArrayListNode *tmp;
if (!pList || position > pList->currentElementCount)
return FALSE;
if (isArrayListFull(pList))
{
pList->maxElementCount *= 2;
tmp = (ArrayListNode *)calloc(pList->maxElementCount, sizeof(ArrayListNode));
memcpy(tmp, pList->pElement, pList->currentElementCount * sizeof(ArrayListNode));
free(pList->pElement);
pList->pElement = tmp;
}
if ((pList->currentElementCount && position > pList->currentElementCount))
return FALSE;
memmove(pList->pElement + position + 1, pList->pElement + position, sizeof(ArrayListNode) * (pList->currentElementCount - position));
*(pList->pElement + position) = element;
(pList->currentElementCount)++;
return TRUE;
}
int removeALElement(ArrayList* pList, int position)
{
ArrayListNode *tmp;
if (position >= pList->currentElementCount || pList->currentElementCount == 0)
return FALSE;
memmove(pList->pElement + position, pList->pElement + position + 1, sizeof(ArrayListNode) * (pList->currentElementCount - position));
(pList->currentElementCount)--;
//tmp = (ArrayListNode *)calloc(pList->currentElementCount, sizeof(ArrayListNode));
//memcpy(tmp, pList->pElement, sizeof(ArrayListNode) * pList->currentElementCount);
//free(pList->pElement);
//pList->pElement = tmp;
return TRUE;
}
ArrayListNode* getALElement(ArrayList* pList, int position)
{
return position < pList->maxElementCount ? pList->pElement + position : NULL;
}
void clearArrayList(ArrayList* pList)
{
free(pList->pElement);
pList->pElement = NULL;
pList->currentElementCount = 0;
}
int getArrayListLength(ArrayList *pList)
{
return pList->currentElementCount;
}