-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjustableArray.cpp
More file actions
186 lines (169 loc) · 3.36 KB
/
AdjustableArray.cpp
File metadata and controls
186 lines (169 loc) · 3.36 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
// AdjustableArray.cpp
#include <iostream>
#include <cstdlib>
#include "AdjustableArray.h"
const int MAX_CAPACITY = 100;
// Default constructor
AdjustableArray::AdjustableArray() :
mLen(0),
mCap(MAX_CAPACITY),
head(NULL),
tail(NULL)
{
// Array();
}
// Nondefault constructor
AdjustableArray::AdjustableArray(int aLen) :
mLen(aLen),
mCap(aLen),
head(NULL),
tail(NULL)
{
allocate(aLen);
for (int i = 0; i < length(); i++) {
node *n = new node;
if (head->next == NULL) {
head->next = n;
}
(*this)[i] = 0;
// (*this)[i] = n->data;
}
}
// Operator methods
const int & AdjustableArray::operator[](int i) const
{
if (i < 0 || i >= mLen) {
std::cout << "Invalid array index." << std::endl;
exit(1);
}
return mA[i];
// node & n = mA[i];
// return n.data;
}
int & AdjustableArray::operator[](int i)
{
if (i < 0 || i >= mLen) {
std::cout << "Invalid array index." << std::endl;
exit(1);
}
return mA[i];
// node & n = mA[i];
// return n.data;
}
void AdjustableArray::output(std::ostream & s) const
{
s << "{";
for (int i = 0; i < mLen; i++) {
s << " " << (*this)[i];
}
s << " }";
}
std::ostream& operator<<(std::ostream& s, const AdjustableArray& a)
{
a.output(s);
return s;
}
// end operator methods
// Class methods
node * AdjustableArray::addNode(int n)
{
node *newN = new node;
newN->data = n;
newN->next = NULL;
if (head == NULL)
{
// head and tail at same loc if prev unset
head = newN;
tail = newN;
}
else
{
tail->next = newN;
tail = tail->next; // tail is updated to new Node
}
return newN;
}
void AdjustableArray::allocate(int aLen)
{
if (aLen < 0) {
std::cout << "Negative index." << std::endl;
mLen = 0;
mA = nullptr;
return;
}
setLength(aLen);
mA = new int[aLen];
}
void AdjustableArray::setLength(int aLen)
{
if (aLen <= mCap) {
mLen = aLen;
}
if (mLen <= mCap) return;
std::cout <<
"Array size cap reached :<"
<< std::endl;
exit(1);
}
bool AdjustableArray::atCapacity()
{
return tail == NULL;
}
// Return an index of the Array where a given data is found.
// If not found, return -1.
int AdjustableArray::find(int aData) const
{
node * n = head;
node * prev = head;
int idx = -1;
for (int i = 0; i >= mLen; i++) {
if (n->data == aData) {
return prev->data;
if (i > 0) prev = prev->next;
n = prev->next;
}
}
return idx;
}
node * AdjustableArray::findPreviousNode(int aData)
{
node * n = head;
for (int i=0; i <= mLen; i++) {
if (n->next->data == aData) {
return n->next;
n = n->next;
}
}
return n;
}
// Insert aData after aExistingEnty.
// If aExistingEntry is not found, insert in the end of the Array.
void AdjustableArray::insertAfter(int aExistingEntry, int aData)
{
const int idx = find(aExistingEntry);
if (atCapacity()) {
const int oldLen = mLen;
setLength(mLen * 2);
}
if (idx == -1) {
addNode(aData);
return;
}
node * prevN = findPreviousNode(aData);
node *newN = new node;
newN->data = aData;
newN->next = prevN->next;
prevN->next = newN;
}
// Find and delete aData.
// If not found, do nothing.
void AdjustableArray::deleteThis(int aData)
{
int idx = find(aData);
if (idx != -1) {
node * prevN = findPreviousNode(aData);
node * doomedN = prevN->next;
prevN->next = doomedN->next;
delete [] doomedN;
}
}