-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.cpp
executable file
·82 lines (78 loc) · 1.38 KB
/
list.cpp
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 "list.h"
CList::CList()
{
items = (void**)malloc(5*sizeof(void*));
buffsize = 5;
count = 0;
}
CList::CList(int buffsize)
{
buffsize = std::max(buffsize, 1);
this->buffsize = buffsize;
items = (void**)malloc(buffsize*sizeof(void*));
count = 0;
}
CList::~CList()
{
free(items);
}
int CList::Add(void*item)
{
if(count >= buffsize)
{
buffsize += 5;
items = (void**)realloc(items, buffsize*sizeof(void**));
}
items[count] = item;
count++;
return count;
}
void CList::Remove(void*item)
{
for(int i = 0; i < count; i++)
{
if(items[i] == item)
{
memmove(items+i, items+i+1, (count-i-1)*sizeof(void**));
count--;
break;
}
}
}
void CList::Remove(int i)
{
if(i < 0 || i >= count)return;
if(count > 1)
{
memmove(items+i, items+i+1, (count-i-1)*sizeof(void**));
count--;
} else Clear();
}
void CList::set(int pos, void*item)
{
if(pos < 0 || pos >= count)return;
items[pos] = item;
}
void CList::Clear()
{
free(items);
items = (void**)malloc(5*sizeof(void*));
buffsize = 5;
count = 0;
}
void* CList::find(void*item)
{
for(int i = 0; i < count; i++)
if(items[i] == item)return items[i];
return NULL;
}
void* CList::operator [](int i)
{
if(i < 0 || i >= count)return NULL;
return items[i];
}
void* CList::item(int i)
{
if(i < 0 || i >= count)return NULL;
return items[i];
}