CS 212
Lab 5
Purpose: Practical
introduction to the List ADT and its
implementation using an array-based data structure.
Reading: Linked
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:
- Draw a picture depicting the
nodes array after initialization
along with first and
free
.
- Implement the getNumFree
function that returns the value of numFree (i.e., the
number of available nodes).
- Implement the getNumInList
function that returns the value of numInList (i.e., the
number of nodes on the list).
- Implement the isEmpty
function. It should return true
( >1) when the value of first is equal to NULLVAL, and false (0) otherwise.
- 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.
- 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.
- 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
- 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. .
- Implement the isMember
function. Given an element, it should
return true
if that element is a member of the list, and false otherwise.
- 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.
- Implement the remove function. It
should find and delete the node from the list (if it is in the list), and deallocate the node.
- 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:
- Create a list called list1.
- Check to see if the list isEmpty.
- Insert the following values
into the list: 20, 50, 30, 10, 70.
- Check to see if the list isEmpty.
- Display the list.
- Call isMember to check if the following
values are in the list: 10, 25, 50, 70.
- Remove the following values
from the list: 30, 20, 70.
- Check to see if the list isEmpty.
- Display the list.
- Add the following values to
the list: 80, 0, 35.
- Display the list.
- Remove the following values
from the list: 40, 10.
- Display the list.
- Remove the remaining values
from the list.
- Check to see if the list isEmpty.
- Declare a new list, list2.
- Enter the values on the
keyboard from step 3 above, and read them into list2.
- 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