SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
Applications of Stack
www.eshikshak.co.in
Reverse String or List
 ● We can accomplish this task by pushing each character or member from the
   string/list in the order it appears
 ● When the line is finished, characters are then proposed off the stack

                     “They come off in a reverse order”




                         www.eshikshak.co.in
Reverse string – Name=“Angel”
                                              L
P                                    E        E
U
                           G         G        G
S
H              N           N         N        N
      A        A           A         A        A




      E
P
O     G        G
P     N        N            N
      A        A            A        A

    Name :   Name :       Name :   Name :   Name :
    “L”      “LE”         “LEG”    “LEGN”   “LEGNA”
                      www.eshikshak.co.in
Polish Notation
 ● The process of writing the operators of expression either before their
   operands or after them is called “Polish Notation”
 ● The main property of Polish Notation is that the order in which operations are
   to be performed is ascertained by the position of the operators and operands
   in the expression




                           www.eshikshak.co.in
Polish Notation
 ● The notation refers to these complex arithmetic expressions in three forms:
     ○ If the operator symbols are placed between its
       operands, then the expression is in infix notation
          ■A + B
     ○ If the operator symbols are placed before its operands,
       then the expression is in prefix notation
          ■ +AB
     ○ If the operator symbols are placed after its operands,
       then the expression is in postfix notation
          ■ AB+




                          www.eshikshak.co.in
Notation


Infix Notation                   Prefix Notation      Postfix Notation


A+B                              + AB                 AB +


(A - C) + B                      + - ACB              AC – B +


A+(B*C)                          + A * BC             ABC *+


(A+B)/(C-D)                      /+ AB – CD           AB + CD -/


(A + (B * C))/(C – (D * B))      /+ A * BC – C * DB   ABC * + CDB * - /


                              www.eshikshak.co.in
Convert Infix expression to Postfix expression
A – It is an expression B – Postfix expression
Step 1. Push left parenthesis “(“ into STACK and add right parenthesis “)” to the
end of A
Step 2. Scan A from left to right and repeat step 3 to 6 for each element of A
until the stack is empty
Step 3. If an operand is encountered, add it to B
Step 4. If a left parenthesis is encountered push it onto the stack
Step 5. If an operator is encountered then
  1. Repeatedly pop from the STACK and add to B each operator (on the top of
      stack) which has the same precedence as or higher precedence than
      operator
  2. Add operator to STACK
Step 6. If a right parenthesis is encountered, then
  1. Repeatedly pop from the STACK and add to B each operator (on the top of
      STACK) until a left parenthesis is encountered
  2. Remove the left parenthesis. (Do not add left parenthesis to B)
Step 7. Exit
                           www.eshikshak.co.in
Convert Infix expression to Prefix expression
  ● A – Arithmetic Expression B – Prefix Expression
