Last active
April 2, 2020 23:30
-
-
Save chrisdel101/6dd8ee56b1047a4d8a4644583beba9cf to your computer and use it in GitHub Desktop.
SparseArray files
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; | |
SparseArray::SparseArray() | |
{ | |
default_value = ""; | |
headPtr = NULL; | |
} | |
SparseArray::~SparseArray() | |
{ | |
} | |
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; | |
} | |
const SparseArrayNode SparseArray::*getExactNode(int index) | |
{ | |
// headptr undefined | |
for (SparseArrayNode *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
//do stuff | |
} | |
} | |
SparseArrayNode SparseArray::*getPreviousNode(int index) | |
{ | |
} | |
// assignment operator | |
SparseArray &SparseArray::operator=(const SparseArray &to_copy) | |
{ | |
// 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& | |
return *this; | |
} | |
bool SparseArray::isAnyDefault() const | |
{ | |
// if string does not exist return false | |
if (default_value.empty()) | |
{ | |
return false; | |
} | |
return true; | |
} | |
bool SparseArray::isSorted() const | |
{ | |
return true; | |
} | |
SparseArrayNode SparseArray::*copyLinkedList(const SparseArrayNode *p_old_head) | |
{ | |
return 0; | |
} | |
bool SparseArray::invariant() const | |
{ | |
return true; | |
} | |
const string &SparseArray::getDefaultValue() const | |
{ | |
return default_value; | |
} | |
bool SparseArray::isEmpty() const | |
{ | |
if (headPtr == NULL) | |
return true; | |
return false; | |
} | |
bool SparseArray::isSet(int index) const | |
{ | |
return true; | |
} | |
const string &SparseArray::getAt(int index) const | |
{ | |
string *p = new string("Hello"); | |
return *p; | |
} | |
unsigned int SparseArray::getNodeCount() const | |
{ | |
unsigned int count = 0; | |
for (SparseArrayNode const *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
count++; | |
} | |
return count++; | |
} | |
void SparseArray::printNodes() const | |
{ | |
if (headPtr == NULL) | |
{ | |
cout << "NONE" << endl; | |
return; | |
} | |
else | |
{ | |
cout << "Nodes: -> "; | |
for (SparseArrayNode const *pNode = headPtr; pNode != NULL; pNode = pNode->next) | |
{ | |
cout << "[" << pNode->index << ": "; | |
cout << "\"" << pNode->value << "\""; | |
cout << "]"; | |
if (pNode->next != NULL) | |
{ | |
cout << " -> "; | |
} | |
} | |
cout << endl; | |
} | |
} | |
void SparseArray::insert(int index, const string &value) | |
{ | |
if (value != default_value) | |
{ | |
SparseArrayNode *s = new SparseArrayNode(); | |
s->index = index; | |
s->value = value; | |
s->next = headPtr; | |
headPtr = s; | |
} | |
} | |
void SparseArray::remove(int index) | |
{ | |
} | |
void SparseArray::clear() | |
{ | |
} |
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 *getExactNode(int index); | |
SparseArrayNode *getPreviousNode(int index); | |
SparseArrayNode *copyLinkedList(const SparseArrayNode *p_old_head) const; | |
bool invariant() const; | |
public: | |
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