-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayListUtility.cpp
More file actions
93 lines (73 loc) · 1.48 KB
/
arrayListUtility.cpp
File metadata and controls
93 lines (73 loc) · 1.48 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
83
84
85
86
87
88
89
90
91
92
93
#include "arrayListUtility.h"
using namespace list;
// Functions used ONLY for printing
// Creates an empty dynamic array
list::List list::createEmpty()
{
List l;
l.size = 0;
l.maxsize = BLOCKDIM;
Elem* aux = new Elem[BLOCKDIM];
l.list = aux;
return l;
}
// re-use add function to do a rear add
void list::addBack(Elem e, List& l)
{
add(l.size, e, l);
}
// Add in "pos" position
void list::add(int pos, Elem e, List& l)
{
// Checking "pos" value
if(pos < 0 || pos > l.size) return;
// Resizing d_array
if(l.size == l.maxsize)
{
// Creating aux d_array with more space
List aux;
aux.size = l.size + 1;
aux.maxsize = l.maxsize + BLOCKDIM;
Elem* tmp = new Elem[aux.maxsize];
aux.list = tmp;
// Saving elements before "pos"
for(int i = 0; i < pos; ++i)
{
aux.list[i] = l.list[i];
}
// Save new element in "pos"
aux.list[pos] = e;
// Saving the rest of the old d_array after "pos"
for(int i = pos+1; i < aux.size; ++i)
{
aux.list[i] = l.list[i-1];
}
// Deleting old d_array and make l point to aux
delete[] l.list;
l = aux;
return;
}
// Not resizing d_array
l.size++;
for(int i = l.size-1; i > pos; --i)
l.list[i] = l.list[i-1];
l.list[pos] = e;
}
int list::size(const List& l)
{
return l.size;
}
// Checking if "e" exists in d_array "l"
bool list::findElem(Elem e, List l)
{
for(int i = 0; i < l.size; ++i)
{
if(l.list[i] == e)
return true;
}
return false;
}
bool list::isEmpty(List l)
{
return !(size(l));
}