SlideShare a Scribd company logo
Applications of Stack
The following Applications of stack are:
1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processorโ€™s stack.
5. During a function call the return address and arguments are pushed onto a stack and on return they
are popped off.
Expression evaluation and conversion
โ€ข The most frequent application of stacks is in the evaluation of arithmetic
expressions. An arithmetic expression is made of operands, operators.
โ€ข Operand is the quantity on which a mathematical operation is performed. Operand
may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol
which signifies a mathematical or logical operation between the operands.
Examples of familiar operators include +, -, *, /, ^ etc.
โ€ข When high-level programming languages came into existence, one of the major
difficulties faced by computer scientists was to generate machine language
instructions that could properly evaluate any arithmetic expression. A complex
assignment statement such as
Might have several meanings, and even if the meanings were uniquely defied, it is
still difficult to generate a correct and reasonable instruction sequence. The fist
problem in understanding the meaning of an expression is to decide the order in
which the operations are to be carried out. This demands that every language must
uniquely define such an order.
Another example:
To calculate this expression for values 4, 3, 7 for A, B, C respectively we must
follow certain in order to have the right result :
โ€ข The answer is not correct; multiplication is to be done before the addition, because
multiplication has higher precedence over addition. This means that an expression
is calculated according to the operatorโ€™s precedence not the order as they look like.
โ€ข Thus expression A + B * C can be interpreted as A + (B * C). Using this
alternative method we can convey to the computer that multiplication has higher
precedence over addition.
โ€ข To fix the order of evaluation, assign each operator a priority, and evaluated from
left to right.
โ€ข Let us consider the expression
โ€ข By using priorities rules, the expression X is rewritten as
โ€ข We manually evaluate the innermost expression first, followed by the next
parenthesized inner expression, which produces the result.
โ€ข Another application of stack is calculation of postfix expression. There are
basically three types of notation for an expression (mathematical expression; An
expression is defined as the number of operands or data items combined with
several operators.)
1. Infix notation
2. Prefix notation
3. Postfix notation
โ€ข Infix
The infix notation is what we come across in our general mathematics, where the
operator is written in-between the operands. For example: The expression to add
two numbers A and B is written in infix notation as:
โ€ข Prefix
The prefix notation is a notation in which the operator(s) is written before the
operands. The same expression when written in prefix notation looks like:
โ€ข Postfix
In the postfix notation the operator(s) are written after the operands, so it
is called the postfix notation (post means after), it is also known as suffix
notation. The above expression if written in postfix expression looks like:
A + B * C Infix Form
A + (B * C) Parenthesized expression
A + (B C *) Convert the multiplication
A (B C *) + Convert the addition
A B C * + Postfix form
The rules to be remembered during infix to postfix or prefix conversion are:
1. Parenthesize the expression starting from left to right.
2. During parenthesizing the expression, the operands associated with operator having higher
precedence are first parenthesized. For example in the above expression B * C is parenthesized
first before A + B.
3. The sub-expression (part of expression), which has been converted into postfix, is to be treated as
single operand.
4. Once the expression is converted to postfix form, remove the parenthesis.
Exercise
A * B + C / D
(A * B) + (C / D) Parenthesized expression
(A B*) + (C / D) Convert the multiplication
(A B*) + (C D/) Convert the division
(A B*) (C D/) + Convert the addition
A B*C D/ + Postfix form
Converting infix to prefix expression
Example:
A * B + C / D Infix Form
(A * B) + (C/D) Parenthesized expression
(*A B) + (C/D) Convert Multiplication
(*A B) + (/CD ) Convert Division
+(*A B) (/CD ) Convert addition
+*A B /CD Prefix form
Note: the precedence rules for converting an expression from infix to prefix is
identical to postfix. The only change from postfix conversion is that the operator is
placed before the operands rather than after them.
Conversion from infix to postfix (another method):
Procedure to convert from infix expression to postfix expression is as follows:
1. Scan the infix expression from left to right.
2. a) If the scanned symbol is left parenthesis, push it onto the stack.
b) If the scanned symbol is an operand, then place directly in the postfix expression
(output).
c) If the symbol scanned is a right parenthesis, then go on popping all the items from the
stack and place them in the postfix expression till we get the matching left parenthesis.
d) If the scanned symbol is an operator, then go on removing all the operators from the
stack and place them in the postfix expression, if and only if the precedence of the
operator which is on the top of the stack is greater than (or greater than or equal) to the
precedence of the scanned operator and push the scanned operator onto the stack
otherwise, push the scanned operator onto the stack.
The three important features of postfix expression are:
1. The operands maintain the same order as in the equivalent infix expression.
2. The parentheses are not needed to designate the expression unambiguously.
3. While evaluating the postfix expression the priority of the operators is no longer relevant.
Example: Convert the following infix expression to the equivalent postfix notation.
(30+23)*(43-21)/(84+7)
Need for prefix and postfix expression
The evaluation of an infix expression using a computer needs proper code
generation by the compiler without any ambiguity and is difficult because of various
aspects such as the operatorโ€™s priority . This problem can be overcome by writing or
converting the infix expression to an alternate notation such as the prefix or the
postfix.
((A - (B + C)) * (C+ D / B )) ^ B + C Infix expression
A B C + - C D B / + * B ^ C + Postfix expression
Lets:
A = 6
B = 2
C = 3
D = 8
6 2 3 + - 3 8 2 / + * 2 ^ 3 + Postfix
Evaluate the following expression in postfix
6 2 3 + - 3 8 2 / + * 2 ^ 3 +
Ad

