CS 240
Lab 5
Purpose:
Practical
introduction to the List
ADT and its linked list implementation using a
pointer-based data structure.
Reading: Linked list
concepts are discussed
on pp. 287-294 in the text. A
pointer based implementation of linked lists is discussed on pp.
295-300.
Motivation: Last week we simulated a memory storage pool
from which we allocated nodes and to which we returned deallocated
nodes (similar to what happens when we use the new and delete
commands). This storage pool was managed using a variation of a
simple linked list called a linked stack.
We used the nodes we allocated to populate an ordinary linked list (our
"working list"). We made it possible to create a list, check to
see if our list was empty, allocate a node from the storage pool,
insert a node to our list, search for a node on our list, and remove a
node from our list and deallocate it. We basically did all the
things needed to maintain data using a simple linked list structure,
except using the data stored for something useful! This may seem
strange, but remember that the point of this class is to learn a
variety of data or storage structures and the algorithms to manage
them. For now, we will leave the practical application of the
data structures we create to our project assignments.
In addition to learning about linked lists, managing your storage pool
should have given you some insigth into what goes on behind the scenes
whenever memory is dynamically allocated or deallocated.
This week we will build an ordered
pointer-based linked
list. Since we will be dynamically
allocating memory using new
and deallocating it using delete,
you will
only have to manage your working list.
Assignment:
Design,
implement, and test an ordered
pointer-based
linked list
- There are both similarities and differences between pointer-based
and array-based LinkedList
implementations. Most of the constants
used in
an array-based implementation are unnecessary in a pointer-based
implementation. Note especially that the NULLVAL
constant is no longer equal to -1,
and it cannot be declared and initialized
as usual. Rather, it must be set equal to 0 (i.e., the null pointer
value) via a #define
preprocesser statement (i.e., #define NULLVAL 0).
- Since allocation is handled via the new
operation, and deallocation is
handled via the delete
operation, separate allocate
and deallocate
functions are unnecessary, and neither is the Link
called free.
- Following the pattern in the text, the pointer-based LinkedList class should contain a
private section that contains the definition of a nested Node class.
Node
has public data members data ( of
type Element)
and next
(of type *
Node).
Note the recursive
defiinition of the Node class
(See p. 296).
- The nested Node class
will also contain a public Node
constructor.
- As in the array-based implementation, a typedef
statement outside the class
will define type Element
(renaming int).
Unlike
the array-based implementation, a typedef
statement within the LinkedList class will define a new
type Link
that is a
pointer to a Node.
Since it is defined inside the class, its use in headers outside the
class (i.e., your .cpp) will have to be qualified with LinkedList:: (i.e., LinkedList::Link ).
- The data members of the LinkedList
class shall be first (type
Link)
and size
(type int). Instead
of accessors getNumFree
and getNumTaken, implement the
accessor getSize.
- Adapt and implement the remaining functions (as described in the
array-based version) for a pointer-based LinkedList class.
Although the syntax when employing dynamic memory allocation will be
different (ptr = ptr->next; instead of ptr =
node[ptr].next;), the logic will be very practically the same.
- Unlike the array-based implementation, the pointer-based linked
list will require a destructor,
copy constructor,
and overloaded assignment
operator in order to work properly.
- With the help of the guidelines above, and the information in
your text on pp. 295-300, code and test a pointer-based linked
list. You should appropriately implement the client program
described in last week's lab to
test this LinkedList class as
well.
Submission: FTP all source (including READM) files to your FTP
account by the due date for your section.
Last updated: 2/26/07 - RMW & RVS