Homework 2, Spinning in Circles.
Short Description:
Implement a doubly linked list circular class.
This assignment is worth 50 points.
Goals
When you finish this homework, you should have:
- Refreshed you knowledge of using dynamic memory in classes.
- Implemented a class using linked lists.
Formal Description
One interesting likked list type structure is the doubly linked circular list (DLCL) . This will allow for a very nice "random selection" feature where the program can generate a random offset from the current creature, either positive or negative, and easily move by that amount to find the next creature.
Your assignment is to implement this class.
A doubly linked list is a list where each node is linked to both neighbors. In the node structure there are most likely two pointers, a left pointer which points to the node to the left, and a right pointer which points to the node to the right.
The double links make it easy to traverse the list in either direction.
In a circularly linked list the first node is connected to the last node, forming a circle. There is still a head node pointing to the first node in the list, but you can move from the last node in the list to the first node in the list via a link. In a doubly linked circular list, the last node and the head node are fully linked to each other.
For a DLCL with one node, that node will have two pointers pointing the node.
Until we learn to develop templates, we must fix the data type. For this exercise, use std::string as the data type to store in each node.
Until we learn how to deal with exceptions, your class will print an error and return a standard value when the user attempts to perform an illegal operation. Full details are provided below.
You should implement your DLCL in separate source and header files to allow use in multiple program. Please use DListT.h for your header file.
This class should support iterators, but since we don't yet have the ability to implement these, we will maintain an internal "current" pointer. This pointer will allow the user to manipulate and access the data stored in the structure. The user does not have direct access to the current pointer, but must manipulate it through calls to member functions such as Left, Right and Home.
The current pointer (current) should be invalid until data is inserted into the structured. When data is inserted, current should always point to the inserted data. When data is deleted, current should point to a valid node, if possible. Please read the details below.
Your DLCL should support the following methods:
- Size return the number of nodes in the list.
- Data return the data in the node pointed to by current.
- If current is valid
- Return the data in the node pointed to by current.
- Otherwise
- Print out the message "Error: Attempt to access Empty List."
- Return the string "NO DATA"
- Home move current to the head of the list.
- Right move current to the right by one node.
- Left move current to the left by one node.
- Delete remove the node pointed at by current.
- If the list is empty
- Print out the message "Error: Attempt to delete in an Empty List."
- If the node is the only node in the list
- The list becomes empty
- The current is not longer valid.
- If current is pointing at the head of the list
- The node to the right of current becomes the new head of the list.
- Delete the old head node.
- Current points to the new head node.
- Otherwise
- The current node is deleted
- Current points to the node left of the previous current.
- Insert insert a new node into the list at the current position.
- If the list is empty
- The new node is inserted at the head of the list
- Current points to the new node.
- Current points to the head of the list
- The new node is inserted at the head of the list.
- Current points to the new node.
Otherwise
- The new node is inserted before current.
- Current points to the new node.
- InsertAfter insert a new node into the list following the current position.
- If the list is empty
- The new node is inserted at the head of the list.
- Current points to the new node.
- Otherwise
- Insert the new node at the node following current.
- Current points to the new node.
You should also implement any routines needed to create a fully functional class containing dynamic memory. This includes the ability to pass the class by value, do assignment between instances of classes, and avoid memory leaks or other problems.
You should thoroughly tests your DLCL.
Additional Requirements
- Your solution must work on cslab103
- You must compile with the following flags: -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -Werror -std=c++17
- You may use this Makefile as the basis for your build. This file assumes you will compile a program called main.
- Your solution must compile without warnings or errors.
- Your solution must not have memory leaks or other dynamic memory errors.
- Make no changes to the header file you have been given.
- I will replace your header file with a copy of the original.
- I will supply my own test file that links against your DListT.* and tests your code.
- Make sure you provide a list of collaborators in your program documentation.
Required Files
A single tar or zip file containing the source code and makefile for this program. This should not contain object files or executables.
Submission
Please submit your single file to the D2L folder Homework 2 by the submission deadline.