Homework 7 - Extra Credit
Due: Wednesday March 18, 2009 at 10:00am (Before the final)
Absolutely NO late assignments will be accepted.
You will define three classes with the following hierarchy, plus a ListNode
class for use by List.
List
/ \
/ \
Linked Stack
List
ListNode class
This is the basic node for the list. This node will be a template class.
It holds the data and the pointer to the next node.
- Private variables
-
- A template variable to hold the data
- A template pointer to the next node
- Public functions
-
- Default and copy constructors
- A constructor that takes an element and a pointer. It will create a node
that holds the element as its data and points to the pointer as the next
node.
- Even though this class has a pointer, there is NO destructor for the class.
The List class will handle deallocation of the nodes.
- Accessor and mutator functions (e.g. getNext, setNext, getData and setData)
List class
The List class is the base class from which the other two classes are
derived. It has no real usable features to the main program. Its purpose is
to define common functions the other two classes will use. These common
functions are put in the protected section so only the derived classes can
access them. Since it uses the ListNode class for a member variable, it is
a template class. It has the following features:
- Protected variables
-
- A list node for the head node
- Protected functions
-
- void front_insert(element) - insert a node at the front of the list
- element front_delete() - delete the front of the list and return the value stored in it
- void insert_node(ListNode<T> *prev, element) - inserts a node into the middle of the list
- void delete_node(ListNode<T> *node) - delete the specified node from the list
- Public functions
-
LinkedList class
This class takes the base class and creates a fully working linked list. It
will expand the base class to allow searching and deleting specific values.
- Public functions
-
- Default and copy constructors
- Destructor
- virtual void print_type() - prints "Linked List" to standard out
- void head_insert(element) - wraps around front_insert()
- void insert(ListNode *prev, element) - wraps around insert_node()
- ListNode *search_forward(element) - returns the first node that contains value, or NULL if value is not in the list
- void delete_first(element) - delete the first instance of value if found, wrap around delete_node()
Stack class
This will implement a stack that is derived from the base list class. It will
define the push and pop functions.
- Public functions
-
- Default and copy constructors
- Destructor
- virtual void print_type() - prints "Stack" to standard out
- void push(element) - wraps around front_insert() to add value
- element pop() - wraps around delete_front() to remove the head node from the stack and return the value it contains
Menu Program
Once the classes are defined, code the following nested menu program to test
that the classes work. A nested menu program has multiple menus that are
selected from a main menu. When the program is first invoked, the main menu
is presented. Options on the main menu give access to the sub-menus. For this
assignment, use the following main menu:
Welcome to the CS222 Homework 7 Menu
==============================================
1. Test the linked list class with integers
2. Test the linked list class with characters
3. Test the stack class with integers
4. Test the stack class with doubles
0. Exit
==============================================
The submenus are defined as follows.
Linked List Menu
Implement this as a template function, similar to Homework 6.
Linked List Menu
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. Insert a value at the head of the list
2. Search forwards for a value
3. Insert a value after the result of the previous search
4. Delete the first instance of a value
5. Print the list contents
0. Return to main homework menu
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Option 2 should store the result of the previous search so it is
available for Option 3. If there is no prior search result available,
Option 3 should print an error and return to the menu.
Stack Menu
Implement this as a template function, similar to Homework 6.
Stack Menu
---------------------------------
1. Push a value onto the stack
2. Pop a value off of the stack
3. Print the stack contents
0. Return to main homework menu
---------------------------------