You are on page 1of 23

@ McGraw-Hill Education

Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by
Tata McGraw Hill. This resource material is for Instructor's use only.

Stacks
(Chapter 4)

PROPRIETARY MATERIAL. 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.

Outline of Chapter 4
Introduction - definitions
Stack Operations (Push & Pop)
Stack Implementation (Array & Linked List)
Algorithms of Push and Pop operations
Applications
Recursive Programming
Evaluation of Expressions

ADT for stacks

1. Stack : Definition

What is Stack? Definition

A stack is an ordered list with the


restriction that elements are added
or deleted from only one end of the
list termed top of stack.

The other end of the list which lies


inactive is termed bottom of stack.

The stack data structure obeys the


principle of Last In First Out (LIFO).

In other words, elements inserted or


added into the stack join last and
those that joined last are the first to
be removed.

Top of stack
Bottom of
stack

Stack Operations
The two operations which stack data structure
supports
(i) Insertion or addition of elements known as Push.
(ii) Deletion or removal of elements known as Pop.

Top of stack
Bottom of
stack

Why use Stack?


Certain problems & applications
need stack mechanism:
(i) Simulation of people entering
& leaving a lift in a building.
(ii) Heap memory management
in OS.
(iii) Managing data in recursion
(iv) Managing arithmetic
expressions
We shall see some examples
later.

Enters

Leaves

2. Stack Implementation

Stack Implementation
A common and a basic method of
implementing stacks is to make
use of another fundamental data
structure viz., arrays.
While arrays are sequential data
structures the other alternative
of
employing
linked
data
structures
have
been
successfully
attempted
and
applied.
An array based implementation of
stacks
is
fairly
convenient
considering the fact that stacks
are uni-dimensional ordered lists
and so are arrays which despite
their multi-dimensional structure
are inherently associated with a
one-dimensional consecutive set

// In Java
Int top=-1; // current
location,
top=-1: stack
empty.
int[] Stack = new int[10];
Stack[++top] = item; //
push
Item = Stack[top--]; // pop

Top of stack
Bottom of
stack

Stack : Algorithm of PUSH


Algorithm 4.1 Implementation of push
operation on a stack
// Stack is the array
// n=max size; top=current location;
// item=data to be pushed
procedure PUSH(STACK, n, top, item)
if (top = n) then STACK_FULL;
else
{ top = top + 1;
STACK[top] = item; /* store
item as top element of STACK */
}
end PUSH

10

Stack : Algorithm of POP


Algorithm 4.2 : Implementation of
pop operation on a stack
// STACK is the array
// top=current location;
// item=data retrieved
procedure POP(STACK, top, item)
if (top = -1) then STACK_EMPTY;
else
{ item = STACK[top];
top = top - 1; }
end POP

11

3. Examples of Stack
Applications

12

Example#1: Stack Application


Recursive programming

During the execution of a recursive program, to keep track


of the calls made to itself and to record the status of the
parameters at the time of the call, a stack data structure is
used

Tail recursion or Tail end recursion is a special case of


recursion where a recursive call to the function turns out to
be the last action in the calling function. Note that the
recursive call needs to be the last executed statement in
the function and not necessarily the last statement in the
function.

13

Example: Stack Application


Recursive programming (cont)

In a stack implementation of a recursive call, all


the local variables of the function that are to be
remembered, are pushed into the stack when
the call is made.

Upon termination of the recursive call, the local


variables are popped out and restored to their
previous values.

Now for tail recursion, since the recursive call


turns out to be the last executed statement, there
is no need that the local variables must be pushed
into a stack for them to be remembered and
restored on termination of the recursive call.

14

Example: Data movement in


Stack for the recursive gcf(4,6)
// call gcf(4,6)
// a = 4; b = 6; answer = 2
public static int gcf (int a, int b)
{
if (a == b) // if a equals b
return a;

PUSH
gcf (4, 6)
1 call, PUSH
st

PUSH
gcf(4, 2)
gcf (4, 6)
a<b, PUSH

if (a > b)
return gcf( a - b, b);

PUSH
gcf(2, 2)

POP
return 2

else // if a < b
return gcf(a, b - a);

gcf(4, 2)
gcf (4, 6)

gcf(2, 2)
gcf(4, 2)
gcf (4, 6)

}
a>b, PUSH

a=b, POP

15

Example#2: Stack Application


Evaluation of Expressions
Infix expression

<Operand> <Operator> <Operand>

An aritmetic expression:

a bc d

Postfix expression <Operand> <Operand> <Operator>

abc d
Prefix expression
<Operand>

<Operator> <Operand>

a * bcd

16

Scenario: Given abc*+d-, we get ((b*c)+a)-d


Algorithm 4.3 : Procedure to evaluate a post fix expression E.
Input: Expression E
Output: Original Expression
Procedure EVAL_POSTFIX(E)
x = get_next_character (E);
/* get the next character of expression E */
case x of
:x is an operand:
Push x into stack S;
:x is an operator:

:x = $:
end case
end EVAL-POSTFIX.

Pop out required number of


operands from the stack S,
evaluate the operator and push
the result into the stack S;
Pop out the result from stack S;

17

Exercise: Tracing the Stack


operations to evaluate Postfix
Expressions
Postfix expression
<Operator>

3
2

<Operand> <Operand>

237 5
7
3
2

2
1
2

18

Exercise: Can we use Stack to


evaluate Prefix Expressions? Try
it!
Prefix expression <Operator> <Operand> <Operand>

a * bcd

19

4. ADT of Stack

20

ADT for Stack


Data objects:
A finite set of elements of the same type
Operations:
Create an empty stack and initialize top of stack
CREATE(STACK)
Check if stack is empty
CHK_STACK_EMPTY(STACK) (Boolean function)
Check if stack is full
CHK_STACK_FULL(STACK) (Boolean function)
Push ITEM into stack STACK
PUSH(STACK, ITEM)
Pop element from stack STACK and output the element
popped in ITEM
POP(STACK, ITEM)

21

Exercises

22

#1 Write a Java Interface & Java program to implement the Stack


ADT.
#2 Write a Java program, using Stack in converting a decimal
number into a binary number
The logic for transforming a decimal number into a binary number is as
follows:
Read a number
Iteration (while number is greater than zero)
Find out the remainder after dividing the number by 2
Print the remainder
Divide the number by 2
End the iteration
However, there is a problem with this logic. Suppose the number, whose
binary form we want to find is 23. Using this logic, we get the result as
11101, instead of getting 10111. To solve this problem, we use a stack. [6]
We make use of the LIFO property of the stack. Initially we push the
binary digit formed into the stack, instead of printing it directly. After the
entire number has been converted into the binary form, we pop one digit
at a time from the stack and print it. Therefore we get the decimal
number converted into its proper binary form.

@ McGraw-Hill Education

23

Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by
Tata McGraw Hill. This resource material is for Instructor's use only.

End of
Stacks
(Chapter 4)

PROPRIETARY MATERIAL. 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.

You might also like