CS 202 Fall 2013 > Notes for Tuesday, November 26, 2013 |
CS 202 Fall 2013
Notes for Tuesday, November 26, 2013
Stacks [18.1–18.2]
The Stack ADT
An abstract data type (ADT) is:
- A collection of data.
- A set of operations on that data.
Today we discuss the ADT Stack. A stack is a sequence that can be modified at one end, called the top. We usually think of a Stack vertically. A common metaphor is a stack of plates: we can put a new plate on the top, and we can remove the top plate, but we cannot do anything with any plate other than the top one.
Here is a more formal description of ADT Stack.
- Data
- A sequence of items (generally all of the same type). One end of the sequence is the top.
- Operations
- Push. Add a new item to the top.
- Pop. Remove the top item.
- Top. Look at the top item.
- Empty. Return whether there are any items.
Because the item we pop will always be the most recent one pushed, a Stack is also called a Last-In-First-Out (LIFO) structure.
Applications of Stacks
Program Execution
An important part of the execution of a program is the call stack. Each time a function is called, a stack frame for the function is pushed on the call stack. The stack frame contains parameters, other (non-static) local variables, and the function’s return address, which tells where to return to when the function finishes.
A function’s stack frame remains on the stack until it returns; at that point, the frame is popped. If function A calls function B, then the stack frame for function A remains on the stack, a frame for function B is pushed, and then popped when B returns, leaving the frame for function A on the top again.
Expression Evaluation
Suppose we are computing \((3+8)\times(4+9)\). We find that \(3+8=11\). Now we need to compute \(4+9\). We need a place to save that \(11\) while we perform this next computation. A Stack is the ideal structure for this. Push the \(11\). Compute \(4+9=13\). Then pop the \(11\) and compute \(11+13=24\).
In a more complex expression, we may have several values on the Stack at once. When we retrieve a saved value, the one on top will always be the one we need.
Shortly we will have more to say about Stacks and expression evaluation.
The STL Stack
The C++ Standard Library contains a Stack.
It is a class template called stack
,
defined in the standard header <stack>
.
This works much like vector
:
a template parameter specifies the type of each item in the Stack.
Member functions performing the four Stack operations are
push
,
pop
,
top
, and
empty
.
If the stack
is not const
,
then member function top
allows alteration of the
top value.
[C++]
stack<int> s; s.push(4); // Now top is 4 cout << s.top() << endl; // Prints 4 s.top() = 33; // Now top is 33
Note that member function pop
does not return a value.
Thus, looking at the top and removing it are separate operations.
[C++]
int t = s.top(); s.pop()
Reverse Polish Notation
Suppose we are given a list containing numbers (like 32
)
and binary arithmetic operators (like +
).
We have a Stack, which holds numbers.
We process the list item by item as follows.
- If an item is a number, then we push it on the Stack.
- If an item is an operator, then we pop the top two items from the Stack, perform the operation on them, and push the result.
Interpreting a list in this way gives us Reverse Polish Notation (RPN), or postfix notation. (This is a reversal of a notation invented a century ago by Polish mathematician Jan Łukasiewicz.) For example “\(5\;\;3\;\;{+}\)” is RPN for the expression we usually write as “\(5+3\)”. Our usual notation for arithmetic expressions is infix notation.
This gives us a way to write and evaluate arithmetic expressions.
Infix | RPN / Postfix |
---|---|
\(62\) | \(62\) |
\(5+3\) | \(5\;\;3\;\;{+}\) |
\(18-2\) | \(18\;\;2\;\;{-}\) |
\((5+3)*(18-2)\) | \(5\;\;3\;\;{+}\;\;18\;\;2\;\;{-}\;\;{*}\) |
\((2+3)*5\) | \(2\;\;3\;\;{+}\;\;5\;\;{*}\) |
\(2+(3*5)\) | \(2\;\;3\;\;5\;\;{*}\;\;{+}\) |
Note that with RPN we never need parentheses; precedence and associativity are irrelevant.
For today’s lab work, see the 11/26 Challenge.