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
1. Stack : Definition
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
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
10
11
3. Examples of Stack
Applications
12
13
14
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
An aritmetic expression:
a bc d
abc d
Prefix expression
<Operand>
<Operator> <Operand>
a * bcd
16
:x = $:
end case
end EVAL-POSTFIX.
17
3
2
<Operand> <Operand>
237 5
7
3
2
2
1
2
18
a * bcd
19
4. ADT of Stack
20
21
Exercises
22
@ 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.