Step 1. Push “)” onto STACK, and add “(“ to end of the A
Step 2. Scan A from right to left and repeat step 3 to 6 for each element
of A until the STACK is empty
Step 3. If an operand is encountered add it to B
Step 4. If a right parenthesis is encountered push it onto STACK
Step 5. If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each operator (on the top
of STACK) which has
b. Add operator to STACK
Step 6. If left parenthesis is encontered then
a. Repeatedly pop from the STACK and add to B (each operator on top
of stack until a left parenthesis is encounterd)
b. Remove the left parenthesis
Step 7. Exit

                        www.eshikshak.co.in
Evaluation of Postfix Expression
/* Reading the expression takes place from left to right */
Step 1. Read the next element /* first element for first time */
Step 2. If element is operand than
i. Push the element in the stack
Step 3. If element is operator then
i. Pop two operands from the stack /* POP one operand in case of NOT operator
*/
ii. Evaluate the expression formed by the two operands and the operator
iii. Push the results of the expression in the stack end.
Step 4. If no more elements then
i. POP the result
else
goto step 1
Step 5. Exit
                             www.eshikshak.co.in
Evaluation of Prefix Expression

/* Reading the expression take place from right to left /*
Step 1. Read the next element
Step 2. If element is operand then
i. Push the element in the stack
Step 3. If element is operator then
i. Pop two operands from the stack
ii. Evaluate the expression formed by two operands and the operator
iii. Push the results of the expression in the stack end
Step 4. if no more elements then
i. Pop the result
else
goto step 1
Step 5. Exit
                           www.eshikshak.co.in
Recursion
  ● The function which call itself (In function body ) again and
    again is known as recursive function. This function will call
    itself as long as the condition is satisfied.
  ● This recursion can be removed by two ways:
         1. Through Iteration.
2. Through Stack

  ● Many mathematical functions can be defined recursively:
       ○ Factorial
       ○ Fibonacci
       ○ Euclid's GCD (greatest common denominator)
       ○ Fourier Transform




                                 www.eshikshak.co.in
Factorial function in c
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return(n*fact(n-1));
}
}
                       www.eshikshak.co.in
Use of STACK – FACTORIAL EXECUTION


      n=1         1


              2 * fact(1)
      n=2                    2*1=2

              3 * fact(2)
      n=3                    3*2=6

              4 * fact(3)
      n=4                    4 * 6 = 24


      n=5     5 * fact(4)   5 * 24 = 120




            www.eshikshak.co.in
Removal Through Stack
 ● Suppose P is a recursive procedure . The translation of
   recursive procedure P into a non recursive procedure
   work as follows:


 ● First of all, one defines:
1. A stack STPAR for each parameter PAR
2. A stack STVAR for each local variable VAR
3. A local variable ADD and a stack STADD to hold return
address.




                      www.eshikshak.co.in
● The algorithm which translates the recursive procedure P
   into a non recursive procedure follows. It consist of three
   parts:


1. Preparation
2. Translating each recursive call P in procedure P.
3. Translating each return in procedure P.




                     www.eshikshak.co.in
1. Preparation
(a) define a stack STPAR for each parameter PAR, a stack
STVAR for each local variable VAR, and a local variable
ADD and a stack STADD to hold return address.
(b) Set TOP = NULL
2. Translation of “step K.call P.”
(a) Push the current values of the parameters and local
variable onto the appropriate stacks, and push the new
return address [step ] K+1 onto STADD.




                     www.eshikshak.co.in
(b) reset the parameters using the new argument values.
(c) go to step 1.[The beginning of procedure P]
3. Translation of “Step J.return”
(a) if STADD is empty, then return.[control is return to the
main program].
(b) Restore the top values of the stacks.That is, set the
parameters and local variables equal to the top values on
the stacks, and set ADD equal to the top value on the
STADD.
(c) Go to the step ADD.
4. Step L. return




                     www.eshikshak.co.in
Translation of recursive to non-recursive procedure

● Declare STACK to hold all the local variables
● Push all the local variables and parameters called by
  value into the stack
● At the end of recursive function or whenever the function
  returns to the calling program, the following steps should
  be performed
   ○ If stack is empty, then the recursion has finished;
     make a normal return
   ○ Otherwise pop the stack to restore the values of all
     local variables and parameters called by value



                    www.eshikshak.co.in
Difference between Recursion and Iteration

 ● ITERATION                            ● RECURSIVE
  ● It is a process of executing        ● Recursion is a technique of
    statements repeatedly, until          defining anything in terms of
    some specific condition is            itself
    specified
                                        ● There must be an exclusive if
  ● Iteration involves four clear cut     statement inside the recursive
    steps, initialization, condition,     function specifying stopping
    execution and updating                condition
  ● Any recursive problem can be        ● Not all problems has recursive
    solved iteratively                    solution
  ● Iterative computer part of a        ● Recursion is generally a worst
    problem is more efficient in          option to go for simple program
    terms of memory utilization and       or problems not recursive in
    execution speed                       nature

        www.eshikshak.co.in
Tower of Hanoi
 ● Suppose three peg – A, B & C
 ● On peg A there are finite numbers of n disks with
   decreasing size




                    www.eshikshak.co.in
Tower of Hanoi - Rules
 ● Only one disk may be moved at a time (only the top disk
   on any peg may be moved to any other peg)
 ● A larger disk can be placed on a smaller (smaller disk can
   not be placed on larger)




                    www.eshikshak.co.in
Tower of Hanoi – Plates Move




         www.eshikshak.
Algorithm : Tower(N, BEG, AUX, END)
 ● This procedure gives a recursive solution to the Towers of
   Hanoi problem for N disks
Step 1. If N = 1 then
(a) Write : BEG -> END
(b) Return
End of If Structure
Step 2. [Move N – 1 disks peg BEG to peg AUX ]
Call TOWER(N-1, BEG, END, AUX)
Step 3. Write : BEG->END
Step 4. [Move N – 1 disks peg AUX to peg END]
Call TOWER(N-1, AUX, BEG, END)
Step 5. Return

                         www.eshikshak.co.in
www.eshikshak.co.in

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Stack
StackStack
Stack
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
single linked list
single linked listsingle linked list
single linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Circular queue
Circular queueCircular queue
Circular queue
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Queues
QueuesQueues
Queues
 

Ähnlich wie Applications of Stacks in Programming

Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptxBaby81727
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfarihantsherwani
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha Mahalingam
 

Ähnlich wie Applications of Stacks in Programming (20)

Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack
StackStack
Stack
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
stack
stackstack
stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 

Mehr von eShikshak

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluationeShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythoneShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerceeShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computingeShikshak
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computingeShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloudeShikshak
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computingeShikshak
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computingeShikshak
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variableseShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorseShikshak
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 

Mehr von eShikshak (20)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 

Kürzlich hochgeladen

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Kürzlich hochgeladen (20)

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Applications of Stacks in Programming

  • 2. Reverse String or List ● We can accomplish this task by pushing each character or member from the string/list in the order it appears ● When the line is finished, characters are then proposed off the stack “They come off in a reverse order” www.eshikshak.co.in
  • 3. Reverse string – Name=“Angel” L P E E U G G G S H N N N N A A A A A E P O G G P N N N A A A A Name : Name : Name : Name : Name : “L” “LE” “LEG” “LEGN” “LEGNA” www.eshikshak.co.in
  • 4. Polish Notation ● The process of writing the operators of expression either before their operands or after them is called “Polish Notation” ● The main property of Polish Notation is that the order in which operations are to be performed is ascertained by the position of the operators and operands in the expression www.eshikshak.co.in
  • 5. Polish Notation ● The notation refers to these complex arithmetic expressions in three forms: ○ If the operator symbols are placed between its operands, then the expression is in infix notation ■A + B ○ If the operator symbols are placed before its operands, then the expression is in prefix notation ■ +AB ○ If the operator symbols are placed after its operands, then the expression is in postfix notation ■ AB+ www.eshikshak.co.in
  • 6. Notation Infix Notation Prefix Notation Postfix Notation A+B + AB AB + (A - C) + B + - ACB AC – B + A+(B*C) + A * BC ABC *+ (A+B)/(C-D) /+ AB – CD AB + CD -/ (A + (B * C))/(C – (D * B)) /+ A * BC – C * DB ABC * + CDB * - / www.eshikshak.co.in
  • 7. Convert Infix expression to Postfix expression A – It is an expression B – Postfix expression Step 1. Push left parenthesis “(“ into STACK and add right parenthesis “)” to the end of A Step 2. Scan A from left to right and repeat step 3 to 6 for each element of A until the stack is empty Step 3. If an operand is encountered, add it to B Step 4. If a left parenthesis is encountered push it onto the stack Step 5. If an operator is encountered then 1. Repeatedly pop from the STACK and add to B each operator (on the top of stack) which has the same precedence as or higher precedence than operator 2. Add operator to STACK Step 6. If a right parenthesis is encountered, then 1. Repeatedly pop from the STACK and add to B each operator (on the top of STACK) until a left parenthesis is encountered 2. Remove the left parenthesis. (Do not add left parenthesis to B) Step 7. Exit www.eshikshak.co.in
  • 8. Convert Infix expression to Prefix expression ● A – Arithmetic Expression B – Prefix Expression Step 1. Push “)” onto STACK, and add “(“ to end of the A Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty Step 3. If an operand is encountered add it to B Step 4. If a right parenthesis is encountered push it onto STACK Step 5. If an operator is encountered then: a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has b. Add operator to STACK Step 6. If left parenthesis is encontered then a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd) b. Remove the left parenthesis Step 7. Exit www.eshikshak.co.in
  • 9. Evaluation of Postfix Expression /* Reading the expression takes place from left to right */ Step 1. Read the next element /* first element for first time */ Step 2. If element is operand than i. Push the element in the stack Step 3. If element is operator then i. Pop two operands from the stack /* POP one operand in case of NOT operator */ ii. Evaluate the expression formed by the two operands and the operator iii. Push the results of the expression in the stack end. Step 4. If no more elements then i. POP the result else goto step 1 Step 5. Exit www.eshikshak.co.in
  • 10. Evaluation of Prefix Expression /* Reading the expression take place from right to left /* Step 1. Read the next element Step 2. If element is operand then i. Push the element in the stack Step 3. If element is operator then i. Pop two operands from the stack ii. Evaluate the expression formed by two operands and the operator iii. Push the results of the expression in the stack end Step 4. if no more elements then i. Pop the result else goto step 1 Step 5. Exit www.eshikshak.co.in
  • 11. Recursion ● The function which call itself (In function body ) again and again is known as recursive function. This function will call itself as long as the condition is satisfied. ● This recursion can be removed by two ways: 1. Through Iteration. 2. Through Stack ● Many mathematical functions can be defined recursively: ○ Factorial ○ Fibonacci ○ Euclid's GCD (greatest common denominator) ○ Fourier Transform www.eshikshak.co.in
  • 12. Factorial function in c int fact(int n) { if(n==1) { return 1; } else { return(n*fact(n-1)); } } www.eshikshak.co.in
  • 13. Use of STACK – FACTORIAL EXECUTION n=1 1 2 * fact(1) n=2 2*1=2 3 * fact(2) n=3 3*2=6 4 * fact(3) n=4 4 * 6 = 24 n=5 5 * fact(4) 5 * 24 = 120 www.eshikshak.co.in
  • 14. Removal Through Stack ● Suppose P is a recursive procedure . The translation of recursive procedure P into a non recursive procedure work as follows: ● First of all, one defines: 1. A stack STPAR for each parameter PAR 2. A stack STVAR for each local variable VAR 3. A local variable ADD and a stack STADD to hold return address. www.eshikshak.co.in
  • 15. ● The algorithm which translates the recursive procedure P into a non recursive procedure follows. It consist of three parts: 1. Preparation 2. Translating each recursive call P in procedure P. 3. Translating each return in procedure P. www.eshikshak.co.in
  • 16. 1. Preparation (a) define a stack STPAR for each parameter PAR, a stack STVAR for each local variable VAR, and a local variable ADD and a stack STADD to hold return address. (b) Set TOP = NULL 2. Translation of “step K.call P.” (a) Push the current values of the parameters and local variable onto the appropriate stacks, and push the new return address [step ] K+1 onto STADD. www.eshikshak.co.in
  • 17. (b) reset the parameters using the new argument values. (c) go to step 1.[The beginning of procedure P] 3. Translation of “Step J.return” (a) if STADD is empty, then return.[control is return to the main program]. (b) Restore the top values of the stacks.That is, set the parameters and local variables equal to the top values on the stacks, and set ADD equal to the top value on the STADD. (c) Go to the step ADD. 4. Step L. return www.eshikshak.co.in
  • 18. Translation of recursive to non-recursive procedure ● Declare STACK to hold all the local variables ● Push all the local variables and parameters called by value into the stack ● At the end of recursive function or whenever the function returns to the calling program, the following steps should be performed ○ If stack is empty, then the recursion has finished; make a normal return ○ Otherwise pop the stack to restore the values of all local variables and parameters called by value www.eshikshak.co.in
  • 19. Difference between Recursion and Iteration ● ITERATION ● RECURSIVE ● It is a process of executing ● Recursion is a technique of statements repeatedly, until defining anything in terms of some specific condition is itself specified ● There must be an exclusive if ● Iteration involves four clear cut statement inside the recursive steps, initialization, condition, function specifying stopping execution and updating condition ● Any recursive problem can be ● Not all problems has recursive solved iteratively solution ● Iterative computer part of a ● Recursion is generally a worst problem is more efficient in option to go for simple program terms of memory utilization and or problems not recursive in execution speed nature www.eshikshak.co.in
  • 20. Tower of Hanoi ● Suppose three peg – A, B & C ● On peg A there are finite numbers of n disks with decreasing size www.eshikshak.co.in
  • 21. Tower of Hanoi - Rules ● Only one disk may be moved at a time (only the top disk on any peg may be moved to any other peg) ● A larger disk can be placed on a smaller (smaller disk can not be placed on larger) www.eshikshak.co.in
  • 22. Tower of Hanoi – Plates Move www.eshikshak.
  • 23. Algorithm : Tower(N, BEG, AUX, END) ● This procedure gives a recursive solution to the Towers of Hanoi problem for N disks Step 1. If N = 1 then (a) Write : BEG -> END (b) Return End of If Structure Step 2. [Move N – 1 disks peg BEG to peg AUX ] Call TOWER(N-1, BEG, END, AUX) Step 3. Write : BEG->END Step 4. [Move N – 1 disks peg AUX to peg END] Call TOWER(N-1, AUX, BEG, END) Step 5. Return www.eshikshak.co.in