CS 153 Data Structures I
Program #7 - Expand Doubly Linked List
Due Date: October 5, 2000, during your teams scheduled time. If
you would Like to demo your program before Thursday, you can do so by scheduling
an appointment with a TA.
Emphasis:
-
A full compliment of member functions for a Doubly Linked List
-
An ITERATOR for the List class container
Discussion: The LIST object that you created for #6 was 'stateless'
meaning that each time you called Insert or Remove or Find the object did
not remember anything about what had transpired. This assignment
will add a state variable to the object which will be called pCurrent
here. pCurrent will be of type Node * (the address of a Node or NULL).
pCurrent specifies where in the list of Nodes you currently are looking.
Most
of the member functions use or modify pCurrent. Be very careful with
pCurrent. If it is allowed to contain #$%^&*@ your otherwise
correct functions will 'blow up'.
Program Requirements: This linked list will be a modified
version of your previous List.
REMOVE the friend declaration from the Class List. ...Dlg must
not be a friend in this assignment. The DisplayList function that
needed the friend(ship) will use the Iterator functions instead.
How to do this is explained at the bottom.
Place enough buttons on your GUI to test all of the functionality.
The following are the required functions (in addition to the member
functions already required in #6):
-
bool InsertHead(const char * arg);
-
make the new Node containing arg the 1st Node in the list
-
set pCurrent to this new Node
-
you may want to just rename the Insert function from #6
-
bool InsertTail(const char * arg);
-
make the new Node containing arg the last Node in the list
-
set pCurrent to this new Node
-
bool InsertBefore(const char * arg);
-
make the new Node containing arg the previous of the Node pointed
to by pCurrent
-
if pCurrent points to NULL or the 1st Node then the new Node would become
the 1st Node in the list
-
set pCurrent to this new Node
-
bool InsertAfter(const char * arg);
-
make the new Node containing arg the next of the Node pointed to
by pCurrent
-
if pCurrent points to NULL then the new Node would become the 1st Node
in the list
-
if pCurrent points to last Node then the new Node would become the last
Node in the list
-
set pCurrent to this new Node
-
CString RemoveHead();
-
remove the 1st Node in the list
-
set pCurrent to NULL
-
set Head to the next Node
-
delete the removed Node
-
return the Node's data as a CString
-
CString RemoveTail();
-
set pCurrent to NULL
-
set Tail to the previous Node
-
delete the removed Node
-
return the Node's data as a CString
-
CString RemoveCurrent();
-
set pCurrent to NULL
-
update Head or Tail or both if pCurrent pointed to the 1st or last Node
-
delete the removed Node
-
return the Node's data as a CString
-
bool Find(const char * arg);
-
search for a Node's data that strcmp() matches arg STARTING FROM THE pCurrent
POSITION; i.e. if your list contained 3 different Nodes that held the data
"TESTING" then the following sequence should find() each of the 3 in turn:
-
Reset() - position pCurrent to start from the beginning
-
Find("TESTING"); - returns true, the 1st instance was found
-
Find("TESTING"); - returns true, the 2nd instance was found
-
Find("TESTING"); - returns true, the 3rd instance was found
-
Find("TESTING"); - returns false
-
CString GetHead();
-
return the data of the 1st Node
-
set pCurrent to point to the 1st Node
-
CString GetTail();
-
return the data of the last Node
-
set pCurrent to point to the last Node
-
CString GetCurrent();
-
return the data of the Node pointed to by pCurrent
-
pCurrent remains unchanged
-
void Reset(); - set pCurrent to the beginning of the list. This
does not necessarily mean that pCurrent should point to the 1st Node.
It may mean that pCurrent should be made NULL. Before you begin programming
you must decide the following questions. What should be the value
of pCurrent when -
-
the list is empty
-
the list contains 1 item and Reset() has been called
-
the list contains 1 item and Reset()/GetNext() has been called 1 time
-
the list contains n items and n items have been returned by GetNext
-
EOL() is supposed to return true
-
CString GetNext();
-
return the successor (Next) of the Node last returned by GetNext() or the
1st Node if Reset() has last been called
-
bool EOL();
-
return false if it would be legal to call GetNext() again
-
else return true
Example of how DisplayList() might be programmed using the List Iterator
member functions making unnecessary the undesirable 'friend' capability.
void DisplayList()
{
// The CListBox is named m_DisplayBox
CString data;
m_DisplayBox.ResetContent();
L.Reset();
while( L.EOL() != true )
{
data = L.GetNext();
m_DisplayBox.AddString(data);
}
}
Make sure that every time you call a function you meet the preconditions
for that function!
Make sure you have preconditions, post conditions 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.