More Related Content

Similar to Applications of Stack (Data Structure).pdf (20)

Lecture6
Lecture6Lecture6
Lecture6
Muhammad Zubair
ย 
Prefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptxPrefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptx
rtiwary190801
ย 
Stack application in infix to prefix expression
Stack application in infix to prefix expressionStack application in infix to prefix expression
Stack application in infix to prefix expression
deepalishinkar1
ย 
data structure stack appplication in python
data structure stack appplication in pythondata structure stack appplication in python
data structure stack appplication in python
deepalishinkar1
ย 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
ย 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
ย 
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
ย 
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdf
arihantmobilepoint15
ย 
Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)
lahariit406
ย 
The New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of theThe New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of the
shubhamgupta7133
ย 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
ย 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
ย 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
ย 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
NARESH GUMMAGUTTA
ย 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
ย 
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
devismileyrockz
ย 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
mrizwan38
ย 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
ย 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
ย 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
ย 
Prefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptxPrefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptx
rtiwary190801
ย 
Stack application in infix to prefix expression
Stack application in infix to prefix expressionStack application in infix to prefix expression
Stack application in infix to prefix expression
deepalishinkar1
ย 
data structure stack appplication in python
data structure stack appplication in pythondata structure stack appplication in python
data structure stack appplication in python
deepalishinkar1
ย 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
ย 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
ย 
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
ย 
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdf
arihantmobilepoint15
ย 
Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)
lahariit406
ย 
The New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of theThe New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of the
shubhamgupta7133
ย 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
ย 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
ย 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
ย 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
NARESH GUMMAGUTTA
ย 
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
devismileyrockz
ย 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
mrizwan38
ย 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
ย 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
ย 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
ย 

Recently uploaded (20)

COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
ย 
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngnReport Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
yousafmuzammil19
ย 
Naan mudalvan assignment for students_064143.pptx
Naan mudalvan assignment for  students_064143.pptxNaan mudalvan assignment for  students_064143.pptx
Naan mudalvan assignment for students_064143.pptx
NaziaFarheen13
ย 
325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf
shivsin165
ย 
Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?
Gregory Vigneaux
ย 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
ย 
STOCK ANALYSYS.pptx manajemen keuangan s
STOCK ANALYSYS.pptx manajemen keuangan sSTOCK ANALYSYS.pptx manajemen keuangan s
STOCK ANALYSYS.pptx manajemen keuangan s
kfdpontianak2012
ย 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
ย 
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
Friends of Figm a, Sydney
ย 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
ย 
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptxDesign of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
BapujiBanothu
ย 
dFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptxdFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptx
AKSHAYKAMBLE806728
ย 
behiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdfbehiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdf
ShakibulHasan14
ย 
Emirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdfEmirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdf
asfianoor1
ย 
mid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptxmid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptx
omar164646
ย 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
ย 
Presentation for Schoool Management System
Presentation for Schoool Management SystemPresentation for Schoool Management System
Presentation for Schoool Management System
kolay922013
ย 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
ย 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
ย 
presentation on healing architecture .pptx
presentation on healing architecture .pptxpresentation on healing architecture .pptx
presentation on healing architecture .pptx
buildnpl
ย 
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
ย 
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngnReport Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
Report Writing PPT.pptxcvdbfbdbfvvdvfvfbfbgngn
yousafmuzammil19
ย 
Naan mudalvan assignment for students_064143.pptx
Naan mudalvan assignment for  students_064143.pptxNaan mudalvan assignment for  students_064143.pptx
Naan mudalvan assignment for students_064143.pptx
NaziaFarheen13
ย 
325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf325295919-AAC-Blocks-Seminar-Presentation.pdf
325295919-AAC-Blocks-Seminar-Presentation.pdf
shivsin165
ย 
Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?
Gregory Vigneaux
ย 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
ย 
STOCK ANALYSYS.pptx manajemen keuangan s
STOCK ANALYSYS.pptx manajemen keuangan sSTOCK ANALYSYS.pptx manajemen keuangan s
STOCK ANALYSYS.pptx manajemen keuangan s
kfdpontianak2012
ย 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
ย 
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ฎ๐—น๐—น: ๐—œ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€
Friends of Figm a, Sydney
ย 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
ย 
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptxDesign of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
BapujiBanothu
ย 
dFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptxdFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptx
AKSHAYKAMBLE806728
ย 
behiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdfbehiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdf
ShakibulHasan14
ย 
Emirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdfEmirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdf
asfianoor1
ย 
mid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptxmid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptx
omar164646
ย 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
ย 
Presentation for Schoool Management System
Presentation for Schoool Management SystemPresentation for Schoool Management System
Presentation for Schoool Management System
kolay922013
ย 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
ย 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
ย 
presentation on healing architecture .pptx
presentation on healing architecture .pptxpresentation on healing architecture .pptx
presentation on healing architecture .pptx
buildnpl
ย 
Ad

