CS 153 Data Structures I
Programming Assignment #5
Bag Class with Linked List
Due Date: September 21, 2000, during your teams scheduled time. Do not
show up late, do not show up early. If you would like to demo your program
before Thursday, you can do so by scheduling an appointment
with a TA.
Emphasis: Linked List, Underlying implementation can be changed with
minimal modification to the GUI.
Program Requirements:
-
In a nutshell, change your bag class so that it uses a linked list instead
of an array. The dialog class should not have to change much at all.
-
The insert function should allocate a new Node and insert it into
the linked list.
-
The remove function should unlink the first occurrence of the number from
the linked list.
The memory that is used to store that number should be deleted at
that time.
If there happens to be more than one occurence of that number, the remaining
numbers will not be removed.
-
The find function should look through the list to determine whether or
not the number exists.
-
The empty function will check to see if there are any Nodes in the list.
-
The full function will contain a different logic.
When using an array to hold the bag element full() compares the current
number of elements with the Max number of elements.
When using a dynamic list, being out of memory is the only limitation to
how many elements may be placed into the Bag.
You determine if there is enough memory for 1 more element by
-
attempting to allocate a new Node
-
if new is successful you know that enough memory exists so you delete
the Node just allocated and return(false)
-
if new is NOT successful return(true)
-
Your CBag class will also need a destructor. The general rule is,
"If any member function of the class does dynamic memory allocation, then
a destructor MUST be supplied that deletes that memory (gives back).
The destructor is called automatically, but you must write it.
The name of the destructor in this case is ~CBag(); (For
additional help with destructors)
The logic of ~CBag() will be detailed in class, however the logic for
a link list is very straight forward: Starting with the 1st element in
the list, delete it, move to the next element in the list and continue
deleteing until all Nodes have been returned to the operating system.
-
Hint: Use the debugger profusely!
-
Make sure that every time you call a function you meet the preconditions
for that function!
-
Make sure you have preconditions, postconditions and a one-line description
of your program with every function prototype. (in the header file!)
-
Make sure you comment variables which are not self explanatory.
-
Make sure you comment logical blocks of code.
-
Don't comment every line of your code.
-
Make sure you follow the departmental programming guidelines and the programming
requirements for this class.
Click here
to read the departmental programming guidelines.
Click
here to read programming requirements specific to this class.