Professional Documents
Culture Documents
STACKS
In this lecture we will discuss one of the most common data structures found in the computer
algorithms; stacks
Stacks are special cases of the more general data structures than an ordered list.
A stack is an ordered list in which all insertions and deletions are made at one end, called the top. In
other words it is a data structure in which all access is restricted to the most recently inserted elements.
Stack is dynamically changing data type. How does it change? As we know that the ADTs are defined
together with the operations defined on them. The last item added to the stack is placed on the top and
is easily accessible. The items that have been in a stack for a while are more difficult to access. The
stack structure is more appropriate if we expect to access only to the item at the top; all other items are
inaccessible.
1. Stack Operations
In a stack the three natural operations are Insert, Delete and Find. These are renamed as Push
(insertAtFront), Pop (removeFromFront) and Top (peek). We can combine Pop and Top operations
and return the popped item in Top. We also need an operation to create the stack.
Pop A Push(D) D
A Top D Top
Top
B B B
C C C
We expect that each stack operation take constant amount of time, independent of the number of the
items in the stack. Analogy, in stack of newspapers finding today's paper takes fixed amount of time
no matter how deep the stack is.
1
CSE211 Lecture Notes – 2004/2005-II
What makes the stack useful is that there are many applications for which we need to access the most
recently inserted item to the list.
Algorithm:
1. Make an empty stack.
2. Read symbols until the end of the file.
3. If the token is an opening symbol, push it onto the stack.
4. If the token is a closing symbol and the stack is empty report error.
Otherwise, pop the stack.
5. If the symbol popped is not the corresponding opening symbol report an error
6. At the end of the file, if the stack is not empty report an error
Otherwise, the symbols are balanced.
What happens when a function is called? Three actions are taken in steps:
1. A copy of the arguments is made locally within the function. Any changes to them are made on the
local copy.
2. Storage is allocated for the local variables.
3. Control is transferred to the function. Before passing the control to the fuction return address in the
calling program must also be saved. This is an implicit argument passed to the function. When the
function is finished it retrieves the return address in its own data space and passes the control to it.
When a function returns three actions are performed.
1. Return address is stored in a safe location.
2
CSE211 Lecture Notes – 2004/2005-II
If we examine the memory while A3 is computing there will be an implicit stack which contains (r0,
r1, r2, r3) , where r0 is the address in O/S where main returns control.
r3
Q. Why shall we use a stack?
A. The returns are made in the reverse order of calls. r2
r1
Implementing Recursive Functions using Stacks r0
The use of stacks in implementing function calls becomes clerarer in this special case. If we assume
that the return address for each function is kept in hardware we have a problem implementing reentrant
code (recursive). Therefore, a separate stack of return addresses must be used and each time a
recursive function calls itself, a new data area for that call must be allocated containing all the
parameters and local variables.
Given a stack, item i, maximum size of the stack as n and top being the variable that holds the current
number of elements in the stack, performing PUSH(stack,i,n top) operation adds the item i to the top
of the stack . Similary, POP(stack,n,top) removes the top element and returns it in i. It is also possible
to return the top element as a function value. For example: pop operation can be impelmented in a
away that
3
CSE211 Lecture Notes – 2004/2005-II
The following procedures give the algorithms for these operations (considering that the stack is
implemented with an array of size n).
Additional to these methods implementing peek (returns the value at the top of the Stack without
removing it), getStackSize, isEmpty and isFull (if a static structure is used) methods are very useful.
The above definition tells us that the maximum size of a Stack type is given by the STACKSIZE
which is 100 in the given example. The identifier top indicates the top most item in the stack. For
example: if the value of s.top is 4 this means that there are five elements in the stack. s.items[0],
s.items[1], ...s.items[4]. When the stack is popped the value of the top is reduced to 3 which means 4
items remain in the stack.
The empty stack contains no elements and can be indicated by s.top= -1 .To initialize the stack we can
set the value of the top to -1.
We may write a function which returns if the given stack is empty or not.
4
CSE211 Lecture Notes – 2004/2005-II
#define true 1
#define false 0
int isEmpty(Stack *ps)
{
if (ps->top == -1) return(true);
else return(false);
};
#define SUCCESS 1
#define STACK_EMPTY -1
#define STACK_FULL -2
5
CSE211 Lecture Notes – 2004/2005-II
( ( (A/B)**C)+(D*E)-(A*C) )
We will see the relationship between an infix and postfix notation, we will also see how stacks can be
used to evaluate an RPN expression. Also to convert an infix to postfix by hand.
RPN is used in interpreters and compilers as an intermediate representational form for statements.
There is also prefix form (Forward Polish Notation) in which the operator precedes its operands.
Ex A*B/C infix
AB*C/ postfix
Translation by hand:
ABC*/DE*+AC*-
6
CSE211 Lecture Notes – 2004/2005-II
Examples:
Infix RPN
A A
A-B AB-
(A-B)+C AB-C+
A-(B+C) ABC+-
(A+B)*(C-D) AB+CD-*
If we relax the condition that expression be fully parenthesized. We need to use operator precedence
from left to right. (associative rule)
Associative Rule
7
CSE211 Lecture Notes – 2004/2005-II
Note that do not display or add “(“ or “)” to the rpn string.
Push left parenthesis onto the operator stack (create a false bottom). Apply the algorithm. When a right
parenthesis is reached the stack is emptied until the false bottom)
Assuming only binary operators, the algorithm for evaluating RPN expressions is:
Get a token until EOF
If token is a number (operand), push it into a stack
Else // it is an operator
Pop two numbers from the stack, otherwise exit with ERROR
Apply the operator
Push the result back onto the stack
Pop the result
If the stack is empty return the result, otherwise exit with ERROR
35+ 24 - * 6 *
9- 6 10- -96
-16
8
CSE211 Lecture Notes – 2004/2005-II
Each snapshot is taken just before the leftmost input character is examined.
In general,
1. If argument is an operand, stack it.
2. If argument is an n-ary operator, then the n arguments are already on the stack. Pop the n arguments
from the stack and replace by the value of the operator applied to the arguments.