CS 240
Lab 4

Purpose: 
Practical introduction to the List ADT and its implementation using an array-based data structure.

Reading:  Linked list concepts are discussed on pp. 287-294 in the text.  An array-based implementation of linked lists is discussed on pp. 303-308.

Motivation: 
The List ADT can be implemented using either static or dynamic arrays.  While either of these implementations may be adequate, they have their limitations.  A static array stores list elements in consecutive locations.  It has a fixed size, which may be too small or too large.  Although a dynamic array can be adjusted to any size, like a static array, it too stores list elements in consecutive locations.  Therefore, both require extensive copying of values in order to implement insert and delete List ADT operations.  Another data structure that can be used to implement the List ADT is a linked list.  Unlike static or dynamic arrays, linked lists permit insertions and deletions without the need for extensive copying because consecutive elements in the list are NOT stored in consecutive locations.  Rather, each element -- along with a reference to the location of the next element in the list -- is part of a structure called a node.  Think of it as the CS version of a Treasure Hunt!

Assignment:  Design, implement, and test an ordered array-based linked list (in an ordered list, all items are placed on the list by numeric or alphanumeric order)
  1. Using typedef statements: define type Element to be an int and type Link to be int
  2. Define constants NUMNODES (use a #define OR make it an int constant)  - set to 32, DEFAULT (type Link) - set to 0, INIT (type Element) set to 0, and NULLVAL (type Link) set to -1.
  3. Define a LinkedList class prototype that contains the following private data members:
  1. Write the prototypes for the following member functions:
  1. Write the prototype for the following non-member functions:
  1. Implement the constructor.  It must initialize numFree to the number of nodes in the nodes array, numInList to INIT, first to NULLVAL (because our list is empty to start), free to DEFAULT (so it 'points' to the first available node in the node array) and the next field of each node in the nodes array to 'point' to the next node in the nodes array.  The next field in the last position of the nodes array must be set to NULLVAL (because there are no available nodes after it).  See p. 306 for an illustration.
  2. Draw a picture depicting the nodes array after initialization along with first and free (see the pictures on pp. 305-308).
  3. Implement the getNumFree function that returns the value of numFree (i.e., the number of available nodes).
  4. Implement the getNumInList function that returns the value of numInList (i.e., the number of nodes on the list).
  5. Implement the isEmpty function.  It should return true when the value of first is equal to NULLVAL, and false otherwise.
  6. Implement the findPreviousNode function.  Given an element (i.e., to insert or delete), its purpose is to find the location of the node already on the list (if any) that should precede the given element in the list.  If there is none, the function should return NULLVAL.                      
  7. Implement the search function.  Given an element, its purpose is to find the location of the node containing the given element on the list, if any.  If there is none, the function should return NULLVAL.
  8. Implement the allocate function.  It should return the location of the first free node  (i.e., free), adjust the values of numFree and numInList, and update free to point to the new first free node.  (See bottom of p. 306).
  9. Implement the deallocate function.  Given the location of a node to delete, it should set the next field of the deleted node to free, adjust the values of numFreenumFree and numInList, and set free to the location of the deleted node.  (See bottom of p.307 and top of 308).
  10. Implement the isMember function.   Given an element, it should  return true if that element is a member of the list, and false otherwise.
  11. Implement the insert function.  Given an element, it should allocate a new node, set the data field of this node to the value of the element, then find and insert the node into the proper point in the list.
  12. Implement the remove function.  It should find and delete the node from the list (if it is in the list), and deallocate the node.
  13. Implement the display function.  It should display all the data members of the list.
  14. Implement the read function.  It should read in a series of elements into this list.
  15. Implement the overloaded output operator (Same pattern as we've seen).
  16. Implement the overloaded input operator (Same pattern as above, but with istream instead of ostream).
  1. Create a list called list1.
  2. Check to see if the list isEmpty.
  3. Insert the following values into the list:  20, 50, 30, 10, 70. 
  4. Check to see if the list isEmpty.
  5. Display the list.
  6. Call isMember to check if the following values are in the list:  10, 25, 50, 70.
  7. Remove the following values from the list:  30, 20, 70.  
  8. Check to see if the list isEmpty.
  9. Display the list.
  10. Add the following values to the list:  80, 0, 35.  
  11. Display the list.
  12. Remove the following values from the list:  40, 10. 
  13. Display the list.
  14. Remove the remaining values from the list.
  15. Check to see if the list isEmpty.
  16. Declare a new list, list2.
  17. Enter the values on the keyboard from step 3 above, and read them into list2.
  18. Display the list.
Submission:  FTP all source files to your bigblackbox FTP account.  Due dates and times as usual.

Last updated:  2/18/07 - RMW