#include <stack.h>
Inheritance diagram for MarkedStack:
Public Member Functions | |
MarkedStack () | |
MarkedStack constructor. | |
~MarkedStack () | |
MarkedStack destructor. The items on the stack are not deleted. | |
void | Push (ListItem *Item) |
Pushes Item onto the stack. | |
ListItem * | Pop (void) |
Removes the item from the top of the stack, and returns it. | |
BOOL | Mark () |
Marks the current Top of the stack so that we can easily return to the same state by calling Release(). | |
void | Release () |
Pops and then deletes items from the stack until we find the first mark. If there are no marks then all items on the stack are removed and deleted. | |
void | DeleteAll () |
Deletes all marks and elements in the MarkedStack. | |
Private Attributes | |
Stack | MarkStack |
It has a Mark() method which marks the current top of stack. Mark() can be called more than once to insert a number of marks into the stack.
It has a Release() method which restores the state of the stack to the position of the last mark.
eg.
MarkedStack MS();
MS->Push(a) MS->Mark() MS->Push(b) MS->Push(c) MS->Mark() MS->Push(d)
The stack looks like this
d <- Tail c <- Mark b a <- Mark
If we now perform MS->Release() the stack will look like this
c b a <- Mark
If we perform MS->Release() a second time, the stack will look like this
a
Definition at line 210 of file stack.h.
|
MarkedStack constructor.
Definition at line 274 of file stack.cpp.
|
|
MarkedStack destructor. The items on the stack are not deleted.
Definition at line 293 of file stack.cpp.
|
|
Deletes all marks and elements in the MarkedStack.
Reimplemented from List. Definition at line 431 of file stack.cpp. 00432 { 00433 ListItem* TopOfStack; 00434 TopOfStack = GetTail(); 00435 while (TopOfStack != NULL) // loop while the stack is not empty 00436 { 00437 Release(); // Deletes mark and all items before the mark 00438 TopOfStack = GetTail(); 00439 } 00440 // Make sure there are no marks left 00441 ENSURE(MarkStack.Pop() == NULL, "All marks have not been deleted from the MarkedStack"); 00442 }
|
|
Marks the current Top of the stack so that we can easily return to the same state by calling Release().
Definition at line 357 of file stack.cpp. 00358 { 00359 // Firstly create a mark 00360 ListItemPtrItem* pMark = new ListItemPtrItem(); 00361 // Return with an error if we could not create the mark 00362 if (pMark == NULL) 00363 return FALSE; 00364 pMark->pListItem = GetTail(); // This will be NULL if the list is empty 00365 00366 // Push the mark onto the MarkStack 00367 MarkStack.Push(pMark); 00368 return TRUE; // Success 00369 }
|
|
Removes the item from the top of the stack, and returns it.
Definition at line 333 of file stack.cpp. 00334 { 00335 return (RemoveTail()); 00336 }
|
|
Pushes Item onto the stack.
Definition at line 313 of file stack.cpp. 00314 { 00315 AddTail(Item); // Add the item to the top of the stack 00316 }
|
|
Pops and then deletes items from the stack until we find the first mark. If there are no marks then all items on the stack are removed and deleted.
Definition at line 388 of file stack.cpp. 00389 { 00390 // Find the mark to stop at 00391 ListItemPtrItem* MarkRec = (ListItemPtrItem*)MarkStack.Pop(); 00392 ListItem* Mark; 00393 if (MarkRec == NULL) // There are no marks to find 00394 { 00395 Mark = NULL; 00396 } else 00397 { 00398 Mark = MarkRec->pListItem; 00399 } 00400 ListItem* TopOfStack = GetTail(); 00401 ListItem* AsGoodAsDead; 00402 00403 while (TopOfStack != Mark) // Loop until we find Mark 00404 { 00405 ENSURE(TopOfStack != NULL, "A Mark could not be found in the Marked stack"); 00406 TopOfStack = GetPrev(TopOfStack); 00407 AsGoodAsDead = RemoveTail(); 00408 delete AsGoodAsDead; 00409 } 00410 if (MarkRec != NULL) 00411 { 00412 delete (MarkRec); // Restored State to that pointed to by Mark, so delete it 00413 } 00414 }
|
|
|