3.1 Stacks
3.1 Stacks
representations,
Operations on stack (traversal, push and
pop)
Arithmetic expressions: polish notation,
evaluation and transformation of expressions.
Introduction to Stacks
Consider a card game with a discard pile
Discards always placed on the top of the pile
Players may retrieve a card only from the top
What
What other
other
examples
examples can can you
you
think
think of
of that
that are
are
modeled
modeled by by aa stack?
stack?
We seek a way to represent and
manipulate this in a computer program
This is a stack
2
A stack is a last-in-first-out (LIFO) data structure
Adding an itemReferred to as pushing it onto the
stack
Removing an item
Referred to as popping it from the stack
A stack is a list of elements in which an element may
be inserted or deleted only at one end, called TOP of
the stack.
Also called LIFO, piles, push-down lists.
2 basic operations:
Push
Pop
3
Stack
Definition:
An ordered collection of data items
Can be accessed at only one end (the top)
Operations:
construct a stack (usually empty)
check if it is empty
Push: add an element to the top
Top: retrieve the top element
Pop: remove the top element
4
Applications of stack:
items[1] B
items[0] A
Insert an item A
A new item (A) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Top=0
Insert an item B
A new item (B) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1] B Top=1
items[0] A
Insert an item C
A new item (C) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2] C Top=2
items[1] B
items[0] A
Insert an item D
A new item (D) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3] D Top=3
items[2] C
items[1] B
items[0] A
Array Representation of Stacks
We represent a stack in memory using a one-
way list or a linear array.
Array representation:
Array : STACK.
A pointer variable TOP.
A variable MAXSTK.
The condition, TOP = NULL, indicates stack
is empty.
The condition, TOP = MAXSTK, indicates the
stack is full.
Insert Operation(Array)
PUSH(STACK, N, TOP, ITEM):to insert element in
stack array STACK having N elements where TOP
represent index of last element in the array.
1.If TOP=N,then:
write :OVERFLOW, and Exit.
2. Set TOP := TOP + 1.
3. Set STACK[TOP]:= ITEM.
4. Exit.
Delete D
an item (D) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C Top=2
items[1] B
items[0] A
Delete C
an item (C) is deleted from the Top of the stack.
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B Top=1
items[0] A
Delete B
an item (B) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B
items[0] A Top=0
Delete A
an item (A) is deleted from the Top of the stack.
items[MAX-1]
. .
items[4]
items[3] D
items[2] C
items[1] B
items[0] A Top=-1
Delete Operation(Array)
POP(STACK, N, TOP, ITEM)
1.If TOP = NULL ,then :
write: UNDERFLOW, and Return.
2. Set ITEM := STACK [TOP].
3. Set TOP := TOP - 1.
4. Return.
Linked List Representation of
Stacks
We also call it linked stack, implemented using a
singly linked list.
The INFO fields of the nodes hold the elements of
the stack and the LINK fields hold the pointers to
the neighboring elements in the stack.
The START pointer acts as the TOP of the stack.
The node containing NULL pointer denotes the
BOTTOM of the stack.
PUSH and POP is done from START only.
No need of MAXSTK.
Insert Operation(LL)
PUSH(INFO, LINK, TOP, AVAIL, ITEM)
1.If AVAIL = NULL, then :
write :OVERFLOW, and Exit.
2.Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3.Set INFO[NEW]:= ITEM
4.Set LINK[NEW] := TOP
5.Set TOP := NEW
6.Exit
Delete Operation(LL)
POP(INFO, LINK, TOP, AVAIL, ITEM)
1.If TOP = NULL, then :
write UNDERFLOW, and Exit.
2.Set Set ITEM :=INFO[TOP]
3.Set TEMP:==TOPa nd TOP :=LINK[TOP]
4.Set LINK[TEMP] := AVAIL and Set AVAIL :=
TEMP
5.Exit
Example
Consider following stack of characters, where
MAXSTK=8.
STACK= A, C, D, G, K,_, _, _. Demonstrate
following operations in stack:
POP(STACK, ITEM).
POP(STACK, ITEM).
PUSH(STACK, L).
PUSH(STACK, P).
Is there any chance that C would get deleted
before D?
Minimizing Overflow
Programmer does not have any direct control
over underflow, because it depends totally
upon algorithm and input data.
But overflow depends on the memory space
reserved for each stack.
The space reserved also influences the
number of times the overflow occurs.
The choice of space reserved involves time
space tradeoff.
Allocating large number of space to a stack
will reduce the number of times the
overflow occurs, but it could result in
wastage of memory space if complete
memory is seldom used.
On the other hand, reserving a small
amount of memory may result in increased
number of overflows, and would increase
the time to resolve the overflow.
One solution is to use one single array
whenever we need 2 arrays. But it would
need some modification in the PUSH and
POP algorithms.
Arithmetic Expressions: Polish
Notations
This section gives an algorithm which
finds the value of an arithmetic expression
using reverse Polish(postfix) notation.
It is another application of STACK.
Assumptions:
Precedence
Exponent
Multiplication and division
Addition and subtraction
All expressions are evaluated from left to right.
Eg: 2^3 +5*2^2-12/6
26
Postfix and Prefix Examples
INFIX POSTFIX PREFIX
A+B A B + + A B
A*B+C A B * C + + * A B C
A * (B + C) A B C + * * A + B C
A - (B - (C - D))A B C D--- -A-B-C D
A-B-C-D A B-C-D- ---A B C D
Prefix
Prefix :: Operators
Operators come
come
before
before the
the operands
operands
27
How computer evaluates an arithmetic
expression????
In 2 steps:
1. It converts the expression into postfix notation.
2. Then it evaluates the postfix expression.
Example:
2 7 3 54 6+ -5 -6 *- - *
2 32 47 +5 56 6- -- -* *
2 7 -1 - *
2 7 -1 - * 2 8 * 2 8 * 16
29
Evaluating RPN Expressions
P is an arithmetic expression in Postfix Notation.
2. Scan P from left to right and Repeat Step 3 and 4 for each element of P
until the sentinel “)” is encountered.
31
By hand: "Fully parenthesize-move-erase"
method:
1. Fully parenthesize the expression.
2. Replace each right parenthesis by the
corresponding operator.
3. Erase all left parentheses.
A *Examples:
B + C ((A * B) + C) A * (B + C) (A * (B + C) )
((A B * C + (A (B C + *
A B * C + A B C + *
32
Stack Algorithm
POLISH (Q, P)
1. PUSH “(” on to STACK and add “)” to the end of Q.
2. Scan Q from left to right and Repeat steps 3 to 6 for each element of Q until
the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then:
(a) Repeatedly POP from STACK and add to P each
operator (On the TOP of STACK) which has the same precedence as or
higher precedence than @.
(b) Add @ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly POP from STACK and add to P each operator (On
the TOP of STACK.) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Don’t add the left parenthesis to
P.]
[End of If Structure.]
[End of step 2 Loop.]
7. Exit. 33
By hand: "Fully parenthesize-move-erase" method:
1. Fully parenthesize the expression.
2. Replace each left parenthesis by the
corresponding operator.
3. Erase all right parentheses.
Examples:
A * B + C ((A * B) + C) A * (B + C) (A * (B + C))
+ * A B) C) * A + B C ))
+ * A B C * A + B C
34
Thank You