Last active
April 3, 2020 03:41
-
-
Save chrisdel101/a0700a91ed6ef72a8a46be88c735d236 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include "SparseArray.h" | |
using namespace std; | |
const string &SparseArray::getAt(int index) const | |
{ | |
if (getExactNode(index) != NULL) | |
{ | |
return getExactNode(index)->value; | |
} | |
else | |
{ | |
// issue is here | |
string *p = new string("none"); | |
return *p; | |
} | |
} | |
bool SparseArray::isAnyDefault() const | |
{ | |
for (SparseArrayNode const *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
if (pNode->value == default_value) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
const SparseArrayNode *SparseArray::getExactNode(int index) const | |
{ | |
for (SparseArrayNode *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
if (pNode->index == index) | |
{ | |
return pNode; | |
} | |
} | |
return NULL; | |
} | |
SparseArrayNode SparseArray::*getPreviousNode(int index) | |
{ | |
} | |
SparseArrayNode SparseArray::*copyLinkedList(const SparseArrayNode *p_old_head) | |
{ | |
return 0; | |
} | |
bool SparseArray::invariant() const | |
{ | |
if (isAnyDefault()) | |
{ | |
return false; | |
} | |
return true; | |
} | |
const string &SparseArray::getDefaultValue() const | |
{ | |
assert(invariant()); | |
return default_value; | |
} | |
bool SparseArray::isEmpty() const | |
{ | |
assert(invariant()); | |
if (headPtr == NULL) | |
return true; | |
assert(invariant()); | |
return false; | |
} | |
bool SparseArray::isSet(int index) const | |
{ | |
assert(invariant()); | |
if (getExactNode(index) != NULL) | |
{ | |
return true; | |
} | |
assert(invariant()); | |
return false; | |
} | |
unsigned int SparseArray::getNodeCount() const | |
{ | |
assert(invariant()); | |
unsigned int count = 0; | |
for (SparseArrayNode const *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
count++; | |
} | |
assert(invariant()); | |
return count++; | |
} | |
void SparseArray::printNodes() const | |
{ | |
assert(invariant()); | |
cout << "Nodes: "; | |
if (headPtr == NULL) | |
{ | |
cout << "NONE" << endl; | |
return; | |
} | |
else | |
{ | |
cout << "-> "; | |
for (SparseArrayNode const *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
cout << "[" << pNode->index << ": "; | |
cout << "\"" << pNode->value << "\""; | |
cout << "]"; | |
if (pNode->next != NULL) | |
{ | |
cout << " -> "; | |
} | |
} | |
cout << endl; | |
} | |
assert(invariant()); | |
} | |
void SparseArray::insert(int index, const string &value) | |
{ | |
assert(invariant()); | |
if (value != default_value) | |
{ | |
SparseArrayNode *s = new SparseArrayNode(); | |
s->index = index; | |
s->value = value; | |
s->next = headPtr; | |
headPtr = s; | |
} | |
else | |
{ | |
cout << "Do Nothing" << endl; | |
} | |
assert(invariant()); | |
} | |
void SparseArray::remove(int index) | |
{ | |
assert(invariant()); | |
assert(invariant()); | |
} | |
void SparseArray::clear() | |
{ | |
assert(invariant()); | |
assert(invariant()); | |
} | |
SparseArrayNode SparseArray::*getPreviousNode(int index) | |
{ | |
} | |
SparseArrayNode SparseArray::*copyLinkedList(const SparseArrayNode *p_old_head) | |
{ | |
return 0; | |
} | |
SparseArray::SparseArray() | |
{ | |
default_value = ""; | |
headPtr = NULL; | |
} | |
SparseArray::~SparseArray() | |
{ | |
assert(invariant()); | |
} | |
SparseArray::SparseArray(const string &default_value1) | |
{ | |
default_value = default_value1; | |
headPtr = NULL; | |
} | |
SparseArray::SparseArray(const SparseArray &to_copy) | |
{ | |
default_value = to_copy.default_value; | |
headPtr = to_copy.headPtr; | |
} | |
// assignment operator | |
SparseArray &SparseArray::operator=(const SparseArray &to_copy) | |
{ | |
assert(invariant()); | |
// if current house is inequal to address of house being copied | |
if (this != &to_copy) | |
{ | |
default_value = to_copy.default_value; | |
headPtr = to_copy.headPtr; | |
} | |
// always use this at the end of the operator= | |
// if you declare it to return ClassName& | |
assert(invariant()); | |
return *this; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
using namespace std; | |
struct SparseArrayNode | |
{ | |
int index; | |
string value; | |
SparseArrayNode *next; | |
}; | |
class SparseArray | |
{ | |
private: | |
string default_value; | |
SparseArrayNode *headPtr; | |
bool isAnyDefault() const; | |
bool isSorted() const; | |
const SparseArrayNode *getExactNode(int index) const; | |
SparseArrayNode *getPreviousNode(int index); | |
SparseArrayNode *copyLinkedList(const SparseArrayNode *p_old_head) const; | |
bool invariant() const; | |
public: | |
int number; | |
SparseArray(); | |
SparseArray(const string &default_value1); | |
SparseArray(const SparseArray &to_copy); | |
~SparseArray(); | |
SparseArray &operator=(const SparseArray &to_copy); | |
bool isEmpty() const; | |
const string &getDefaultValue() const; | |
bool isSet(int index) const; | |
const string &getAt(int index) const; | |
unsigned int getNodeCount() const; | |
void printNodes() const; | |
void insert(int index, const string &value); | |
void remove(int index); | |
void clear(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment