Advertisement
Phr0zen_Penguin

UnsortedLinkedList.cpp - VTC C++ Unsorted Linked List

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