My Linked Array ADT Keeps crashing.
Mar 14, 2017 at 6:58pm UTC
So, I've been struggling with this assignment for a while.. At first I thought I had the issue solved when I was finally able to get the thing to compile, but now it crashes after the dummy test.. I tried getting some help from my friend, but I think that further messed up my code.. Any help on figuring out what is up would be most helpful!
Header:
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#include <vector>
using namespace std;
template <class myUnit>
class LinkedArray
{
public :
template <typename T>
struct Node
{
T data;
Node *here;
int key;
bool inUse;
Node():data(T()), key(-1), inUse(false ), here(NULL){}
};
static myUnit dummy;
/// end here
LinkedArray();
virtual ~LinkedArray();
LinkedArray(const LinkedArray<myUnit> & copy);
myUnit operator [](int index) const ;
myUnit & operator [](int index);
int CAPACITY() const {return CAP;}
int Size() const {return size;}
bool containsKey(int index) const ;
void DeleteKey(int index);
void Clear();
vector<int > Keys() const ;
LinkedArray<myUnit> & operator =( const LinkedArray<myUnit> & copy);
private :
Node<myUnit> * begin;
Node<myUnit> * end;
int CAP;
int size;
void del();
void Copy(const LinkedArray<myUnit> & copy);
};
template <class myUnit>
myUnit LinkedArray<myUnit>::dummy = myUnit();
template <typename myUnit>
LinkedArray<myUnit>::LinkedArray()
:begin(NULL), end(NULL), CAP(0), size(0){}
template <typename myUnit>
LinkedArray<myUnit>::LinkedArray (const LinkedArray<myUnit> & copy)
:begin(NULL), end(NULL), CAP(0), size(0){ Copy(copy);}
template <class myUnit>
LinkedArray<myUnit>::~LinkedArray(){del();}
template <class myUnit>
LinkedArray<myUnit> & LinkedArray<myUnit>::operator =( const LinkedArray<myUnit> & copy )
{
if ( this != © )
{
del();
Copy(copy);
}
return *this ;
}
template <typename myUnit>
myUnit LinkedArray<myUnit>::operator [](int index) const
{
if (index < 0 || index > CAP)
return dummy;
Node<myUnit> *const *prePtr = &begin;
while ( *prePtr != NULL && (*prePtr)->key < index)
prePtr = &((*prePtr)->here);
if ( *prePtr == NULL)
return dummy;
else if ( (*prePtr)->key > index)
return dummy;
else
return (*prePtr)->data;
}
template <class myUnit>
myUnit & LinkedArray<myUnit>::operator [](int index)
{
if (index < 0)
return dummy;
if (index >= CAP)
{
if (end == NULL)
begin = end = new Node<myUnit>;
else
{
end->here = new Node<myUnit>;
end = end-> here;
}
end->key = index;
end->inUse = true ;
size++;
CAP = index+1;
return end->data;
}
Node<myUnit> ** prePtr = &begin;
while ( (*prePtr) != NULL && (*prePtr)->key < index)
prePtr = &((*prePtr)-> here);
Node<myUnit> *aNode = NULL;
if ( (*prePtr)->key > index)
{
aNode = new Node<myUnit>;
aNode->key= index;
aNode->here = *prePtr;
*prePtr = aNode;
}
else
{
aNode = *prePtr;
}
if ( aNode->inUse == false )
{
size++;
aNode -> inUse = true ;
}
return aNode->data;
}
template <typename myUnit>
void LinkedArray<myUnit>::Copy (const LinkedArray<myUnit> & copy)
{
Node<myUnit> * cPtr = copy.begin;
Node<myUnit> * iPtr = NULL;
while (cPtr != NULL);
{
if (cPtr == copy.begin)
{
begin = new Node<myUnit>;
iPtr = begin;
}
else
{
iPtr -> here = new Node<myUnit>;
iPtr = iPtr -> here;
}
iPtr->data=cPtr->data;
iPtr->inUse = cPtr->inUse;
iPtr->key = cPtr->key;
if (cPtr == copy.end)
end = iPtr;
cPtr = cPtr->here;
}
size = copy.size;
CAP = copy.CAP;
}
template <class myUnit>
bool LinkedArray<myUnit>::containsKey(int index) const
{
if (index <0 || index>= CAP)
return false ;
Node<myUnit> const *nPtr = begin;
while (nPtr != NULL && nPtr->key < index)
nPtr = nPtr->here;
if (nPtr != NULL)
if (nPtr->key == index)
if (nPtr->inUse==true )
return true ;
return false ;
}
template <typename myUnit>
void LinkedArray<myUnit>::DeleteKey(int index)
{
if (index >= 0 && index < CAP)
{
Node<myUnit> *nPtr = begin;
while (nPtr != NULL && nPtr->key < index)
nPtr = nPtr->here;
if (nPtr != NULL && nPtr->key == index)
{
if (nPtr->inUse == true )
size--;
nPtr->inUse = false ;
}
}
}
template <typename myUnit>
void LinkedArray<myUnit>::del()
{
Node<myUnit> * point = begin;
while (begin!=NULL)
{
begin = begin->here;
delete point;
point = begin;
}
begin = end = NULL;
CAP = size = 0;
}
template <class myUnit>
void LinkedArray<myUnit>::Clear()
{
Node<myUnit> * nPtr = begin;
while (nPtr!=NULL)
{
nPtr -> inUse = false ;
nPtr = nPtr -> here;
}
size = 0;
}
template <typename myUnit>
vector<int > LinkedArray<myUnit>::Keys() const
{
vector<int > keys;
Node<myUnit> const *nPtr = begin;
while ( nPtr != NULL)
{
if ( nPtr->inUse == true )
keys.push_back(nPtr->key);
nPtr = nPtr->here;
}
return keys;
}
Driver:
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include "LinkedArray.h"
using namespace std;
template <typename myUnit>
ostream & operator <<(ostream & os, const LinkedArray<myUnit> & list)
{
vector<int > keys= list.Keys();
int size = keys.size();
for (int x = 0; x < size; x++)
{
os << "[" << keys[x] << " , " << list[keys[x]] << " ]" ;
if ( x < size-1)
os << " , " ;
}
return os;
}
double randVal(int maxV=100)
{
return (double )(rand()%maxV) * rand() / RAND_MAX;
}
int randInd(int MAX=100) /// randomly populate 3
{
return rand()%3 == 0 ? - rand()%MAX : rand()%MAX;
}
void populate( LinkedArray<double > & list, int popCap = 8)
{
for (int x = 0; x < popCap; x++)
{
list[randInd()] = randVal();
}
}
void print( int line = 2)
{
for (int x = 0; x < line; x++)
cout << " ~~ " ;
cout << endl;
}
int main()
{
cout << "Setting dummy to 333.555" << endl;
LinkedArray<double >::dummy = 998.9;
cout << "Dummy currently is : " << LinkedArray<double >::dummy << endl;
print();
LinkedArray<double >::dummy=999.9;
LinkedArray<double > testOne;
const LinkedArray<double > &testTwo = testOne;
LinkedArray<double > testOneCopy(testOne);
const LinkedArray<double > &testTwoCopy = testOneCopy;
print();
cout << "testOne size check : " << endl;
cout << "testOne array size is : " << testOne.Size() << endl;
cout << "testOne CAP is : " << testOne.CAPACITY() << endl;
cout << "Test [ ] " << endl;
for (int x = 0; x<5; x++)
{
int index = randInd();
double val = randVal();
cout << "testOne[" << index << "] = " << val << endl;
testOne[index]=val;
cout << "Size after : " << testOne.Size() << endl;
cout << "CAP after : " << testOne.CAPACITY() << endl;
}
cout << "Overwriting check " << endl;
cout << "testOne[5] = 55 " << endl;
testOne[5] = 55;
cout << testOne;
cout << "Size & CAP : " << testOne.Size() << " | " << testOne.CAPACITY() << endl;
print();
cout << "testing operator and populate function." << endl;
LinkedArray<double > testThree;
const LinkedArray<double > &testThreeCopy = testThree;
populate(testThree);
cout << "testOne Size & CAP : " << testOne.Size() << " ~~ " << testOne.CAPACITY() << endl;
cout << "testTwo Size & CAP : " << testThree.Size() << " ~~ " << testThree.CAPACITY() << endl;
cout << "Testing = : " << endl << endl;
testThree=testOne;
cout << "testOne Size & CAP : " << testOne.Size() << " ~~ " << testOne.CAPACITY() << endl;
cout << "testTwo Size & CAP : " << testThree.Size() << " ~~ " << testThree.CAPACITY() << endl;
cout << endl;
cout << "Clear() test " << endl;
cout << "testOne Size & CAP : " << testOne.Size() << " ~~ " << testOne.CAPACITY() << endl;
cout << testOne << endl;
cout << "After Clear()..." << endl;
testOne.Clear();
cout << "testOne Size & CAP : " << testOne.Size() << " ~~ " << testOne.CAPACITY() << endl;
cout << testOne << endl;
print();
}
Mar 14, 2017 at 7:43pm UTC
I suggest you go back and add braces to all of your control statements. Your indentation, in several places suggest that you intend to have more than one line in the statement body:
1 2 3 4 5 6 7 8 9 10 11 12 13
template <typename myUnit>
vector<int > LinkedArray<myUnit>::Keys() const
{
vector<int > keys;
Node<myUnit> const *nPtr = begin;
while ( nPtr != NULL)
{
if ( nPtr->inUse == true )
keys.push_back(nPtr->key);
nPtr = nPtr->here; // Is this supposed to be part of the if() body?
}
return keys;
}
And you really shouldn't be using a semicolon to indicate empty while bodies:
while (cPtr != NULL);
Also note that you should no longer be using NULL in C++, prefer nullptr (added for C++11).
Lastly your indentation is really horrible. You really need to consistently use a decent indentation style.
https://en.wikipedia.org/wiki/Indent_style
Topic archived. No new replies allowed.