CS 153 Data Structures I
Programming Assignment #5
Due: 2/15/01
This programming assignment is designed to:
-
build on assignment #4 by making the linked list a doubly linked list
-
introduce the concept of an iterator
-
create a completely general container that will be used as a base-class
in future assignments
In order to meet these goals you may:
-
use program #4 as a starting point
-
create a new folder named Project5
-
copy everything from your Project4 folder into the new folder
-
modify the existing functions to work with a doubly linked list
-
add several new functions to the CBag class (detailed below)
-
remove the CBag Display function from the class (detailed below)
Changing from a Forward list to a Forward/Backward list just adds functionality.
Instead of always needing to start at the Head of the list, you will now
be able to work from either end. The new functions below are just
exensions of what you have already done.
The 2nd significant change from project4 is more philosophical.
Previously, when you wanted to Display the content of the CBag container
you called a CBag::Display(...) member function. For reasons that
will be discussed in class this is normally considered an undesirable design.
What is preferred is called an iterator capability. An iterator
capability is made up of 3 CBag member functions and 1 CBag member
variable. Under this new arrangement, when you need to display the
content of the container you will:
mybag.Reset()
while( ! mybag.EOC() )
{
some-int = mybag.GetNext();
do-whatever-with-the-int;
//put the integer into a CListBox or . . .
}
Here is a suggested prototype for iterator capability
-
void CBag::Reset()
-
establish my current position at the beginning of the container
-
int CBag::GetNext()
-
return the current integer and advance to the next Node
-
bool CBag::EOC()
-
have we reached the end-of-the-container(bag)?
-
Node * m_current
-
points to the Node that is current
To upgrade to a doubly linked list, you will need to modify your
struct Node so that it has a prev pointer. Your next pointer will
work in the same manner as it did in project4, while the newly added prev
pointer will point to the Node which precedes it. You should modify
your existing functions to support this new pointer. Since struct
Node has only 1 function, this only involves
-
the Node() constructor should initialize prev
You will need to add 2 new pointers to the CBag class.
a Node * m_tail (comparible to m_head) which always points to the last
Node in the list
a Node * m_current described above. This pointer will be modified
by Reset() and GetNext() and will keep track of the current position in
the list.
You must remove the CBag::Display() function (the contents of the bag
can be retreived by successive calls to GetNext() ) and replace it will
a ...Dlg::Display(CBag whichBag) function. Call this function from
each of the OnButtonxxx functions that needs to display the container contents.
Since this ...Dlg::Display(...) function doesn't need to be called from
a click of a button, the easiest way to add it is to right click on the
...Dlg class and select AddNewMemberFunction. The wizard will add
the function prototype to the ...Dlg class and will build the
...Dlg::Display()
{ structure
in the ...Dlg.cpp
where you add the mybag.Reset()
and
the while ( ! mybag.EOC() ) loop
}
You will need add the following functions in your CBag class; these
are in addition to the functions you already have.
-
Rename Insert to InsertHead
-
It will continue to put a new node at the front of the list.
-
void Reset(); //Precondition: None
-
Will reset the current pointer to it's home position
-
int GetNext(); //Precondition: ! EOC()
-
Will return the next value in the list and advance the m_current position
-
void InsertTail(int);
-
Will put a new node at the end of the list
-
int RemoveHead(); //Precondition: ! isEmpty()
-
Will remove the first node and return the value stored there
-
int RemoveTail(); //Precondition: ! isEmpty()
-
Will remove the last node and return the value stored there
-
int GetHead(); //Precondition: ! isEmpty()
-
Will return the value stored in the first node and will not change the
list
-
int GetTail(); //Precondition: ! isEmpty()
-
Will return the value stored in the last node and will not change the list
-
bool EOC(); //Precondition: None
-
EOC means End of Container
-
Returns true if current is at the end of the list -
-
Returns false if current is not at the end of the list