C++ Program to Implement Sparse Array

This C++ Program demonstrates the implementation of Sparse Array.

Here is source code of the C++ Program to demonstrate the implementation of Sparse Array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Sparse Array
  3.  */
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. using namespace std;    
  8. /*
  9.  * Class List Declaration
  10.  */
  11. class List
  12. {
  13.     private: 
  14.         int index;
  15.         int value;
  16.         List *nextindex;
  17.     public: 
  18.         List(int index)
  19.         {
  20.             this->index = index;
  21.             nextindex = NULL;
  22.             value = NULL;
  23.         }
  24.         List()
  25.         {
  26.             index = -1;
  27.             value = NULL;
  28.             nextindex = NULL;
  29.         }
  30.         void store(int index, int value)
  31.         {
  32.             List *current = this;
  33.             List *previous = NULL;
  34.             List *node = new List(index);
  35.             node->value = value;
  36.             while (current != NULL && current->index < index)
  37.             {
  38.                 previous = current;
  39.                 current = current->nextindex;
  40.             }
  41.             if (current == NULL)
  42.             {
  43.                 previous->nextindex = node;
  44.             } 
  45.             else
  46.             {
  47.                 if (current->index == index)
  48.                 {
  49.                     cout<<"DUPLICATE INDEX"<<endl;
  50.                     return;
  51.                 }
  52.                 previous->nextindex = node;
  53.                 node->nextindex = current;
  54.             }
  55.             return;
  56.         }
  57.  
  58.         int fetch(int index)
  59.         {
  60.             List *current = this;
  61.             int value = NULL;
  62.             while (current != NULL && current->index != index)
  63.             {
  64.                 current = current->nextindex;
  65.             }
  66.             if (current != NULL)
  67.             {
  68.                 value = current->value;
  69.             } 
  70.             else
  71.             {
  72.                 value = NULL;
  73.             }
  74.             return value;
  75.         }
  76.  
  77.         int elementCount()
  78.         {
  79.             int elementCount = 0;
  80.             List *current = this->nextindex;
  81.             for ( ; (current != NULL); current = current->nextindex)
  82.             {
  83.                 elementCount++;
  84.             }
  85.             return elementCount;
  86.         }
  87. };
  88. /*
  89.  * Class Sparse Array Declaration
  90.  */
  91. class SparseArray
  92. {
  93.     private:
  94.         List *start;
  95.         int index;
  96.     public:
  97.         SparseArray(int index)
  98.         {
  99.             start = new List();
  100.             this->index = index;
  101.         }
  102.         void store(int index, int value)
  103.         {
  104.             if (index >= 0 && index < this->index)
  105.             {
  106.                 if (value != NULL)
  107.                     start->store(index, value);
  108.             } 
  109.             else
  110.             {
  111.                 cout<<"INDEX OUT OF BOUNDS"<<endl;
  112.             }
  113.         }
  114.         int fetch(int index)
  115.         {
  116.             if (index >= 0 && index < this->index)
  117.                 return start->fetch(index);
  118.             else 
  119.             {
  120.                 cout<<"INDEX OUT OF BOUNDS"<<endl;
  121.                 return NULL;
  122.             }
  123.         }
  124.         int elementCount()
  125.         {
  126.             return start->elementCount();
  127.         }
  128. };
  129. /*
  130.  * Main
  131.  */
  132. int main()
  133. {
  134.     int iarray[5];
  135.     iarray[0] = 1;
  136.     iarray[1] = NULL;
  137.     iarray[2] = 2;
  138.     iarray[3] = NULL;
  139.     iarray[4] = NULL;
  140.     SparseArray sparseArray(5);
  141.     for (int i = 0; i < 5; i++)
  142.     {
  143.         sparseArray.store(i, iarray[i]);
  144.     }
  145.     cout<<"NORMAL ARRAY"<<endl;
  146.     for (int i = 0 ; i < 5; i++)
  147.     {
  148.         if (iarray[i] == NULL)
  149.             cout<<"NULL"<<"\t";
  150.         else
  151.             cout<<iarray[i] <<"\t";
  152.     }
  153.  
  154.     cout<<"\nSPARSE ARRAY"<<endl;
  155.     for (int i = 0; i < 5; i++)
  156.     {
  157.         if (sparseArray.fetch(i) != NULL)
  158.             cout<<sparseArray.fetch(i) <<"\t";
  159.     }
  160.     cout<<"The Size of Sparse Array is "<<sparseArray.elementCount()<<endl;
  161. }

$ g++ sparse_array.cpp
$ a.out
 
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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.