-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayList.c
More file actions
63 lines (61 loc) · 1.82 KB
/
Copy patharrayList.c
File metadata and controls
63 lines (61 loc) · 1.82 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
#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;
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 (isArrayListFull(pList))
pList->maxElementCount *= 2;
if ((pList->currentElementCount && position > pList->currentElementCount))
return FALSE;
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 displayArrayList(ArrayList* pList)
{
for (int i = 0 ; i < pList->currentElementCount ; i++)
printf(“%d “,(pList->pElement + i)->data);
}
void clearArrayList(ArrayList* pList)
{
free(pList->pElement);
pList->pElement = NULL;
pList->currentElementCount = 0;
}
int getArrayListLength(ArrayList *pList)
{
return pList->currentElementCount;
}