Homework 2 - Linked Lists
Due: Wednesday April 14, 2010 at 5:00pm
The purpose of this assignment is to create a linked list using template
classes. This will be accomplished by creating a ListNode and a LinkedList
class. The file hw2_list.cpp
contains the "header" information for both the ListNode and LinkedList class.
Your task for this assignment is to code the bodies of the functions for both
classes. We will discuss what each function is supposed to do in class. You
may add additional helper functions to the private section of the provided
classes, but do not add additional public functions.
ListNode class
You will define the member functions to do the following:
- The default constructor will set next to NULL
- The constructor that takes just the element will set the element to the
given value and set next to NULL
- The constructor that takes an element and pointer will use those values
to set the element and next pointer.
- setData will set the element to the given value
- getData will return the element
- setNext will set the next pointer to the given value
- getNext will return the next pointer
- The equality operator will return true if the right element is equal
to the stored element. (Note: This requires that the element class has an
equality operator defined)
- The output operator will output the element. (Note: this requires that the
element class has the output operator defined)
LinkedList class
You will define the member functions to do the following:
- The find_previous helper function traverses the list to find out what
node comes before the given node. Return NULL if the given node is the
head of the list.
- The default constructor will create the empty list by setting head to NULL
- The destructor will traverse the list and deallocate each node
- The copy constructor will traverse the given list and create a copy of
it. This will involve allocating new nodes and copying the element of
the source node into the new node. The next pointers will not be copied.
Instead, next should be set like you would for a tail insert.
- The assignment operator copies a list like the copy constructor does.
Make sure to deallocate the old list before copying the new list over.
Also check for assigning to itself (e.g. a=a) and return out of the
function when that occurs.
- empty() checks if the list is empty.
- The search function traverses the list and sees if the element exists.
It will return the node if the element is found or NULL if not found.
- The insert function inserts the node into the list (as discussed in the
book and in lecture).
- There are two remove functions. The remove that takes a node pointer will
delete that node from the list. The remove that takes an element will
search the list for that element and delete it if found.
- The output operator will traverse the list and print out each node using
the output operator defined for the ListNode class.
Menu Program
The menu program will test your implementation of the linked list. In main,
you will define a linked list that stores characters. You will then operate
on that list according to the following menu:
Welcome to the Linked List Menu
=======================================
1. Add a character to the list
2. Search for a character in the list
3. Delete a character from the list
4. Print the list
5. Copy the list and print the copy
0. Exit
=======================================
The delete and print options (options 3 and 4) should first check if the list
is empty. If the list is empty, it should print that the list is empty.
The copy option should create a copy of an empty list, but will also print
that the copied list is empty if the source list is empty.
Submit your completed code as an attachment in an email.