Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Parenth.Frd/Stack.cpp

Go to the documentation of this file.
00001 // Stack.cpp - Stack Class using single-link Implementation 
00002 #include "Stack.hpp"
00003 
00004 Stack::Stack() {                        // Default Constructor
00005    top  = NULL;
00006    size = 0;
00007 }
00008 
00009 Stack::Stack( DBase* item ) {           // First item Constructor
00010     top  = new Link( item, NULL );
00011     size = 1;
00012 }
00013 
00014 Stack::Stack( const Stack& stk ) {      // Copy Constructor
00015     Link* fromCurr = stk.top;
00016     Link* toCurr   = new Link( *fromCurr );
00017 
00018    top = toCurr;                       // Setting the top pointer
00019     size = 1;
00020 
00021     while ( fromCurr->next != NULL ) {  // Is it the end of the from Stack ?
00022 
00023         fromCurr = fromCurr->next;      // Move forward in the reading stack
00024 
00025         toCurr->next = new Link( *fromCurr ); // Copy the next Link content
00026 
00027         toCurr = toCurr->next;          // Move forward in the writing stack
00028         size++;                         // We just added a new Link
00029     }
00030 
00031     if ( size != stk.size ) {
00032         cout << "An error occured while copying the Stack to a new Stack\n";
00033     }
00034 }
00035 
00036 void Stack::push( DBase* item ) {   // Insert an item at the top
00037     top = new Link( item, top );
00038     size++;
00039 }
00040 
00041 DBase* Stack::pop() {               // Remove an item from the top
00042 
00043    if ( isEmpty() )
00044       return NULL;
00045 
00046     DBase* dtemp = top->element;
00047     Link* ltemp = top->next;
00048 
00049     delete top;
00050     top = ltemp;
00051     size--;
00052 
00053     return dtemp;
00054 }
00055 
00056 void Stack::clear() {               // Clear the stack
00057       if ( isEmpty() )           // If it's empty then get out.
00058       return;
00059 
00060    while( top != NULL ) {
00061         Link* temp = top;
00062         top = top->next;
00063         delete temp;
00064         size--;
00065     }
00066 
00067     assert( top == NULL );
00068 }
00069 
00070 DBase& Stack::topValue() const {    // What's the top Value ?
00071     assert( !isEmpty() );
00072     return *top->element;
00073 }
00074 
00075 void Stack::display() const {
00076     Link* curr = top;
00077 
00078    if ( isEmpty() )           // If it's empty then get out.
00079       return;
00080 
00081    while ( curr->next != NULL ) {  // Is it the end of the from Stack ?
00082 
00083       curr->element->display();
00084         curr = curr->next;       // Move forward in the reading stack
00085     }
00086 
00087 }
00088 

Generated on Sun Oct 14 18:46:18 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001