Homework 2 - Linked Lists
Due: Wednesday September 26, 2007 at 5pm
This assignment focuses on creating and using a generic LinkedList template
class. You will have to define the member functions of a LinkedList. The
file hw2_list.cpp contains the "header" information for
both the ListNode and LinkedList class.
Update Sep 20: The header file had to be altered. The "delete" keyword
is reserved by C++. So the "delete" operation for the linked list has now
become the "remove" operation.
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. Requires that the element class has an equality
operator defined.
- The output operator will output the element. 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 should involve allocating new nodes and copying the element of
the source 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.
- 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
delete remove functions. The
delete remove that takes a node pointer will
delete that node from the list. The delete remove that
takes an element will search the list for that element and delete it if
found.
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.