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
We will begin refactoring our LinkedList operations from a structured procedural style to a more Object-Oriented approach.
Create a new file called LinkedList.h
that will store our Linked List class
declaration. Note the naming scheme of our files now. We're using the PascalCase (UpperCamelCase) to denote that this
file contains a class declaration (since it's a header) and the name of the class is LinkedList.
The Class Shell
Begin by creating the LinkedList class declaration. This class will represent a LinkedList data structure. It's contained state will be the actual linked list and it will contain the methods to manipulate that state - the linked list.
The UML for the class is described below (with a description of the corresponding use):
Linkedlist
- Data Members
- -
mpHead
:Node*
pointer to the head node of the list - -
mpTail
:Node*
pointer to the tail node of the list - -
mSize
:unsigned int
stores the current number of nodes in the list
- -
- Member Functions
- +
LinkedList()
creates an empty list that has size equal to zero and sets the head & tail pointers to be null - +
~LinkedList()
destroys the existing list by removing all nodes that are present in the list - +
get(const int POS)
:int
returns the value of the element at given position. If position doesn't exist, throws astd::out_of_range
exception - +
find(const int TARGET)
:int
returns the position of the first occurrence of the target. If target doesn't exist, returns -1 - +
insert(const int POS, const int VALUE)
:void
inserts a new node with the provided value BEFORE the provided position. That is, the newly created node will point to the current node at the provided position. If the provided position is out of range for the current list, then clamp the position to the head or tail of the list as appropriate - +
max()
: int
returns the max value within the list. If the list is empty, throws astd::out_of_range
exception - +
min()
: int
returns the min value within the list. If the list is empty, throws astd::out_of_range
exception - +
remove(const int POS)
:void
if the provided position exists in the list, removes and deallocates the associated node. If the provided position is out of range for the current list, then clamp the position to the head or tail of the list as appropriate - +
set(const int POS, const int VALUE)
:void
if the provided position exists in the list, sets the vale of the associated node. If the provided position is out of range for the current list, do nothing - +
size()
:unsigned int
returns the current number of nodes within the list - -
mMakeNodeForValue(const int VALUE)
:Node*
creates a node with provided value and null previous & next pointers
- +
- Data Members
Once the class is declared, create the corresponding implementation in a file named LinkedList.cpp
.
Again, a test suite has been provided in linked_list_class_tests.zip. This time, the test suite will not compile until the class is declared and the functions are stubbed out.
Lab Submission
Submit your LinkedList.h, LinkedList.cpp
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.