Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*creates a list based on the user defined size, utilizing the insertFirst Function;
- Pre: - integer for the size of the list
- Post: - dynamically allocated list of size listSize*/
- template<class T>
- void List<T>::newList(int listSize)
- {
- for (int i = 0; i < listSize; i++) //can add option to allow user insert the data into the list on creation.
- {
- Node<T> *listCreate = new Node<T>;
- insertFirst(listCreate);
- listCreate = nullptr;
- delete listCreate;
- }
- }
- /*Traverse list from start to finish and set the last Node
- Pre: - void
- Post: - traverse pointer will point to the last value*/
- template<class T>
- void List<T>::setLast()
- {
- Node<T> *traverse = new Node<T>;
- traverse = first;
- for (int i = 1; i < count; i++)
- traverse = traverse->getNext();
- last = traverse;
- }
- /*Insert a new Node to the front of the List
- Pre: - Generic Node pointer newFirst with value
- Post: - new Node inserted to front of list*/
- template <class T>
- void List<T>::insertFirst(Node<T> *newFirst)
- {
- newFirst->setNext(first);
- first = newFirst;
- count++;
- }
- /*Insert a new Node to the back of the List
- Pre: - Generic Node pointer newLast with value
- Post: - new Node inserted to back of list*/
- template <class T>
- void List<T>::insertLast(Node<T> *newLast)
- {
- Node<T> *traverse = new Node<T>;
- traverse = first;
- for (int i = 1; i < count; i++)
- traverse = traverse->getNext(); //traverse is at the last;
- traverse->setNext(newLast);
- 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.
- delete traverse;
- count++;
- }
- /*Insert a new Node at a user defined position in the List
- Pre: - Generic Node pointer newInsert with value
- - integer for the position in the list to insert new Node
- Post: - new Node is inserted at a user defined position in the list*/
- template <class T>
- void List<T>::insertAny(Node<T> *newInsert, int position)
- {
- 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.
- {
- Node<T> *temp = new Node<T>;
- temp = first;
- for (int i = 1; i < position - 1; i++)
- temp = temp->getNext(); //set temp to right before the desired position
- newInsert->setNext(temp->getNext()); //new node points to what temp is pointing to
- temp->setNext(newInsert); //temp now points to new node
- temp = nullptr;
- delete temp;
- count++;
- }
- else if (position == 1) //insert at first
- {
- insertFirst(newInsert);
- }
- else if (count + 1 == position) //insert at last
- {
- insertLast(newInsert);
- }
- else
- std::cout << "Out of the bounds of the list, enter a valid location.";
- }
- /*Delete the first Node in the List
- Pre: void
- Post: - first Node is removed from the list with its next pointer becoming the new front*/
- template <class T>
- void List<T>::deleteFirst()
- {
- Node<T> *temp = new Node<T>;
- temp = first;
- first = first->getNext(); //head now points to the second node;
- delete temp; //original first is deleted.
- temp = nullptr;
- count--;
- }
- /*Delete the last Node in the List
- Pre: void
- Post: - last Node is removed from the list with the pointer pointing to it becoming the new back*/
- template <class T>
- void List<T>::deleteLast()
- {
- Node<T> *traverse = new Node<T>;
- Node<T> *temp = new Node<T>;
- traverse = first;
- for (int i = 1; i < count - 1; i++)
- traverse = traverse->getNext(); //traverse set to the node right before the last;
- temp = traverse->getNext(); //temp is the last node;
- traverse->setNext(NULL);
- delete temp;
- temp = nullptr;
- traverse = nullptr;
- delete traverse; //done in reverse to preserve data;
- count--;
- }
- /*Delete a Node at a user defined position in the List
- Pre: - integer for the position in the list
- Post: - Node removed from user defined position in the list*/
- template <class T>
- void List<T>::deleteAny(int position)
- {
- if (position == 1)
- {
- deleteFirst();
- }
- else if (position > 1 && position < count)
- {
- Node<T> *traverse = new Node<T>;
- Node<T> *temp = new Node<T>;
- traverse = first;
- for (int i = 1; i < position - 1; i++)
- traverse = traverse->getNext(); //get traverse to point right before the one that needs to be deleted
- temp = traverse->getNext(); //temp is now the one needed for deletion;
- traverse->setNext(temp->getNext()); //have traverse point to the location that temp is pointing 2.
- delete temp; //delete the data
- temp = nullptr;
- traverse = nullptr;
- delete traverse; //done in reverse to preserve the data of the node used to navigate.
- count--;
- }
- else if (position == count)
- {
- deleteLast();
- }
- else
- std::cout << "Out of the bounds of the list, enter a valid location.";
- }
- /*Delete all Nodes in the List
- Pre: void
- Post: - removes all Nodes from the list*/
- template <class T>
- void List<T>::emptyList()
- {
- for (int i = 0; i < count;) //no i++ cause the delete first continually loweres the count;
- deleteFirst();
- first = nullptr;
- last = nullptr;
- }
- /*Return the value stored within the Node at the position defined by the user in the List
- Pre: - integer for position in list
- Post: - list Nodes are traversed until user defined position is reached
- Return: - generic data type value of current Node*/
- template <class T>
- T List<T>::getData(int position)
- {
- Node<T> *traverse = new Node<T>;
- traverse = first;
- for (int i = 1; i < position; i++)
- traverse = traverse->getNext();
- T dummy = traverse->getData();
- traverse = nullptr;
- delete traverse;
- return dummy;
- }
- /*Return the index of the Node containing the value defined by the user in the List
- Pre: - generic data type value used to search for in list
- Post: - list Nodes are traversed until user defined value is found
- Return: - integer value representing position of Node with userData value in list*/
- template <class T>
- int List<T>::findAny(T userData) //might need to rework to better suit the case of data not being found.
- { // 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.
- T nodeData = first->getData(); // nodeData intialized with first nodes data
- int position = 1; //set to node data having first nodes data;
- while (userData != nodeData && position < count)
- {
- position++; //set to 2, cause while loop will catch if userdata si the same as the data in first node.
- nodeData = getData(position);
- }
- if (userData == nodeData)
- return position;
- else
- return -1; //-1 to signify that the position was not found.
- }
- /*Display all Node values in the List
- Pre: void
- Post: - all list Nodes are traversed and displayed on console*/
- template<class T>
- void List<T>::printList() //ayyyyy dis stuff does the printing
- {
- T tempData;
- Node<T> *traverse = new Node<T>;
- traverse = first;
- if (traverse != NULL)
- {
- for (int i = 1; i <= count; i++)
- {
- tempData = traverse->getData();
- std::cout << tempData << std::endl;
- traverse = traverse->getNext();
- }
- }
- traverse = nullptr;
- delete traverse;
- }
- /*Display all Node values prefixed by a number, in order, from the front to the back of the List
- Pre: void
- Post: - all list Nodes are traversed and displayed in numbered order*/
- template<class T>
- void List<T>::printNumList() //ayyyyy dis stuff does the printing
- {
- T tempData;
- Node<T> *traverse = new Node<T>;
- traverse = first;
- if (traverse != NULL)
- {
- for (int i = 1; i <= count; i++)
- {
- tempData = traverse->getData();
- std::cout << "(" << i << ")" << tempData << std::endl;
- traverse = traverse->getNext();
- }
- }
- traverse = nullptr;
- delete traverse;
- }
- /*Insert a new Node into the a position that will keep the List sorted
- Pre: - Generic Node pointer with value to insert into list
- Post: - new Node is inserted into list position that keeps it sorted*/
- template<class T>
- void List<T>::sortInsert(Node<T> *newInsert)
- {
- int position = 1;
- Node<T> *temp = new Node<T>;
- temp = first;
- if (position <= getCount())
- {
- while (temp != NULL && newInsert->getData() > temp->getData())
- {
- temp = temp->getNext();
- position++;
- }
- }
- insertAny(newInsert, position);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement