0% found this document useful (0 votes)
24 views

Week 8 Stack

The document discusses stacks and their implementation. It defines what a stack is, examples of real-life applications of stacks, and how stacks are represented and used in computer science. It also covers evaluating arithmetic expressions using stacks and the rules for transforming infix notation to postfix notation.

Uploaded by

Mannam J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Week 8 Stack

The document discusses stacks and their implementation. It defines what a stack is, examples of real-life applications of stacks, and how stacks are represented and used in computer science. It also covers evaluating arithmetic expressions using stacks and the rules for transforming infix notation to postfix notation.

Uploaded by

Mannam J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CLO-1

Week#08
Stack and Its Implementation

Course: Data Structures and Algorithm (CS-215)


Course Teacher: Dr. Umme Laila

Contact Info:
Room No: 112
Email: [email protected]

1
"stack n.
The set of things a person has to do in the future. "I haven't done it
yet because every time I pop my stack something new gets
pushed." If you are interrupted several times in the middle of a
conversation, "My stack overflowed" means "I forget what we
were talking about."

-The Hacker's Dictionary

Stacks
Friedrich L. Bauer
German computer scientist
who proposed "stack method
of expression evaluation"
in 1955.
2
Introduction to Stack
• A stack is a linear structure in which items may be added
and removed only on one end.

• It is an ordered-list in which all the insertion and deletions


are made at one end to maintain the Last-in-First-out
(LIFO) order.

• This means, that the last item is to be added to a stack is


the first item to be removed.

• Although stack may seem to be very restrict type of data


structure, it has many important applications in computer
science

3
Real-life Application of Stack
•Three everyday examples of such a structure

 Stack of dishes
 Stack of pennies
 Stack of folded towels

4
Application of Stack in computer Science
•Stack is used in

 Recursion
 Expression Evaluation
 String Comparison
 Function calling
 Memory manipulation to store address
 Tree manipulation
 Etc.

5
Problems that Use Stacks

CS 307

Computer Science
Fundamentals of
• The runtime stack used by a process (running program) to
keep track of methods in progress
• Search problems
• Undo, redo, back, forward

Stacks
6
Stack Representation
• Two basic operations associated with stacks

a) “PUSH” is the term used to insert an element into a stack


b) “POP” is the term used to delete an element from a stack

• The following diagram depicts a stack and its operations −

7
Stack Representation
• Example 6.1
• Suppose the following 6 elements are pushed, in order, onto an
empty stack:
AAA, BBB, CCC, DDD, EEE, FFF
 There are three ways of picturing such a stack

 The AVAIL list was implemented as a stack. 8


Postponed Decisions
• Stacks are frequently used to indicate the order of the processing of
data when certain steps of the processing must be postponed until
other conditions are fulfilled.
• Suppose we have three procedures in a project A, B, C
1. While processing procedure A we are required to move on to
procedure B, whose completion is required in order to complete
procedure A.
2. While processing B we are led to the procedure C , we place
process B on A and begin process C.
3. Similarly, while processing C we need D and after processing it we
continue with C and upon completion remove C from stack and so
on.

9
Array Representation of Stack
• Stack may be represented in computer in various ways, by means of a
one-way list or a linear array.
• Each of the stacks are maintained by:
 STACK = A linear array named
 TOP = A pointer variable that contains the location of the top
element of the stack
 MAXSTK = A variable that gives maximum number of elements
that can be held by the stack

 The condition TOP = 0 or TOP =NULL indicates STACK is empty


 In Figure, Stack has 3 elements => since TOP =3
 There is room for 5 more items since MAXSTK =8

10
Operations on STACK
• The operation of adding into and removing an item from a STACK
can be implemented by using following procedures
 PUSH (Adding into STACK)
 POP (Removing from STACK)

PRECONDITIONS:
• To execute the procedure PUSH, the precondition is to check whether
there is a room in STACK if not OVERFLOW
TOP = MAXSTK then OVERFLOW

• To execute the procedure POP, the precondition is to check whether


there is an element in the STACK if not UNDERFLOW
TOP = 0 or NULL then UNDERFLOW

11
Algorithms for PUSH and POP

