CS120
Assignment #12
Due on or before Friday April 18 at 11:59 PM.
For this assignment we will continue to work with
linked lists. Linked lists are the fundamental
dynamic data structure. Dynamic means a data
structure that can change size (add or remove data)
while the program is running. In contrast arrays are
a static data structure - once created, the size of an
array can't change.
The code for a simple linked list is here. This code creates a
linked list with three data elements in it. There is
a pointer called "head" that points to the
beginning of the list.
The node class in the text is similar to this code and reviewing
it may be of assistance in completing the assignment.
Make the following changes to the program:
- Add at least two additional data elements to the
node class. That is, currently a node only stores
a name and a pointer to the next node in the list. Make
it so that each node can store at least two more pieces
of data. One piece of data should be an integer called
id. The other one(s) is up to you.
- Modify the setdata() function in the class so that
the programmer can set all of the data elements of a
node object. (If you want you can create separate set
functions for each type of data: setname(), setID(), etc.
or have one setdata() function that takes several
arguments).
- Modify the print() function in the node class
so that it prints all of the data in a node, not just the
name.
- Add a function called append() to the node class that
takes a pointer to a node as an argument and appends the new
node to the tail end of a linked list. This is not the same
thing as adding an item to the head of the list.
- Add a function called length() to the node class that
calculates and returns the length of the linked list, which is the
number of nodes in the list.
- Add a function called clear() to the node class that
calls the delete operation on every node in a linked list
to release the memory that was being used for that node. Hint: if
you begin by deleting the first node, you won't have any
way to get to the next node, so you will need to do something else.
- Add a function called find() to the node class that takes an
integer as an argument and searches the list for a node with the id field
that is equal to the argument that is passed in. This function should
return a pointer to the node that contains the id equal to the argument, or
NULL if no such node exists in the list.
Write a main() function that demonstrates how each of these
functions work.
Turn in: A copy of your program code and sample
output demonstrating that it works. All items should be sumbitted
via Canvas.