Advertisement
Guest User

List.h

a guest
Jun 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.16 KB | None | 0 0
  1. /*creates a list based on the user defined size, utilizing the insertFirst Function;
  2.     Pre: - integer for the size of the list
  3.     Post: - dynamically allocated list of size listSize*/
  4. template<class T>
  5. void List<T>::newList(int listSize)
  6. {
  7.     for (int i = 0; i < listSize; i++) //can add option to allow user insert the data into the list on creation.
  8.     {
  9.         Node<T> *listCreate = new Node<T>;
  10.         insertFirst(listCreate);
  11.         listCreate = nullptr;
  12.         delete listCreate;
  13.     }
  14. }
  15.  
  16. /*Traverse list from start to finish and set the last Node
  17.     Pre: - void
  18.     Post: - traverse pointer will point to the last value*/
  19. template<class T>
  20. void List<T>::setLast()
  21. {
  22.     Node<T> *traverse = new Node<T>;
  23.     traverse = first;
  24.     for (int i = 1; i < count; i++)
  25.         traverse = traverse->getNext();
  26.     last = traverse;
  27. }
  28.  
  29. /*Insert a new Node to the front of the List
  30.     Pre: - Generic Node pointer newFirst with value
  31.     Post: - new Node inserted to front of list*/
  32. template <class T>
  33. void List<T>::insertFirst(Node<T> *newFirst)
  34. {
  35.     newFirst->setNext(first);
  36.     first = newFirst;
  37.     count++;
  38. }
  39.  
  40. /*Insert a new Node to the back of the List
  41.     Pre: - Generic Node pointer newLast with value
  42.     Post: - new Node inserted to back of list*/
  43. template <class T>
  44. void List<T>::insertLast(Node<T> *newLast)
  45. {
  46.     Node<T> *traverse = new Node<T>;
  47.     traverse = first;
  48.     for (int i = 1; i < count; i++)
  49.         traverse = traverse->getNext(); //traverse is at the last;
  50.     traverse->setNext(newLast);
  51.     traverse = nullptr; //deletion of pointer done in reverse to mmake sure delete traverse does not clear out the node, the delete functions will cover that.
  52.     delete traverse;
  53.     count++;
  54. }
  55.  
  56. /*Insert a new Node at a user defined position in the List
  57.     Pre: - Generic Node pointer newInsert with value
  58.          - integer for the position in the list to insert new Node
  59.     Post: - new Node is inserted at a user defined position in the list*/
  60. template <class T>
  61. void List<T>::insertAny(Node<T> *newInsert, int position)
  62. {
  63.     if (position <= count && position > 1) //anywhere inbetween the first and last nodes; <----- this seems to work so far for inserting in third position of a size 3 list.
  64.     {
  65.         Node<T> *temp = new Node<T>;
  66.         temp = first;
  67.         for (int i = 1; i < position - 1; i++)
  68.             temp = temp->getNext(); //set temp to right before the desired position
  69.         newInsert->setNext(temp->getNext()); //new node points to what temp is pointing to
  70.         temp->setNext(newInsert); //temp now points to new node
  71.         temp = nullptr;
  72.         delete temp;
  73.         count++;
  74.     }
  75.     else if (position == 1) //insert at first
  76.     {
  77.         insertFirst(newInsert);
  78.     }
  79.     else if (count + 1 == position) //insert at last
  80.     {
  81.         insertLast(newInsert);
  82.     }
  83.     else
  84.         std::cout << "Out of the bounds of the list, enter a valid location.";
  85. }
  86.  
  87. /*Delete the first Node in the List
  88.     Pre: void
  89.     Post: - first Node is removed from the list with its next pointer becoming the new front*/
  90. template <class T>
  91. void List<T>::deleteFirst()
  92. {
  93.     Node<T> *temp = new Node<T>;
  94.     temp = first;
  95.     first = first->getNext(); //head now points to the second node;
  96.     delete temp; //original first is deleted.
  97.     temp = nullptr;
  98.     count--;
  99. }
  100.  
  101. /*Delete the last Node in the List
  102.     Pre: void
  103.     Post: - last Node is removed from the list with the pointer pointing to it becoming the new back*/
  104. template <class T>
  105. void List<T>::deleteLast()
  106. {
  107.     Node<T> *traverse = new Node<T>;
  108.     Node<T> *temp = new Node<T>;
  109.     traverse = first;
  110.     for (int i = 1; i < count - 1; i++)
  111.         traverse = traverse->getNext(); //traverse set to the node right before the last;
  112.     temp = traverse->getNext(); //temp is the last node;
  113.     traverse->setNext(NULL);
  114.     delete temp;
  115.     temp = nullptr;
  116.     traverse = nullptr;
  117.     delete traverse; //done in reverse to preserve data;
  118.     count--;
  119. }
  120.  
  121. /*Delete a Node at a user defined position in the List
  122.     Pre: - integer for the position in the list
  123.     Post: - Node removed from user defined position in the list*/
  124. template <class T>
  125. void List<T>::deleteAny(int position)
  126. {
  127.     if (position == 1)
  128.     {
  129.         deleteFirst();
  130.     }
  131.     else if (position > 1 && position < count)
  132.     {
  133.         Node<T> *traverse = new Node<T>;
  134.         Node<T> *temp = new Node<T>;
  135.         traverse = first;
  136.         for (int i = 1; i < position - 1; i++)
  137.             traverse = traverse->getNext(); //get traverse to point right before the one that needs to be deleted
  138.         temp = traverse->getNext(); //temp is now the one needed for deletion;
  139.         traverse->setNext(temp->getNext()); //have traverse point to the location  that temp is pointing 2.
  140.         delete temp; //delete the data
  141.         temp = nullptr;
  142.         traverse = nullptr;
  143.         delete traverse; //done in reverse to preserve the data of the node used to navigate.
  144.         count--;
  145.     }
  146.     else if (position == count)
  147.     {
  148.         deleteLast();
  149.     }
  150.     else
  151.         std::cout << "Out of the bounds of the list, enter a valid location.";
  152.  
  153. }
  154.  
  155. /*Delete all Nodes in the List
  156.     Pre: void
  157.     Post: - removes all Nodes from the list*/
  158. template <class T>
  159. void List<T>::emptyList()
  160. {
  161.     for (int i = 0; i < count;) //no i++ cause the delete first continually loweres the count;
  162.         deleteFirst();
  163.     first = nullptr;
  164.     last = nullptr;
  165. }
  166.  
  167. /*Return the value stored within the Node at the position defined by the user in the List
  168.     Pre: - integer for position in list
  169.     Post: - list Nodes are traversed until user defined position is reached
  170.     Return: - generic data type value of current Node*/
  171. template <class T>
  172. T List<T>::getData(int position)
  173. {
  174.     Node<T> *traverse = new Node<T>;
  175.     traverse = first;
  176.     for (int i = 1; i < position; i++)
  177.         traverse = traverse->getNext();
  178.     T dummy = traverse->getData();
  179.     traverse = nullptr;
  180.     delete traverse;
  181.     return dummy;
  182. }
  183.  
  184. /*Return the index of the Node containing the value defined by the user in the List
  185.     Pre: - generic data type value used to search for in list
  186.     Post: - list Nodes are traversed until user defined value is found
  187.     Return: - integer value representing position of Node with userData value in list*/
  188. template <class T>
  189. int List<T>::findAny(T userData) //might need to rework to better suit the case of data not being found.
  190. { // instead of returning a position of the node, return the posion by referece, and actually return a string to show if the data has been found or not.
  191.     T nodeData = first->getData(); // nodeData intialized with first nodes data
  192.     int position = 1; //set to node data having first nodes data;
  193.     while (userData != nodeData && position < count)
  194.     {
  195.         position++; //set to 2, cause while loop will catch if userdata si the same as the data in first node.
  196.         nodeData = getData(position);  
  197.     }
  198.     if (userData == nodeData)
  199.         return position;
  200.     else
  201.         return -1; //-1 to signify that the position was not found.
  202. }
  203.  
  204. /*Display all Node values in the List
  205.     Pre: void
  206.     Post: - all list Nodes are traversed and displayed on console*/
  207. template<class T>
  208. void List<T>::printList() //ayyyyy dis stuff does the printing
  209. {
  210.     T tempData;
  211.     Node<T> *traverse = new Node<T>;
  212.     traverse = first;
  213.     if (traverse != NULL)
  214.     {
  215.         for (int i = 1; i <= count; i++)
  216.         {
  217.             tempData = traverse->getData();
  218.             std::cout << tempData << std::endl;
  219.             traverse = traverse->getNext();
  220.         }
  221.     }
  222.     traverse = nullptr;
  223.     delete traverse;
  224. }
  225.  
  226. /*Display all Node values prefixed by a number, in order, from the front to the back of the List
  227.     Pre: void
  228.     Post: - all list Nodes are traversed and displayed in numbered order*/
  229. template<class T>
  230. void List<T>::printNumList() //ayyyyy dis stuff does the printing
  231. {
  232.     T tempData;
  233.     Node<T> *traverse = new Node<T>;
  234.     traverse = first;
  235.     if (traverse != NULL)
  236.     {
  237.         for (int i = 1; i <= count; i++)
  238.         {
  239.             tempData = traverse->getData();
  240.             std::cout << "(" << i << ")" << tempData << std::endl;
  241.             traverse = traverse->getNext();
  242.         }
  243.     }
  244.     traverse = nullptr;
  245.     delete traverse;
  246. }
  247.  
  248. /*Insert a new Node into the a position that will keep the List sorted
  249.     Pre: - Generic Node pointer with value to insert into list
  250.     Post: - new Node is inserted into list position that keeps it sorted*/
  251. template<class T>
  252. void List<T>::sortInsert(Node<T> *newInsert)
  253. {
  254.     int position = 1;
  255.     Node<T> *temp = new Node<T>;
  256.     temp = first;
  257.     if (position <= getCount())
  258.     {
  259.         while (temp != NULL && newInsert->getData() > temp->getData())
  260.         {
  261.             temp = temp->getNext();
  262.             position++;
  263.         }
  264.     }
  265.     insertAny(newInsert, position);
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement