0% found this document useful (0 votes)
104 views25 pages

Stacks - Infix To Postfix

Here are the steps to solve these expressions: 1) Convert the infix expressions to postfix 2) Evaluate the postfix expression using a stack This allows the expressions to be evaluated in a straightforward manner without needing to deal with operator precedence issues.

Uploaded by

Rajan singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views25 pages

Stacks - Infix To Postfix

Here are the steps to solve these expressions: 1) Convert the infix expressions to postfix 2) Evaluate the postfix expression using a stack This allows the expressions to be evaluated in a straightforward manner without needing to deal with operator precedence issues.

Uploaded by

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

STAC 1

K
MODULE 3
AGENDA
2
• Basic Definition of a Stack
• Array Implementation of a Stack
• Basic Operations – Push(), POP(), IsEmpty()
• Applications
• Balancing parenthesis
• Recursive function Calls
• Expression Evaluation
• Infix, Prefix and Postfix Notations
• Infix to Prefix and Postfix conversion
• Postfix Evaluation
What is a Stack ? 3
BASIC DEFINITION
4

• A stack is a list of elements in which an element may be inserted or


deleted only at one end, called the top of the stack.
• Stacks are sometimes known as LIFO (last in, first out) lists.
• As the items can be added or removed only from the top i.e. the last
item to be added to a stack is the first item to be removed.
• A stack can be implemented by means of Array, Structure, Pointer, and
Linked List.
• Stack can either be a fixed size one or it may have a sense of dynamic
resizing.
BASIC OPERATIONS
5
• Stack operations may involve initializing the stack, using it and then de-
initializing it.
• Apart from these basic stuffs, a stack is used for the following two
primary operations −
• push() − Pushing (storing) an element on the stack.
• pop() − Removing (accessing) an element from the stack.
• To use a stack efficiently, need to check the status of stack as well.
• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty
6
PUSH OPERATION 7

• Push Operation The process of putting a new data element onto stack is
known as a Push Operation.
• Push operation involves a series of steps
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
8
35

Algorithm

begin procedure push: stack, data

if stack is full
top 35 return null
endif
top 36
36

31
31 top ← top + 1
32 stack[top] ← data
32
34
34 end procedure
POP OPERATION
• 9
Accessing the content while removing it from the stack, is known as a Pop Operation.
• In an array implementation of pop() operation, the data element is not actually
removed, instead top is decremented to a lower position in the stack to point to the
next value.
• But in linked-list implementation, pop() actually removes data element and deallocates
memory space.
• A Pop operation may involve the following steps
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
35 10

Algorithm

begin procedure pop: stack


top
if stack is empty
35
top
return null
36 36 endif
31 31
data ← stack[top]
32 32 top ← top – 1
34 34 return data

35 35 end procedure
STACK APPLICATIONS
11
• Balancing parenthesis
• Recursive function Calls
• Expression Evaluation
• Reversing a list
• Conversion of an infix expression into a postfix expression
• Evaluation of a postfix expression
• Towers of Hanoi
BALANCING PARENTHESIS 12

• takes a expression as input and checks if the expression is


correctly parenthesized.
• Algorithm
• Take a expression as input and store it in the array.
• Check for the “(” and “)” in the expression.
• If “(” encounters, then push it to the separate array.
• If “)” encounters, then pop the element of the array.
• If the number of “(” and “)” are equal, then the expression is
correctly parenthesized. Otherwise it is not.
13
Stack

Expression { (a + b) * (a – b) }
Algorithm
14

•Initialize a character stack. Set top pointer of stack to -1.


•Find length of input string using strlen function and store it in an integer
variable "length".

•Using a for loop, traverse input string from index 0 to length-1.


• If current character is '{', then push it inside stack.
• If current character is '}', then pop a character from stack. If stack is empty,
then input string is invalid, it means there is no matching opening
parenthesis corresponding to closing parenthesis.

•After complete traversal of input string, If stack is empty then input expression
is a Valid expression otherwise Invalid.
EXPRESSION EVALUATION
15
• Representation of algebraic expression
• Infix notation
• Prefix or polish notation
• Postfix or reverse polish notation

• Expression A/B+C  ((A/B) + C )


• Prefix  +/ABC
• Postfix  ABC/+
ARITHMETIC EXPRESSION EVALUATION

16
Convert the given (infix) expression to postfix , then evaluate the postfix expression
using stack.
• Infix to Postfix Conversion
• Read the infix expression one character at a time until it encounters the delimiter #
• If the character is an operand place it on the output
• If the character is an operator push it into the stack. If the stack operator has a
higher or equal priority than input operator then pop that operator from the stack
and place it onto the output.
• If the character is a left parenthesis, push it on to the stack
• If the character is the right parenthesis pop all the operators from the stack till it
encounters left parenthesis discard both the parenthesis in the output.
Infix Expression : A*B+(C–D/E)#
OUTPUT 17

A A

* A
*

B
AB
*

STACK
18
EVALUATING POSTFIX NOTATION 19

• Read the postfix expression one character at a time until


it encounters the delimiter #
• If the character is an operand push its associated value
onto the stack
• If the character is an operator pop two values from the
stack, apply the operator to them and push the result
onto the stack.
• Expression is  A B*CDE/- # 20
• The values are A = 4, B=5 , C = 5, D= 8, E = 2
TOWERS OF HANOI 21
 Tower of Hanoi is a mathematical puzzle where we have
three rods and n disks. The objective of the puzzle is to
move the entire stack to another rod, obeying the
following simple rules: 
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one
of the stacks and placing it on top of another stack i.e.
a disk can only be moved if it is the uppermost disk on
a stack.
3. No disk may be placed on top of a smaller disk.
 Step 1 : Shift first disk from 'A' to 'B'. Step
22
 2 : Shift second disk from 'A' to 'C'. Step
 3 : Shift first disk from 'B' to 'C'.

 The pattern here is :


 Shift 'n-1' disks from 'A' to 'B'.
 Shift last disk from 'A' to 'C'.
 Shift 'n-1' disks from 'B' to 'C'.
23
START 24
Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF

END Procedure
STOP
FOR PRACTICE 25
1. A – (B / C + (D % E * F) / G)* H)

2. (A – B / C) * (A / K – L)

3. ((A – (B + C)) * D) / (E + F)

4. A + B * C + (D * E + F) * G

5. A + (B * C – (D / E + F) * G) * H

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy