Homework 5 - Inheritance and Polymorphism
Due: Monday March 12, 2007 at 5pm
You will define four classes with the following hierarchy, plus a ListNode
class for use by List.
List
/ | \
/ | \
Linked Stack Queue
List
ListNode class
This is the basic node for the list. This assignment will use doubly linked
nodes. This node will be for integers (ie not a template class).
- Private variables
- An integer for the data, a pointer for previous
and a pointer for next.
- Public functions
-
- Default and copy constructors
- Accessor and mutator functions (e.g. getNext, setNext, getPrev and so on)
List class
The List class is the base type from which the other three classes are
derived. It has no real usable features to the main program. Its purpose is
to define common functions the other three classes will use. These common
functions are put in the protected section so only the derived classes can
access them. It has the following features:
- Protected variables
- A list node for head and a list node for tail
- Protected functions
-
- void front_insert(int value) - insert a node at the front of the list
- void insert_node(ListNode *prev, int value) - inserts a node into the middle of the list
- void back_insert(int value) - insert a node at the back of the list
- void delete_node(ListNode *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.
- Public functions
-
- Default and copy constructors
- Destructor
- virtual void print_type() - prints "Linked List" to standard out
- void head_insert(int value) - wraps around front_insert()
- void insert(ListNode *prev, int value) - wraps around insert_node()
- void tail_insert(int value) - wraps around back_insert()
- ListNode *search_forward(int value) - returns the first node that contains value, or NULL if value is not in the list
- ListNode *search_backwards(int value) - returns the last node that contains value, or NULL if value is not in the list
- void delete_first(int value) - delete the first instance of value if found, wrap around delete_node()
- void delete_last(int value) - delete the last 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(int value) - wraps around front_insert() to add value
- int pop() - wraps around delete_node() to remove the head node from the stack and return the value it contains
Queue class
This implements a queue that is derived from the base list class. It defines
the add and remove functions.
- Public functions
-
- Default and copy constructors
- Destructor
- virtual void print_type() - prints "Queue" to standard out
- void add(int value) - wraps around back_insert() to add value
- int remove() - wraps around delete_node() to remove the tail node from the queue 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 5 Menu
==========================================
1. Test the linked list class
2. Test the stack class
3. Test the queue class
0. Exit
==========================================
The submenus are defined as follows.
Linked List Menu
When Option 1 is selected, the linked list menu will be printed and the user
can select any item off the linked list menu. When the user exits the linked
list menu, they will return back to the Homework 5 menu. Do not exist the
program until the user selects Option 0 in the Homework 5 menu.
Linked List Menu
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. Insert a value at the head of the list
2. Insert a value at the tail of the list
3. Search forwards for a value
4. Search backwards for a value
5. Insert a value after the result of the previous search
6. Delete the first instance of a value
7. Delete the last instance of a value
8. Print the list contents
0. Return to main homework menu
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Options 3 and 4 should store the result of the previous search so it is
available for Option 5. If there is no prior search result available,
Option 5 should print an error and return to the menu.
Stack Menu
The stack menu is printed when Option 2 from the main homework menu is
selected. As with the linked list menu, the stack menu should return to the
main homework menu when the user selects Option 0.
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
---------------------------------
Queue Menu
The queue menu is printed when Option 3 is selected from the main homework
menu. As with the other two menus, selecting Option 0 in the queue menu should
bring the user back to the main homework menu.
Queue Menu
##################################
1. Add a value to the queue
2. Remove a value from the queue
3. Print the queue contents
0. Return to main homework menu
##################################