c_stack

Table Of Contents

Introduction, c_stack

The stack object implements the c_collection interface, as a LIFO (last in, first out) type queue. The stack is one way, meaning that you can only pull elements off of the top, and you can only add elements onto the top. The standard concepts of push, and pop are implemented for the stack. The add() member function from the c_collection iterface is implemented as a synonym for push().

API Refrence
  • c_stack(void)

    Default constructor. Creates an empty list.

  • c_stack( c_stack& that )

    Copy constructor, creates a copy of the that list.

  • ~c_stack(void)

    Destructor, removes all elements from the list, freeing resource used by the elements. ~T() is called during this process for each element in the list.

  • virtual int get_count( void ) cosnt virtual int add( const T&elm )
    virtual int remove_all( void )
    virtual c_collection_iter<T>* prepare_iter( const c_collection_sorting_order ord = raw_order ) const
    virtual int first( c_collection_iter<T>*, T& ) const
    virtual int next( c_collection_iter<T>*, T& ) const
    virtual int prev( c_collection_iter<T>*, T& ) const
    virtual int last( c_collection_iter<T>*, T& ) const

    These functions are the implementation of the c_collection interface. Please keep in mind that even though last(), and prev() are implemented, they will be extremely inefficent, as the stack is only traversable in a forward fasion. If you need efficent traversal in both directions, you should probably be using a c_list instead.

  • virtual const c_stack<T>& operator=( cosnt c_stack& right )

    This is the assingment operator for a c_stack object. It makes the lvalue a copy of the rvalue. Any existing elements in the lvalue stack are removed before copying the rvalue.

  • virtual void push( const T& elm )
    virtual void pop( T& elm )

    These act like the cannonical push and pop stack functions.

  • virtual int get_at( int pos, T& elm ) const

    get_at() sets elm to the elemnt at position pos. Please note that this will be inefficient for the stack object, due to the fact that the c_stack object can only be traversed in a forward direction. If you need to perform this type of operatios, you should probably be using a c_array instead.

  • virtual int find_index( const T& ) const

    This function returns the integer index (capable of being used in a call to get_at() ) for the argument. Please note that this is inefficent, as the stack object can only be traversed in a forward direction. Also keep in mind that this function is the reason that int operator==( const T& ) needs to be implemented for T.

Example Code:
void pathalogical_liar( void )
{
    c_stack<int> primes;

    primes.push(4);
    primes.push(6);
    primes.push(8);
    primes.push(9);
    primes.push(10);
    primes.push(12);
    primes.push(14);
    primes.push(15);
    primes.push(16);
    primes.push(18);
    primes.push(20);

    int prime;
    while( primes.pop(prime) ) {
        cout << "prime number: " << prime << endl;
    }
}

Up