Applications of Stack (Data Structure).pdf

  • 1. Applications of Stack The following Applications of stack are: 1. Stack is used by compilers to check for balancing of parentheses, brackets and braces. 2. Stack is used to evaluate a postfix expression. 3. Stack is used to convert an infix expression into postfix/prefix form. 4. In recursion, all intermediate arguments and return values are stored on the processorโ€™s stack. 5. During a function call the return address and arguments are pushed onto a stack and on return they are popped off.
  • 2. Expression evaluation and conversion โ€ข The most frequent application of stacks is in the evaluation of arithmetic expressions. An arithmetic expression is made of operands, operators. โ€ข Operand is the quantity on which a mathematical operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol which signifies a mathematical or logical operation between the operands. Examples of familiar operators include +, -, *, /, ^ etc. โ€ข When high-level programming languages came into existence, one of the major difficulties faced by computer scientists was to generate machine language instructions that could properly evaluate any arithmetic expression. A complex assignment statement such as
  • 3. Might have several meanings, and even if the meanings were uniquely defied, it is still difficult to generate a correct and reasonable instruction sequence. The fist problem in understanding the meaning of an expression is to decide the order in which the operations are to be carried out. This demands that every language must uniquely define such an order.
  • 4. Another example: To calculate this expression for values 4, 3, 7 for A, B, C respectively we must follow certain in order to have the right result : โ€ข The answer is not correct; multiplication is to be done before the addition, because multiplication has higher precedence over addition. This means that an expression is calculated according to the operatorโ€™s precedence not the order as they look like. โ€ข Thus expression A + B * C can be interpreted as A + (B * C). Using this alternative method we can convey to the computer that multiplication has higher precedence over addition.
  • 5. โ€ข To fix the order of evaluation, assign each operator a priority, and evaluated from left to right.
  • 6. โ€ข Let us consider the expression โ€ข By using priorities rules, the expression X is rewritten as โ€ข We manually evaluate the innermost expression first, followed by the next parenthesized inner expression, which produces the result. โ€ข Another application of stack is calculation of postfix expression. There are basically three types of notation for an expression (mathematical expression; An expression is defined as the number of operands or data items combined with several operators.) 1. Infix notation 2. Prefix notation 3. Postfix notation
  • 7. โ€ข Infix The infix notation is what we come across in our general mathematics, where the operator is written in-between the operands. For example: The expression to add two numbers A and B is written in infix notation as: โ€ข Prefix The prefix notation is a notation in which the operator(s) is written before the operands. The same expression when written in prefix notation looks like: โ€ข Postfix In the postfix notation the operator(s) are written after the operands, so it is called the postfix notation (post means after), it is also known as suffix notation. The above expression if written in postfix expression looks like:
  • 8. A + B * C Infix Form A + (B * C) Parenthesized expression A + (B C *) Convert the multiplication A (B C *) + Convert the addition A B C * + Postfix form The rules to be remembered during infix to postfix or prefix conversion are: 1. Parenthesize the expression starting from left to right. 2. During parenthesizing the expression, the operands associated with operator having higher precedence are first parenthesized. For example in the above expression B * C is parenthesized first before A + B. 3. The sub-expression (part of expression), which has been converted into postfix, is to be treated as single operand. 4. Once the expression is converted to postfix form, remove the parenthesis.
  • 9. Exercise A * B + C / D (A * B) + (C / D) Parenthesized expression (A B*) + (C / D) Convert the multiplication (A B*) + (C D/) Convert the division (A B*) (C D/) + Convert the addition A B*C D/ + Postfix form
  • 10. Converting infix to prefix expression Example: A * B + C / D Infix Form (A * B) + (C/D) Parenthesized expression (*A B) + (C/D) Convert Multiplication (*A B) + (/CD ) Convert Division +(*A B) (/CD ) Convert addition +*A B /CD Prefix form Note: the precedence rules for converting an expression from infix to prefix is identical to postfix. The only change from postfix conversion is that the operator is placed before the operands rather than after them.
  • 11. Conversion from infix to postfix (another method): Procedure to convert from infix expression to postfix expression is as follows: 1. Scan the infix expression from left to right. 2. a) If the scanned symbol is left parenthesis, push it onto the stack. b) If the scanned symbol is an operand, then place directly in the postfix expression (output). c) If the symbol scanned is a right parenthesis, then go on popping all the items from the stack and place them in the postfix expression till we get the matching left parenthesis. d) If the scanned symbol is an operator, then go on removing all the operators from the stack and place them in the postfix expression, if and only if the precedence of the operator which is on the top of the stack is greater than (or greater than or equal) to the precedence of the scanned operator and push the scanned operator onto the stack otherwise, push the scanned operator onto the stack. The three important features of postfix expression are: 1. The operands maintain the same order as in the equivalent infix expression. 2. The parentheses are not needed to designate the expression unambiguously. 3. While evaluating the postfix expression the priority of the operators is no longer relevant.
  • 12. Example: Convert the following infix expression to the equivalent postfix notation. (30+23)*(43-21)/(84+7)
  • 13. Need for prefix and postfix expression The evaluation of an infix expression using a computer needs proper code generation by the compiler without any ambiguity and is difficult because of various aspects such as the operatorโ€™s priority . This problem can be overcome by writing or converting the infix expression to an alternate notation such as the prefix or the postfix. ((A - (B + C)) * (C+ D / B )) ^ B + C Infix expression A B C + - C D B / + * B ^ C + Postfix expression Lets: A = 6 B = 2 C = 3 D = 8 6 2 3 + - 3 8 2 / + * 2 ^ 3 + Postfix
  • 14. Evaluate the following expression in postfix 6 2 3 + - 3 8 2 / + * 2 ^ 3 +