CS 212
Lab
5


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


ReadingLinked list concepts are discussed in chapter 4 of the text.

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. 

Assignment: 

Given the files : LinkList.h , LinkList.c and LinkListTest.c

Finish writing the implementations for all of the functions defined in the LnkList.h header file such that:

  1. Draw a picture depicting the nodes array after initialization along with first and free .
  2. Implement the getNumFree function that returns the value of numFree (i.e., the number of available nodes).
  3. Implement the getNumInList function that returns the value of numInList (i.e., the number of nodes on the list).
  4. Implement the isEmpty function.  It should return true ( >1) when the value of first is equal to NULLVAL, and false (0) otherwise.
  5. 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.                      
  6. 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.
  7. 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.  (remember free is just an int (i.e. an index) , not a pointer
  8. 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.  .
  9. Implement the isMember function.   Given an element, it should  return true if that element is a member of the list, and false otherwise.
  10. 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.
  11. Implement the remove function.  It should find and delete the node from the list (if it is in the list), and deallocate the node.
  12. Implement the read function.  It should read in a series of elements into this list.
  • Modify LinkListTest.c  client program that tests your program by performing the following:
  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.
  • Draw a picture depicting the lists and their nodes array (along with first and free) for each of the above steps.

Submission:  FTP all source files to your bigblackbox FTP account.  Due dates and times as usual.

Last updated:  022/24/2008 - RVS