Phr0zen_Penguin

SortedList.cpp - VTC C++ Data Structures Sorted List

Apr 6th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. /**
  2.  * [SortedList.cpp]
  3.  * The SortedList class definition/implementation.
  4.  *
  5.  * The sorted list is similar to the Sorted one.  The only difference is, the sorted one is in
  6.  * order.
  7.  */
  8.  
  9. #include "SortedList.h"
  10.  
  11. /**
  12.  * SortedList CLASS CONSTRUCTOR(s):
  13.  */
  14. SortedList::SortedList()
  15. {
  16.     length = 0;
  17.     currentPos = -1;
  18. }
  19.  
  20.  
  21. /**
  22.  * SortedList CLASS ACCESSORS:
  23.  */
  24. float SortedList::GetNextItem()
  25. {
  26.     currentPos++;
  27.  
  28.     return data[currentPos];
  29. }
  30.  
  31. float SortedList::GetPrevItem()
  32. {
  33.     currentPos--;
  34.  
  35.     return data[currentPos];
  36. }
  37.  
  38. float SortedList::RetrieveItem(int position)
  39. {
  40.     return data[position];
  41. }
  42.  
  43.  
  44. /**
  45.  * SortedList CLASS MUTATORS:
  46.  */
  47. void SortedList::InsertItem(float item)
  48. {
  49.     int location = 0;
  50.  
  51.         /**
  52.          * While location is smaller than length (before the end of the list is reached), and the
  53.          * the item in the current location is smaller than the item being inserted...
  54.          */
  55.         while(location < length && data[location] < item)
  56.             location++; // ...keep incrementing the location by 1
  57.  
  58.         /**
  59.          * After the location is found, start shifting all the items, after it, to the right.
  60.          *
  61.          * Starting at the end of the list, while the index is greater than the location (because
  62.          * it's the items after that location that are being shifted), decrement the index.
  63.          */
  64.         for(int index = length; index > location; index--)
  65.             data[index] = data[index - 1]; // move each item in the list one place to the right
  66.  
  67.     data[location] = item; // insert the item at the assigned location
  68.     length++; // increase the length of the list to reflect the insertion
  69. }
  70.  
  71. void SortedList::DeleteItem(float item)
  72. {
  73.     /**
  74.      * Starting from the beginning (visiting all items on the list), while the index is smaller than
  75.      * the length of the list, increment the index.
  76.      */
  77.     for(int index = 0; index < length; index++)
  78.         /**
  79.          * If the item being deleted is equal to the item at the current index...
  80.          */
  81.         if(item == data[index])
  82.         {
  83.                 /**
  84.                  * ...using a sub-index (starting at the current index (the item being deleted)),
  85.                  * while the sub-index is less than the length of the list, increment the sub-index.
  86.                  */
  87.                 for(int subIndex = index; subIndex < length; subIndex++)
  88.                 {
  89.                     /**
  90.                      * Shift all of the items (after the current item being deleted), one place to
  91.                      * the left (replacing the item being deleted).
  92.                      */
  93.                     data[subIndex] = data[subIndex + 1];
  94.  
  95.                     /**
  96.                      * Decrement the index to accordingly, to point to the same item as it was,
  97.                      * before the item was deleted (while the sub-for loop is still recursing).
  98.                      * (The index, in this case, is analogous with a stack pointer.)
  99.                      *
  100.                      * NOTE:
  101.                      * This decrement is detrimental where the same values occur more than once in
  102.                      * the list.
  103.                      */
  104.                     index--;
  105.                 }
  106.  
  107.             length--; // decrease the length of the list to reflect the deletion
  108.         }
  109. }
  110.  
  111.  
  112. /**
  113.  * SortedList CLASS GENERAL USE FUNCTIONS:
  114.  */
  115. bool SortedList::IsEmpty()
  116. {
  117.     if(length == 0)
  118.         return true;
  119.     else
  120.         return false;
  121. }
  122.  
  123. bool SortedList::IsFull()
  124. {
  125.     if(length == MAX_LENGTH_Sorted_List)
  126.         return true;
  127.     else
  128.         return false;
  129. }
  130.  
  131. void SortedList::ResetList()
  132. {
  133.     currentPos = -1;
  134. }
  135.  
  136. int SortedList::LengthIs()
  137. {
  138.     return length;
  139. }
  140.  
  141. bool SortedList::IsInTheList(float item)
  142. {
  143.     for(int i = 0; i < length; i++)
  144.         if(data[i] == item)
  145.             return true;
  146.         else
  147.             return false;
  148. }
  149.  
  150. void SortedList::Print()
  151. {
  152.     for(int i = 0; i < length; i++)
  153.         std::cout << "Item " << i << ": " << data[i] << std::endl;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment