-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
207 lines (171 loc) · 3.8 KB
/
Vector.cpp
File metadata and controls
207 lines (171 loc) · 3.8 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "Vector.h"
#include <iostream>
using namespace std;
// Constructors
template <class DataType>
Vector<DataType>::Vector() // Default constructor
{
size = 0;
capacity = 4;
dataItems = new DataType[capacity]();
}
// Destructor
template <class DataType>
Vector<DataType>::~Vector()
{
delete[] dataItems;
}
// Vector manipulation operations
template <class DataType>
void Vector<DataType>::append(const DataType item) // Add a new item to the end of the vector
{
if (isFull())
{
cout << "The List is Full, Making Space..." << endl;
//int *temp = new int[capacity * 2];
capacity *= 2;
DataType* temp = new DataType[capacity];
for (int i = 0; i < size; i++)
{
temp[i] = dataItems[i];
}
delete dataItems;
dataItems = temp;
}
dataItems[size] = item;
size++;
//&operator = append(item);
}
template <class DataType>
void Vector<DataType>::insertAt(const int index, const DataType item) // Insert a new element at the given index
{
if (index > capacity || index < 0)
{
cout << "The index is out of bounds " << endl;
return;
}
if (isFull())
{
capacity *= 2;
DataType *temp = new DataType[capacity * 2];
for (int i = 0; i < size; i++)
{
temp[i] = dataItems[i];
}
delete dataItems;
dataItems = temp;
}
dataItems[index] = item;
size++;
}
template <class DataType>
void Vector<DataType>::removeAt(const int index) // Remove the data item at the given index
{
if (index > capacity || index < 0)
{
cout << "Given index is out of boundaries... " << endl;
return;
}
for (int i = 0; i < size; i++)
{
if (i == index)
{
dataItems[i] = NULL;
size--;
}
}
cout << "Element has been removed from Index... " << index << endl;
return;
}
template <class DataType>
void Vector<DataType>::remove(int item) // Remove the given element from the vector
{
if (isEmpty())
{
return;
}
for (int i = 0; i < size; i++)
{
if (dataItems[i] == item)
{
dataItems[i] = 700;
}
}
size--;
}
template <class DataType>
void Vector<DataType>::clear() // Make the vector empty
{
size = 0;
/*
while (!isEmpty())
{
delete[] dataItems;
}
*/
for (int i = 0; i < capacity; i++)
{
dataItems[i] = NULL;
}
cout << "Capacity is: " << capacity << endl;
}
template <class DataType>
bool Vector<DataType>::isEmpty() const // Is the Vector empty?
{
return (size == 0);
}
template <class DataType>
bool Vector<DataType>::isFull() const // Is the Vector full?
{
return (size >= capacity);
}
template <class DataType>
int Vector<DataType>::contains(const DataType item) const // Does the vector contain the given item? return the index of the item if found, otherwise, return -1.
{
if (isEmpty())
{
return -1;
}
else
{
for (int i = 0; i < size; i++)
{
if (dataItems[i] == item)
{
return i;
}
}
}
return -1;
}
template <class DataType>
DataType Vector<DataType>::elementAt(const int index) const // Access the element at the given index
{
if (isEmpty())
{
return -1;
}
for (int i = 0; i < size; i++)
{
if (i == index)
{
cout << "The element in index " << index << " is: " << dataItems[i] << endl;
return dataItems[i];
}
}
//return index;
}
template <class DataType>
int Vector<DataType>::getSize() const // Return the number of items in the vector
{
return size;
}
// overloading subscript []
template <class DataType>
DataType& Vector<DataType>::operator[] (const int index) // overloading subscript []
{
return (dataItems[index]);
}
template class Vector<int>;
template class Vector<float>;
template class Vector<char>;