Advertisement
jbonanno

arrayListType.h

Jul 11th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.90 KB | None | 0 0
  1. #ifndef H_arrayListType
  2. #define H_arrayListType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template <class elemType>
  10. class arrayListType
  11. {
  12. public:
  13.     const arrayListType<elemType>&
  14. operator=(const arrayListType<elemType>&);
  15.         //Overloads the assignment operator
  16.  
  17.     bool isEmpty();
  18.         //Function to determine whether the list is empty
  19.         //Postcondition: Returns true if the list is empty;
  20.         //              otherwise, returns false.
  21.     bool isFull();
  22.         //Function to determine whether the list is full
  23.         //Postcondition: Returns true if the list is full;
  24.         //               otherwise, returns false.
  25.     int listSize();
  26.         //Function to determine the number of elements in the list
  27.         //Postcondition: Returns the value of length.
  28.     int maxListSize();
  29.         //Function to determine the size of the list
  30.         //Postcondition: Returns the value of maxSize.
  31.     void print() const;
  32.         //Function to output the elements of the list
  33.         //Postcondition: Elements of the list are output on the
  34.         //               standard output device.
  35.     bool isItemAtEqual(int location, const elemType& item);
  36.         //Function to determine whether the item is the same
  37.         //as the item in the list at the position specified by
  38.         //Postcondition: Returns true if the list[location]
  39.         //               is the same as the item; otherwise,
  40.         //           returns false.
  41.    void insertAt(int location, const elemType& insertItem);
  42.         //Function to insert an item in the list at the
  43.         //position specified by location. The item to be inserted
  44.         //is passed as a parameter to the function.
  45.         //Postcondition: Starting at location, the elements
  46.         //               of the list are shifted down,
  47.         //               list[location] = insertItem;, and
  48.         //               length++;    
  49.         //       If the list is full or location is out of
  50.         //     range, an appropriate message is displayed.    
  51.    void insertEnd(const elemType& insertItem);
  52.         //Function to insert an item at the end of the list
  53.         //The parameter insertItem specifies the item to be
  54.         //inserted.
  55.         //Postcondition: list[length] = insertItem; and length++;
  56.         //               If the list is full, an appropriate
  57.         //               message is displayed.
  58.     void removeAt(int location);
  59.         //Function to remove the item from the list at the
  60.         //position specified by location
  61.         //Postcondition: The list element at list[location] is
  62.         //               removed and length is decremented by 1.
  63.         //       If location is out of range, an appropriate message
  64.         //       is displayed.
  65.     void retrieveAt(int location, elemType& retItem);
  66.         //Function to retrieve the element from the list at the  
  67.         //position specified by location
  68.         //Postcondition: retItem = list[location]
  69.         //      If location is out of range, an appropriate
  70.         //      message is displayed.
  71.     void replaceAt(int location, const elemType& repItem);
  72.         //Function to replace the elements in the list at the
  73.         //position specified by location. The item to be replaced
  74.         //is specified by the parameter repItem.
  75.         //Postcondition: list[location] = repItem
  76.         //      If location is out of range, an appropriate
  77.         //      message is displayed.
  78.     void clearList();
  79.         //Function to remove all the elements from the list
  80.         //After this operation, the size of the list is zero.
  81.         //Postcondition: length = 0;
  82.     int seqSearch(const elemType& item);
  83.         //Function to search the list for a given item.
  84.         //Postcondition: If the item is found, returns the location
  85.         //               in the array where the item is found;
  86.         //               otherwise, returns -1.
  87.     void insert(const elemType& insertItem);
  88.         //Function to insert the item specified by the parameter
  89.         //insertItem at the end of the list. However, first the
  90.         //list is searched to see whether the item to be inserted
  91.         //is already in the list.
  92.         //Postcondition: list[length] = insertItem and length++
  93.         //     If the item is already in the list or the list
  94.         //     is full, an appropriate message is displayed.
  95.     void remove(const elemType& removeItem);
  96.         //Function to remove an item from the list. The parameter
  97.         //removeItem specifies the item to be removed.
  98.         //Postcondition: If removeItem is found in the list,
  99.         //      it is removed from the list and length is
  100.         //      decremented by one.
  101.    
  102.     void removeAll(const elemType& removeItem);
  103.         //Function to remove all occurences of an item from the list. The parameter
  104.         //removeItem specifies the item to be removed.
  105.         //Postcondition: If removeItem is found in the list,
  106.         //      it is removed from the list and length is reduced
  107.  
  108.     arrayListType(int size = 100);
  109.         //constructor
  110.         //Creates an array of the size specified by the
  111.         //parameter size. The default array size is 100.
  112.         //Postcondition: The list points to the array, length = 0,
  113.         //               and maxSize = size
  114.  
  115.     arrayListType(const arrayListType<elemType>& otherList);
  116.         //copy constructor
  117.  
  118.     ~arrayListType();
  119.         //destructor
  120.         //Deallocates the memory occupied by the array.
  121.  
  122. protected:
  123.     elemType *list;     //array to hold the list elements
  124.     int length;     //to store the length of the list
  125.     int maxSize;        //to store the maximum size of the list
  126. };
  127.  
  128.  
  129.  
  130. template <class elemType>
  131. bool arrayListType<elemType>::isEmpty()
  132. {
  133.     return (length == 0);
  134. }
  135.  
  136. template <class elemType>
  137. bool arrayListType<elemType>::isFull()
  138. {
  139.     return (length == maxSize);
  140. }
  141.  
  142. template <class elemType>
  143. int arrayListType<elemType>::listSize()
  144. {
  145.     return length;
  146. }
  147.  
  148. template <class elemType>
  149. int arrayListType<elemType>::maxListSize()
  150. {
  151.     return maxSize;
  152. }
  153.  
  154. template <class elemType>
  155. void arrayListType<elemType>::print() const
  156. {
  157.     for(int i = 0; i < length; i++)
  158.         cout<<list[i]<<" ";
  159.     cout<<endl;
  160. }
  161.  
  162. template <class elemType>
  163. bool arrayListType<elemType>::isItemAtEqual
  164.                    (int location, const elemType& item)
  165. {
  166.     return(list[location] == item);
  167. }
  168.  
  169. template <class elemType>
  170. void arrayListType<elemType>::insertAt
  171.                    (int location, const elemType& insertItem)
  172. {
  173.     if(location < 0 || location >= maxSize)
  174.         cerr<<"The position of the item to be inserted "
  175.             <<"is out of range."<<endl;
  176.     else
  177.         if(length >= maxSize)  //list is full
  178.             cerr<<"Cannot insert in a full list."<<endl;
  179.         else
  180.         {
  181.             for(int i = length; i > location; i--)
  182.                 list[i] = list[i - 1];  //move the elements down
  183.  
  184.             list[location] = insertItem;    //insert the item at the
  185.                                         //specified position
  186.  
  187.             length++;   //increment the length
  188.         }
  189. } //end insertAt
  190.  
  191. template <class elemType>
  192. void arrayListType<elemType>::insertEnd(const elemType& insertItem)
  193. {
  194.     if(length >= maxSize)  //the list is full
  195.         cerr<<"Cannot insert in a full list."<<endl;
  196.     else
  197.     {
  198.         list[length] = insertItem;  //insert the item at the end
  199.         length++;   //increment length
  200.     }
  201. } //end insertEnd
  202.  
  203. template <class elemType>
  204. void arrayListType<elemType>::removeAt(int location)
  205. {
  206.     if(location < 0 || location >= length)
  207.         cerr<<"The location of the item to be removed "
  208.             <<"is out of range."<<endl;
  209.     else
  210.     {
  211.         for(int i = location; i < length - 1; i++)
  212.             list[i] = list[i+1];
  213.  
  214.         length--;
  215.     }
  216. } //end removeAt
  217.  
  218. template <class elemType>
  219. void arrayListType<elemType>::retrieveAt
  220.                      (int location, elemType& retItem)
  221. {
  222.     if(location < 0 || location >= length)
  223.         cerr<<"The location of the item to be retrieved is "
  224.             <<"out of range."<<endl;
  225.     else
  226.         retItem = list[location];
  227. } // retrieveAt
  228.  
  229. template <class elemType>
  230. void arrayListType<elemType>::replaceAt
  231.                     (int location, const elemType& repItem)
  232. {
  233.     if(location < 0 || location >= length)
  234.         cerr<<"The location of the item to be replaced is "
  235.             <<"out of range."<<endl;
  236.     else
  237.         list[location] = repItem;
  238.  
  239. } //end replaceAt
  240.  
  241. template <class elemType>
  242. void arrayListType<elemType>::clearList()
  243. {
  244.     length = 0;
  245. } // end clearList
  246.  
  247. template <class elemType>
  248. int arrayListType<elemType>::seqSearch(const elemType& item)
  249. {
  250.     int loc;
  251.     bool found = false;
  252.  
  253.     for(loc = 0; loc < length; loc++)
  254.        if(list[loc] == item)
  255.        {
  256.         found = true;
  257.         break;
  258.        }
  259.  
  260.     if(found)
  261.         return loc;
  262.     else
  263.         return -1;
  264. } //end seqSearch
  265.  
  266. template <class elemType>
  267. void arrayListType<elemType>::insert(const elemType& insertItem)
  268. {
  269.     int loc;
  270.  
  271.     if(length == 0)                  //list is empty
  272.         list[length++] = insertItem; //insert the item and
  273.                                      //increment the length
  274.     else
  275.         if(length == maxSize)
  276.             cerr<<"Cannot insert in a full list."<<endl;
  277.         else
  278.         {
  279.             loc = seqSearch(insertItem);
  280.  
  281.             if(loc == -1)   //the item to be inserted
  282.                             //does not exist in the list
  283.                 list[length++] = insertItem;
  284.             else
  285.                 cerr<<"the item to be inserted is already in "
  286.                     <<"the list. No duplicates are allowed."<<endl;
  287.     }
  288. } //end insert
  289.  
  290. template <class elemType>
  291. void arrayListType<elemType>::remove(const elemType& removeItem)
  292. {
  293.     int loc;
  294.  
  295.     if(length == 0)
  296.         cerr<<"Cannot delete from an empty list."<<endl;
  297.     else
  298.     {
  299.         loc = seqSearch(removeItem);
  300.  
  301.         if(loc != -1)
  302.             removeAt(loc);
  303.         else
  304.             cout<<"The tem to be deleted is not in the list."
  305.                 <<endl;
  306.     }
  307.  
  308. } //end remove
  309.  
  310. template <class elemType>
  311. void arrayListType<elemType>::removeAll(const elemType& removeItem)
  312. {
  313. int n;
  314. n = maxSize;
  315. int loc;
  316. int counter;
  317. loc = seqSearch(removeItem);
  318.  
  319. // removeItem was passed down
  320.  
  321. for  (counter= 0; n>counter; counter++)
  322.  
  323. {
  324.         remove(removeItem);
  325.  
  326.             }  
  327. cout << "All instances have been removed";
  328. } //end removeAll
  329.  
  330.  
  331. template <class elemType>
  332. arrayListType<elemType>::arrayListType(int size)
  333. {
  334.     if(size < 0)
  335.     {
  336.         cerr<<"The array size must be positive. Creating "
  337.             <<"an array of size 100. "<<endl;
  338.  
  339.        maxSize = 100;
  340.     }
  341.     else
  342.        maxSize = size;
  343.  
  344.     length = 0;
  345.  
  346.     list = new elemType[maxSize];
  347.     assert(list != NULL);
  348. }
  349.  
  350. template <class elemType>
  351. arrayListType<elemType>::~arrayListType()
  352. {
  353.     delete [] list;
  354. }
  355.  
  356.     //copy constructor
  357. template <class elemType>
  358. arrayListType<elemType>::arrayListType
  359.                    (const arrayListType<elemType>& otherList)
  360. {
  361.    maxSize = otherList.maxSize;
  362.    length = otherList.length;
  363.    list = new elemType[maxSize];    //create the array
  364.    assert(list != NULL);    //terminate if unable to allocate
  365.                             //memory space
  366.  
  367.    for(int j = 0; j < length; j++)  //copy otherList
  368.       list [j] = otherList.list[j];
  369. }//end copy constructor
  370.  
  371.  
  372. template <class elemType>
  373. const arrayListType<elemType>& arrayListType<elemType>::operator=
  374.             (const arrayListType<elemType>& otherList)
  375. {
  376.     if(this != &otherList)  //avoid self-assignment
  377.     {                  
  378.        delete [] list;                 
  379.        maxSize = otherList.maxSize;        
  380.        length = otherList.length;              
  381.      
  382.        list = new elemType[maxSize];           
  383.        assert(list != NULL);               
  384.  
  385.        
  386.        for(int i = 0; i < length; i++)     
  387.            list[i] = otherList.list[i];    
  388.     }
  389.  
  390.     return *this;                          
  391. }
  392.  
  393.  
  394. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement