Homework 2 - Linked Lists

Due: Wednesday, April 13, 2011 at 5:00pm

Coding Conventions

Use the same coding conventions given in Homework 1.

Assignment

The purpose of this assignment is to create a linked list to store integers using procedural programming methods. This will be accomplished by creating a ListNode structure and a set of functions to implement list operations.

The ListNode structure will contain a single 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). To keep track of the list, you will have a special ListNode pointer variable in main() called head, which will contain the address of the first node in the list. The head pointer will be initially NULL (until the first insertion function is called).

The majority of your list functions will be passed the head pointer and will operate on the list from that starting point. Some functions may operate differently. We will discuss the pseudocode for the functions in class on Wednesday and Friday.

You will define the following global functions to implement the list:

You may add additional functions to support the above functions if you wish, but you must have the above functions implemented to receive full credit on the assignment.

Your main function will implement the following menu:

      Welcome to the Linked List Menu
  =======================================
  1.  Search for an element in the list
  2.  Add an element to the list
  3.  Delete an element from the list
  4.  Print the list

  0.  Exit
  =======================================
If the user selects Options 1, 3, or 4 before adding data to the list (when the head is NULL), 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 NULL for the "prev" pointer). If the user selects Option 2 after successfully searching for an element with Option 1, the new element will be inserted immediately after the found element (pass the found pointer as "prev"). Otherwise, do a head insertion.

Option 1 will prompt the user for an element and read it in. It will then call listSearch() 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() and update the head pointer with the returned address from listInsert().

Option 3 will prompt the user for an element and read it in. It will then call deleteList() and update the head pointer with the returned address from deleteList().

Option 4 will call listPrint().

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 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.