This lab is due by Tuesday, November 01, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.
Instructions
This lab will generalize the Linked List created in Lab4B. Change your
Node
and LinkedList
classes to be templated. Then add, or modify, the following methods:
LinkedList(const LinkedList<T>& OTHER)
- Copy Constructor that performs a deep copy of the other listLinkedList& operator=(const LinkedList<T>& OTHER)
- Copy Assignment Operator that deallocates the existent list (if any) then performs a deep copy of the other listT get(const int POS) const
- if POS exists, returns the value at that position. Otherwise, throwsstd::out_of_range
exceptionvoid set(const int POS, const T VAL)
- if POS exists, sets the value at that position. Otherwise, does nothingvoid insert(const int POS, const T VAL)
- inserts a node before the given POS. If POS is negative, inserts at the beginning of the list. If POS is greater than the current size, inserts at the end of the list. Updates head, tail, and size as appropriateT max()
- returns the max value within the list. Otherwise, throwsstd::out_of_range
exceptionT min()
- returns the min value within the list. Otherwise, throwsstd::out_of_range
exceptionint find(const T TARGET)
- returns the position of the first occurrence of the target. If target doesn't exist, returns -1Node<T>* mMakeNodeForValue(const T VALUE)
- creates a node with provided value and null next pointer
Be sure to mark any methods that do not modify any data members as const
appropriately.
To test your implementation, perform in main.cpp
the following steps in order:
- Create a doubly linked list of integers
- Add the value 6 at pos 0
- Add the value 5 at pos 0
- Add the value 7 at pos 5
- Add the value 1 at pos -3
- Add the value 2 at pos 1
- Add the value 9 at pos 2
- Add the value 3 at pos 2
- Print the list forwards (prints 1 2 3 9 5 6 7)
- Set pos 3 to be 4
- Print the list forwards (prints 1 2 3 4 5 6 7)
- Remove pos -2
- Remove pos 9
- Remove pos 2
- Get and print pos 2 (prints 5)
- Print the size (prints 4)
- Print the list forwards (prints 2 3 5 6)
- Create a second list of integers using the default constructor
- Print out the second list's size (should be zero)
- Assign the first list to the second list
- Print out both their sizes (should both be four)
- Add the following values to the original first list
- Add the value 1 at pos 0
- Add the value 0 at pos 0
- Print out both of their sizes (first should be six, second should be four)
- Create a third list of integers using the copy constructor and providing the original first list as the argument
- Print out all three sizes (first is six, second four, third six)
- Add the following values to the original first list
- Add the value 8 at pos 10
- Add the value 9 at pos 10
- Print out all three sizes (first is eight, second four, third six)
- Delete all the lists to clean up the memory
- Create a list of strings
- Add the value "Hello" at pos 0
- Add the value "World" at pos 1
- Print the list forwards (prints Hello World)
- Delete the list of strings
Lab Submission
Submit your Node.hpp, LinkedList.hpp, main.cpp, Makefile
file(s).
You will submit your solution to this lab with the rest of Set4. Detailed instructions for doing this are posted in Assignment 4.
This lab is due by Tuesday, November 01, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.