Code:
#include iostream
#include iomanip
#include string
using namespace std;
/*
* Class List Declaration
*/
class List
{
private:
int index;
int value;
List *nextindex;
public:
List(int index)
{
this->index = index;
nextindex = NULL;
value = NULL;
}
List()
{
index = -1;
value = NULL;
nextindex = NULL;
}
void store(int index, int value)
{
List *current = this;
List *previous = NULL;
List *node = new List(index);
node->value = value;
while (current != NULL && current->index < index)
{
previous = current;
current = current->nextindex;
}
if (current == NULL)
{
previous->nextindex = node;
}
else
{
if (current->index == index)
{
cout<<"DUPLICATE INDEX"<
return;
}
previous->nextindex = node;
node->nextindex = current;
}
return;
}
int fetch(int index)
{
List *current = this;
int value = NULL;
while (current != NULL && current->index != index)
{
current = current->nextindex;
}
if (current != NULL)
{
value = current->value;
}
else
{
value = NULL;
}
return value;
}
int elementCount()
{
int elementCount = 0;
List *current = this->nextindex;
for ( ; (current != NULL); current = current->nextindex)
{
elementCount++;
}
return elementCount;
}
};
/*
* Class Sparse Array Declaration
*/
class SparseArray
{
private:
List *start;
int index;
public:
SparseArray(int index)
{
start = new List();
this->index = index;
}
void store(int index, int value)
{
if (index >= 0 && index < this->index)
{
if (value != NULL)
start->store(index, value);
}
else
{
cout<<"INDEX OUT OF BOUNDS"<
}
}
int fetch(int index)
{
if (index >= 0 && index < this->index)
return start->fetch(index);
else
{
cout<<"INDEX OUT OF BOUNDS"<
return NULL;
}
}
int elementCount()
{
return start->elementCount();
}
};
/*
* Main
*/
int main()
{
int iarray[5];
iarray[0] = 1;
iarray[1] = NULL;
iarray[2] = 2;
iarray[3] = NULL;
iarray[4] = NULL;
SparseArray sparseArray(5);
for (int i = 0; i < 5; i++)
{
sparseArray.store(i, iarray[i]);
}
cout<<"NORMAL ARRAY"<
for (int i = 0 ; i < 5; i++)
{
if (iarray[i] == NULL)
cout<<"NULL"<<"\t";
else
cout<
}
cout<<"\nSPARSE ARRAY"<
for (int i = 0; i < 5; i++)
{
if (sparseArray.fetch(i) != NULL)
cout<
}
cout<<"The Size of Sparse Array is "<
}
Output:
NORMAL ARRAY
1 NULL 2 NULL NULL
SPARSE ARRAY
1 2 The Size of Sparse Array is 2
------------------
(program exited with code: 1)
Press return to continue
More C++ Programs: