Apache C++ Standard Library User's Guide

## 10.2 The stack Data Abstraction

As a data abstraction, a stack is traditionally defined as any object that implements the operations defined in Table 16:

#### Table 16: Stack operations

Function | Implemented operation
`empty()` | Returns true if the collection is empty
`size()` | Returns number of elements in collection
`top()` | Returns (but does not remove) the topmost element in the stack
`push(newElement)` | Pushes a new element onto the stack
`pop()` | Removes (but does not return) the topmost element from the stack

Note that removing the element at the top of the stack does not return a value. If the value of the element is important, you must retrieve the element before you remove the element.

### 10.2.1 Include Files

Programs that use the stack data abstraction should include the `stack` header:

    #include <stack>

### 10.2.2 Declaration and Initialization of stack

A stack can use either a vector or deque as the underlying container. When deciding which to use, consider the data which is to be stored. If the number of elements will vary widely, a deque may decrease the total amount of memory used when the stack has few elements. A vector has less overhead, so may be more efficient in cases where the stack will tend to remain at approximately the same size. However, increasing the memory allocated to a vector is relatively slow, so a vector may be inefficient if the size of the container will tend to increase. These are, of course, generalized statements which may not apply to the problem at hand. In cases where efficiency of either speed or memory is important, you should try both underlying containers and compare the results.

The following are sample declarations for a stack:

    std::stack<int> stackOne;                     // stack using deque
    std::stack<double, std::deque<double> > stackTwo;
    std::stack<Part*, std::list<Part*> > stackThree;
    std::stack<Customer, std::list<Customer> > stackFour;

The last example creates a stack from a user-defined type named Customer.

### 10.2.3 Example Program: An RPN Calculator

A classic application of a stack is in the implementation of this calculator.

> NOTE -- The executable version of this program is in the file calc.cpp.

Input to the calculator consists of a standard string that represents an expression written in reverse polish notation (RPN). Operands, called integer constants, are pushed onto a stack of values. As operators are encountered, the appropriate number of operands are popped off the stack, the operation is performed, and the result is pushed back onto the stack.

We can divide the development of our stack simulation into two parts, a calculator engine and a calculator program. A calculator engine is concerned with the actual work involved in the simulation, but does not perform any input or output operations. The name is intended to suggest an analogy to a car engine or a computer processor: the mechanism performs the actual work, but the user of the mechanism does not normally directly interact with it. Wrapped around this is the calculator program, which interacts with the user and passes appropriate instructions to the calculator engine.

We can use the following class definition for our calculator engine. Inside the class declaration we define an enumerated list of values to represent each of the possible operators that the calculator is prepared to accept. We have made two simplifying assumptions: all operands will be integer values, and only binary operators will be handled.

    class CalculatorEngine {
    public:
      enum binaryOperator { PLUS, MINUS, TIMES, DIVIDE };
      int  currentMemory () {
        return data.top ();
      }
      void pushOperand (int value) {
        data.push (value);
      }
      void doOperator (binaryOperator);
    protected:
      std::stack< int, std::vector<int, std::allocator<int> > > data;
    };

The member function `doOperator()` performs the actual work. It pops values from the stack, performs the operation, then pushes the result back onto the stack:

    void CalculatorEngine::doOperator (binaryOperator theOp) {
      int right = data.top ();
      data.pop ();
      int left = data.top ();
      data.pop ();
      switch (theOp) {
        case PLUS:   data.push (left + right); break;
        case MINUS:  data.push (left - right); break;
        case TIMES:  data.push (left * right); break;
        case DIVIDE: data.push (left / right); break;
      }
    }

(This code is simplified for demonstration purposes. A more robust program would check to see if the stack were empty before attempting to `pop()` elements from it.)

The main program reads values in reverse polish notation, invoking the calculator engine to do the actual work:

    int main () {

      std::cout << "Calculator example program, from Chapter 8"
                << std::endl;

      std::cout << "Enter a legal RPN expression, "
                << "end with p q (print and quit)"
                << std::endl;

      char c;
      int intval;
      CalculatorEngine calc;

      while (std::cin >> c)
        switch (c) {
          case '0': case '1': case '2': case '3': case '4':
          case '5': case '6': case '7': case '8': case '9':
            std::cin.putback (c);
            std::cin >> intval;
            calc.pushOperand (intval);
            break;
          case '+':
            calc.doOperator (CalculatorEngine::PLUS); break;
          case '-':
            calc.doOperator (CalculatorEngine::MINUS); break;
          case '*':
            calc.doOperator (CalculatorEngine::TIMES); break;
          case '/':
            calc.doOperator (CalculatorEngine::DIVIDE); break;
          case 'p':
            std::cout << calc.currentMemory () << std::endl;
          case 'q':
            std::cout << "End calculator program" << std::endl;
            return 0; // quit program
        }

      return 0;
    }