• TOP and MAXSTK are global variables, the procedure may use only
PUSH(STACK, ITEM) and POP(STACK, ITEM)

• Note: the value of TOP is changed before insertion in PUSH and the value of TOP
is changed after deletion in POP
12
PUSH and POP in STACK
Example 6.2

13
Minimizing Overflow
• Essential difference between underflow and overflow in dealing stacks

• UNDERFLOW: Depends relatively upon the given algorithm and given


input data and hence there is no direct control by the programmer.

• OVERFLOW: Depends upon the arbitrary choice of the programmer for


memory space reserved for each stack, this choice does influence the number
of items overflow may occur.

• The number of elements in stack fluctuates as elements are added or removed.


How to deal it?
• Initially reserving a great deal of space for each stack will decrease the
number of OVERFLOW may occur.
• Whereas reserving a small amount of memory space may increase the number
of time OVERFLOW occurs.
• Adding space to the stack for resolving OVERFLOW may be more expensive
than the space saved

14
Minimizing Overflow

15
Arithmetic Expressions
• Let Q be an arithmetic expression involving constants and
operations.
• Binary operations in Q may have different level of precedence.
• We assume the following three levels of precedence for the
usual five binary operations:

Highest: Exponentiation (↑)


Next Highest: Multiplication (*) and Division (/)
Lowest: Addition (+) and subtraction (-)

• We also assume that in any parathesis free expression, the


operations on the same level are performed from left to right
(this is not a standard, some languages perform
exponentiation from right to left)

• Stack is an essential tool in finding the value of Q

16
Arithmetic Expressions

17
Arithmetic Expressions: Notations
Infix Notation
• For most of the arithmetic operations, the operator symbol is
placed between its two operands.
For example:
A+B C-D E*F (G/H) +A

•With notation
(A+B)* C and A+(B*C)

•We must distinguish by using either parenthesis or some


operator-precedence convention.

Polish Notation (Prefix Notation)

•The notation in which the operator symbol is placed before its


two operands

+AB -CD *EF /(GH) +A=+/GHA

18
Arithmetic Expressions: Notations
INFIX PREFIX
(A+B)*C [+AB]*C =*+ABC
A+(B*C) A+[*BC]=+A*BC
(A+B)/(C-D) [+AB]/[-CD]= /+AB-CD

•The fundamental property of POLISH (PREFIX) NOTATION is that the order


in which the operations are performed in completely determined by the positions
of the operations and operands in the expression.

•One never needs parenthesis when resulting expressions in this notation.


SOLVE: (A+B)/C*D-E

•Reverse Polish Notation (POSTFIX or SUFIX)

•Refersto the analogous notation in which the operator symbol is placed after its
two operands
AB+ CD- EF* GHA/+

•The computer usually evaluates an arithmetic expression written in infix notation


into steps:
•First converts the expression to postfix notation and
•Evaluates the postfix expression
19
(A+B)/C*D-E

[+AB]/C*D-E
/[+AB]C*D-E
*/[+AB]CD-E
-*/+ABCDE

20
Evaluation of Postfix Expression using Stack

21
Evaluation of Postfix Expression using Stack

22
Evaluation of Postfix Expression using Stack
Symbol STACK
5
6
2
+
*
12
4
/
-
)

23
Evaluation of Postfix Expression using Stack

24
Rules for transforming from Infix to Postfix

• Rule#01: No same precedence operators can stay together (side by side)


in a stack
 If low high its ok (e.g. -*)
 If high low then POP (e.g. * -)

• Rule#02: If same precedence operator are in the form +(- then its ok.
• Rule#03: if operator in a parenthesis occur then POP the operator (+) or
(*)
• Rule#04: if both operators in a parenthesis e.g. (-*) then POP using
LIFO.

25
Transforming Infix into Postfix Expression
(A+B/C*(D+E)-F)
Symbol Stack Postfix
(
A
+
B
/
C
*
(
D
+
E
)
-
F
)

26
Transforming Infix into Postfix Expressions

27
Transforming Infix into Postfix Expressions

28
Transforming Infix into Postfix Expressions

29

You might also like