The ListNode structure will contain a single element, the address of the node containing the previous element and the address of the node containing the next element. The element in this assignment will be an integer (but it could be a double, char, or any other datatype if we wanted it to be). Since this is a circular list, the next and previous pointers will always be set by the insertion and deletion functions to point to some other node in the list.
As with Homework 2, you will have a special ListNode pointer variable in
main() called head
. Unlike Homework 2, the node pointed to by
head
will be a dummy head node. This is a special ListNode that
contains no values. It serves as the sentinel for the beginning and end of
the list when using a circular list. The dummy head node will NEVER change in
this list variant. You will need to initialize the dummy
head node with the listInitialize()
function in main() before
entering the menu loop. The dummy head node is NOT deleted until you deallocate
the entire list.
The majority of your list functions will have the same name as the functions
in Homework 2. However, they may operate differently since we now have a
circular, doubly linked list. There are also three new functions in this
assignment: listInitialize()
, listReverseSearch()
,
and listReversePrint()
. We will discuss these variations in
class on Wednesday and Friday.
class on Wednesday and Friday.
You will modify the following functions from Homework 2 to support the list variant features.
Your main function will implement the following menu. Make sure to call
listInitialize()
BEFORE entering the menu loop:
Welcome to the Linked List Menu =============================================== 1. Search forwards for an element in the list 2. Add an element to the list 3. Delete an element from the list 4. Print the list 5. Search backwards for an element in the list 6. Print the list in reverse 0. Exit ===============================================If the user selects Options 1, 3, 4, 5 or 6 before adding data to the list (when the dummy head node points to itself), tell them the list is empty and return to the selection prompt for the menu.
If the user selects Option 2 while the list is empty, do a head insertion (pass the dummy head node for the "prev" pointer). If the user selects Option 2 after successfully searching for an element with Option 1 or Option 5, the new element will be inserted immediately after the found element (pass the found pointer as "prev"). Otherwise, do a head insertion.
Options 1 and 5 will prompt the user for an element and read it in. It will then call the appropriate search function and store the returned address in a ListNode pointer (to be used by Option 2 as described above). If the pointer is NULL, print out that the element was not found. Otherwise, print out that the element was found at that address.
Option 2 will behave as described above. It will prompt the user for a new
value and read that in. It will then call listInsert()
. In this
variant, the head node never changes. Also, if you want to do a head insert,
you should call listInsert(head, head, element)
since that will
insert after the dummy head node.
Option 3 will prompt the user for an element and read it in. It will then call
deleteList()
. The head node does not change during deletion, since
the dummy head node remains the same
Option 4 will call listPrint()
and Option 6 will call
listReversePrint()
.
Option 0 (exit) will call listDeallocate()
, set the head pointer
to NULL and then exit the menu loop.
Each option that reads in an integer should use C++ robust input (check for cin.fail() after using cin >>) to detect when the user accidently types in a character instead of an integer. No other input checks are need. A list by default can store any value for its datatype.
Submit your completed code as an attachment in an email to melissa in Pine on Sleipnir.