Advertisement
Phr0zen_Penguin

SortedLinkedList.cpp - VTC C++ Sorted Linked List

Apr 19th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1. /**
  2.  * [SortedLinkedList.cpp]
  3.  * The SortedLinkedList class definition/implementation.
  4.  */
  5.  
  6. #include "SortedLinkedList.h"
  7.  
  8.  
  9. /**
  10.  * SortedLinkedList CLASS CONSTRUCTOR(s):
  11.  */
  12. SortedLinkedList::SortedLinkedList()
  13. {
  14.     length = 0;
  15.     data = 0;
  16. }
  17.  
  18.  
  19. /**
  20.  * SortedLinkedList CLASS ACCESSORS:
  21.  */
  22. float SortedLinkedList::GetNextItem(void)
  23. {
  24.         if(currentPos == 0)     // if the current position is null...
  25.             currentPos = data;  // ...the next item will be the head
  26.         else
  27.             currentPos = currentPos->next; // move to the next item after the current position
  28.  
  29.     return currentPos->element;
  30. }
  31.  
  32.  
  33. /**
  34.  * SortedLinkedList CLASS MUTATORS:
  35.  */
  36. void SortedLinkedList::InsertItem(float item)
  37. {
  38.     /**
  39.      * NOTE:
  40.      * The idea in inserting an item into a sorted linked list, is to, first, find the location where
  41.      * the item is to be added, through finding the one before and the one after it, then insert the
  42.      * item in between them, if the location is in the middle.
  43.      *
  44.      * If the location is at the end, the item will be inserted between an item and null.  If the
  45.      * location is at the beginning, the item will be inserted between null and an item.  If the list
  46.      * is empty, a new item will be created in the list.
  47.      *
  48.      * To achieve this, the list must be traversed, keeping track of the predecessor and successor
  49.      * of the position where the item is to be deleted, and then add the item.
  50.      */
  51.     Node    *newNode;   // holds the item to be added
  52.     Node    *predLoc;   // the predecessor of the item to be added
  53.     Node    *location;  // used to traverse the list to find the location where the item will be added
  54.  
  55.     location = data;    // points to the head (the beginning of the list)
  56.     predLoc = 0;        // the predecessor of the head of the list is null
  57.  
  58.         /**
  59.          * While the location is not null (the end of the list is not reached), and the element part
  60.          * of the location is still smaller than the item to be added...
  61.          */
  62.         while(location != 0 && location->element < item)
  63.         {
  64.             predLoc = location;         // ...more the predecessor location to the current location
  65.             location = location->next;  // ...move the location to point the next item
  66.         }
  67.  
  68.     newNode = new Node();               // allocate memory for the node that will hold the item to be inserted...
  69.     newNode->element = item;            // ...add the item to be added to the element part of the node
  70.  
  71.         if(predLoc == 0)        // if the list is empty (the item is being added to the head of the list)
  72.         {
  73.             newNode->next = data;       // the new node points to data (the head)
  74.             data = newNode;             // data becomes the new node
  75.         }
  76.         else                    // if the list is not empty
  77.         {
  78.             newNode->next = location;   // the new node points to the successor of the item to be added
  79.             predLoc->next = newNode;    // the next part of the predecessor location points to the new node (containing the item to be added)
  80.         }
  81.  
  82.     length++;                           // increment the length of the list to reflect the new addition of the item
  83. }
  84.  
  85. void SortedLinkedList::DeleteItem(float item)
  86. {
  87.     /**
  88.      * NOTE:
  89.      *
  90.      * Method for deleting an item:
  91.      * i. Find the item to be deleted.
  92.      * ii. Keep the item in a temporary pointer.
  93.      * iii. Link the item before it to the item after it.
  94.      * iv. It this point the item can/will be deleted.
  95.      *
  96.      * The point here is not to lose the linkage between items if one is being deleted.  Therefore,
  97.      * there must be implementations to make sure the list is still linked.
  98.      *
  99.      * To achieve this goal, two pointers will be needed; one to keep a temporary item, and the other
  100.      * will facilitate going through the list.
  101.      */
  102.  
  103.     Node    *location;
  104.  
  105.         /**
  106.          * NOTE:
  107.          * The item to be deleted might exist in the list as duplicates.  In such a case, the list
  108.          * should be recursed until all duplicates of the provided item are deleted.
  109.          */
  110.         for(int i = 0; i < length; i++) //fix
  111.         {
  112.             Node    *pTemp;     // will hold the temporary item
  113.  
  114.             location = data;    // points to the beginning/head of the list
  115.  
  116.                 /**
  117.                  * In the event of deleting an item from a an sorted linked list, one of two
  118.                  * scenarios will exist:
  119.                  *
  120.                  * If the item to be deleted is at the head of the list, the head can simply be moved
  121.                  * to the item after that one, then it will be safe to get rid of the node.
  122.                  */
  123.                 if(item == data->element || length == 1)
  124.                 {
  125.                     pTemp = location;
  126.  
  127.                         /**
  128.                          * NOTE:
  129.                          * If the list contains only one item, it will simply be set to null (0), or
  130.                          * setting it to the next item would be impossible, hence crashing the program.
  131.                          */
  132.                         (length == 1) ? data = 0 : data = data->next;
  133.  
  134.                     length--;       //fix
  135.                 }
  136.                 /**
  137.                  * If the item to be deleted is not at the head of the list:
  138.                  */
  139.                 else
  140.                 {
  141.                         /**
  142.                          * While going through the list, the code must keep track of the item before
  143.                          * the one to be deleted.  Also, the end of the list must not be passed.
  144.                          */
  145.                         while(!(item == (location->next)->element) && (location->next)->next != 0)  // location will always store the item before the one to be deleted
  146.                         {
  147.                             location = location->next;  // move location to the one next to it until it is found
  148.                         }
  149.  
  150.                         /**
  151.                          * NOTE:
  152.                          * If the item to be deleted is actually found, the following block of code
  153.                          * will be executed.
  154.                          *
  155.                          * The condition is used to safeguard against an attempt to delete a provided
  156.                          * item that is not on the list.
  157.                          */
  158.                         if(location->next->element == item)
  159.                         {
  160.                             pTemp = location->next; // store the item to be deleted in the temporary pointer
  161.  
  162.                             length--; // decrement the list length to reflect the item deletion
  163.  
  164.                             location->next = (location->next)->next; // link the item before the one to be deleted to the one after it
  165.                         }
  166.                 }
  167.  
  168.             delete pTemp; // delete the item/unused pointer in either case
  169.         }
  170. }
  171.  
  172.  
  173. /**
  174.  * SortedLinkedList CLASS GENERAL USE FUNCTIONS:
  175.  */
  176. bool SortedLinkedList::IsEmpty(void)
  177. {
  178.     if(length == 0)
  179.         return true;
  180.     else
  181.         return false;
  182. }
  183.  
  184. bool SortedLinkedList::IsFull(void)
  185. {
  186.     Node    *pNode = new Node();    // attempts to allocate memory for another element
  187.  
  188.         if(pNode == 0)              // if the memory allocation attempt failed...
  189.             return true;            // ...the list is full
  190.         else
  191.         {
  192.             delete pNode;
  193.  
  194.             return false;
  195.         }
  196. }
  197.  
  198. void SortedLinkedList::ResetList(void)
  199. {
  200.     currentPos = 0;
  201. }
  202.  
  203. void SortedLinkedList::MakeEmpty(void)
  204. {
  205.     Node    *pTempNode = new Node();    // allocates memory for a temporary node
  206.  
  207.         while(data != 0)
  208.         {
  209.             pTempNode = data;           // stores an element of the list in the temporary pointer
  210.  
  211.             data = data->next;          // points to the next item in the list (move to the next one)
  212.  
  213.             delete pTempNode;           // delete the temporary pointer
  214.         }
  215.  
  216.     length = 0;                         // sets the length of the list to 0, reflecting its items deletion
  217. }
  218.  
  219. int SortedLinkedList::LengthIs(void)
  220. {
  221.     return length;
  222. }
  223.  
  224. bool SortedLinkedList::IsInTheList(float item)
  225. {
  226.     Node    *location = new Node();     // the current location in the list
  227.  
  228.     location = data;                    // starting from the beginning of the list
  229.  
  230.  
  231.      bool   found = false;
  232.  
  233.         while(location != 0 && !found)
  234.         {
  235.             /**
  236.              * NOTE:
  237.              * Location is a node, that has the element, which is the value being added...
  238.              */
  239.             if(item == location->element)
  240.                 found = true;
  241.             else
  242.                 /**
  243.                  * ...it also contains a pointer to the next node, through the next variable.
  244.                  */
  245.                 location = location->next;  // move to the next item if the item is not found
  246.         }
  247.  
  248.     return found;
  249. }
  250.  
  251. void SortedLinkedList::Print(void)
  252. {
  253.     Node    *location = new Node();
  254.  
  255.     location = data;    // points to the head
  256.  
  257.         while(location != 0)    // while the end of the list is not reached
  258.         {
  259.             std::cout << location->element << std::endl; // ...print the element part of each node
  260.  
  261.             location = location->next;  // ...move to the next item
  262.         }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement