C++

Tuesday, 9 October 2012

Stacks- An introduction:

 What is a stack?
  nIt is an ordered group of homogeneous items of elements.
  nElements are added to and removed from the top of the stack (the most recently added items are at the top of the stack).
  nThe last element to be added is the first to be removed (LIFO: Last In, First Out).
The Stack ADT
         nA stack is a list with the restriction
nthat insertions and deletions can only be performed at the top of the list
nThe other end is called bottom
nFundamental/Primitive operations:
nPush: Equivalent to an insert
nPop: Deletes the most recently inserted element
nEmpty: Examines whether the stack is emp
 Abstract Data Type Stack:
template <class KeyType>
class Stack
{   // objects: A finite ordered list with zero or more elements
  public:
        Stack (int MaxStackSize = DefaultSize);
        ~Stack();
        // Create an empty stack whose maximum size is MaxStackSize
        bool IsFull();
        // if number of elements in the stack is equal to the maximum size
        // of the stack, return TRUE(1) else return FALSE(0)
        bool IsEmpty();
      // if number of elements in the stack is 0, return TRUE(1) else return FALSE(0)
        KeyType Top();
        // Return top element of stack
       
       void Push(const KeyType& item);
        // if IsFull(), then StackFull(); else insert item into the top of the stack.
        
        KeyType Pop( );
        // if IsEmpty(), then StackEmpty() and return 0;
        // else remove the top element of the stack.
};

No comments:

Post a Comment