Homework 7 - Extra Credit
Due: Wednesday March 17, 2009 at noon.
Absolutely NO late assignments will be accepted.
This assignment is worth up to 20 extra credit points in your Homework/Lab
grading category.
The purpose of this assignment is to make an integer linked list and stack
by inheriting from a general parent list class.
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. It holds the data and the pointer to
the next node.
- Private variables
-
- A integer variable to hold the data
- A ListNode pointer to the next node
- Public functions
-
- Default and copy constructors
- A constructor that takes an integer and a ListNode pointer. It will create
a node that holds the integer as its data and points to the ListNode
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 for the data and next pointer (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. It has the following features:
- Protected variables
-
- A list node for the head node
- Protected functions
-
- void front_insert(int) - insert a node at the front of the list
- int front_delete() - delete the front of the list and return the value stored in it
- void insert_node(ListNode *prev, int) - inserts a node into the middle of the list. The new node will contain the integer as its data.
- 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 specific values.
- Public functions
-
- Default and copy constructors
- Destructor
- virtual void print_type() - prints "Linked List" to standard out
- void head_insert(int) - wraps around front_insert()
- void insert(ListNode *prev, int) - wraps around insert_node()
- ListNode *search_forward(int) - returns the first node that contains value, or NULL if value is not in the list
- void delete_first(int) - 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(int) - wraps around front_insert() to add value
- int 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
2. Test the stack class
0. Exit
==============================================
The submenus are defined as follows.
Linked List Menu
This menu will test the linked list class.
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
This menu will test the stack class.
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
---------------------------------