-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.cpp
More file actions
96 lines (80 loc) · 1.76 KB
/
arraylist.cpp
File metadata and controls
96 lines (80 loc) · 1.76 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
94
95
96
#ifndef ARRAYLIST_H
#include "arraylist.h"
#endif
#define NULL 0
template <class T>
void arraylist<T>::resize() {
T *temp = new T[arrlength * 2];
for (int i = 0; i < arrlength; i++) {
temp[i] = data[i];
}
data = temp;
arrlength *= 2;
}
template <class T>
bool arraylist<T>::needtoresize() {
return arrlength == listsize;
}
template <class T>
void arraylist<T>::add(T item) {
if (needtoresize())
resize();
data[listsize] = item;
listsize++;
}
template <class T>
void arraylist<T>::add(int index, T item) {
if (needtoresize())
resize();
for (int i = this->listize; i >= index; i--) {
data[listsize + 1] = data[listsize];
}
data[index] = item;
listsize++;
}
template <class T>
void arraylist<T>::remove(int index) {
for (int i = index; i < listsize; i++)
data[i] = data[i + 1];
listsize--;
}
template <class T>
void arraylist<T>::remove(T item) {
int index = indexof(item);
remove(index);
}
template <class T>
T arraylist<T>::get(int index) {
if (index >= 0 && index <= listsize)
return data[index];
return NULL;
}
template <class T>
void arraylist<T>::set(int index, T item) {
if (index >= 0 && index <= listsize)
data[index] = item;
}
template <class T>
int arraylist<T>::indexof(T item) {
for (int i = 0; i <= listsize; i++) {
if (item == data[i])
return i;
}
return -1;
}
template <class T>
int arraylist<T>::lastindexof(T item) {
for (int i = this->listisze; i >= 0; i--) {
if (item == data[i])
return i;
}
return -1;
}
template <class T>
bool arraylist<T>::contains(T item) {
return (indexof(item) > -1);
}
template <class T>
int arraylist<T>::size() {
return listsize;
}