SlideShare ist ein Scribd-Unternehmen logo
1 von 16
A
Data Structure
   What is a Stack

   Stack Operations

   LIFO Stack

   Animation Depicting Push Operation

   Animation Depicting Pop Operation

   Array Implementation of Stack

   Applications of stack
        Checking for balanced braces
        Converting Infix Expression to Postfix
        Postfix calculator
What is a Stack?
A  stack is a Last In, First Out (LIFO) data
structure,objects inserted last are the first to
come out of the stack


Anything  added to the stack goes on the “top”
of the stack


Anything  removed from the stack is taken from
the “top” of the stack


Things  are removed in the reverse order from
that in which they were inserted
Stack Operations
   PUSH
     Adds the object to the top of the stack

   POP
     Removes the object at the top of the stack
      and returns it

   PEEK
      Returns the top object of the stack but does
      not remove it from the stack

   ISEMPTY
      Returns True if Stack is empty
A LIFO Stack
                Push            Pop




Stack Pointer           Top




                       Bottom
array Implementation of Stack
                         A[1] A[2] ….         ….    ….    A[n]


Push X                    w   y       x



            Stack pointer         Stack pointer       After many   Stack pointer
                 J=2                   J=3            Push Ops          J=n
             Before Push           After Push                        Stack Full




Pop                       w   y

After many Pop ops

         Stack Pointer        Stack Pointer        Stack Pointer
             J=0                   J=2                 J=3
         Stack Empty            After Pop           Before Pop
Checking for Balanced Braces

   ([]({()}[()])) is balanced; ([]({()}[())]) is not
   Simple counting is not enough to check
    balance
   You can do it with a stack: going left to
    right,
      If you see a (, [, or {, push it on the
        stack
      If you see a ), ], or }, pop the stack
        and check whether you got the
        corresponding (, [, or {
      When you reach the end, check that the
        stack is empty
Converting Infix Expressions to Equivalent
               Postfix Expressions
   Infix Expression         a+b

   Postfix Expression       ab+

   An infix expression can be evaluated by first
    being converted into an equivalent postfix
    expression

   Facts about converting from infix to postfix
     Operands always stay in the same order with
      respect to one another
     All parentheses are removed
Algorithm : Converting Infix Expression to
               Postfix using stack

  Suppose X is an arithmetic expression written in infix notation.
  This algorithm finds the equivalent postfix expression Y.

   1. Push "(" onto STACK, and add ")" to the end of X.
   2. Scan X from left to right and REPEAT Steps 3 to 6 for each element of X UNTIL the
       STACK is empty :
   3. If an operand is encountered, add it to Y.
   4. If a left parenthesis is encountered, push it onto STACK.
   5. If an operator is encountered, then :
   (a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has
   the same precedence as or higher precedence than operator.
   (b) Add operator to STACK.
   /* End of If structure * /
   6. If a right parenthesis is encountered, then :
   (a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK)
   until a left parenthesis is encountered.
   (b) Remove the left parenthesis. [Do not add the left parenthesis to Y].
   /* End of If structure * /
   /* End of Step 2 loop * /
   7. END.
A trace of the algorithm that converts the infix expression
             a - (b + c * d)/e to postfix form
A postfix calculator
   When an operand is entered, the calculator
     Pushes it onto a stack



   When an operator is entered, the calculator
     Applies it to the top (one or two, depending
      on unary or binary operator) operand(s) of
      the stack

       Pops the operands from the stack

       Pushes the result of the operation on the
        stack
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
                        Postfix Notation--- 2 3 4 + *
Acknowledgement

   Data Structures BY Tanenbaum
   Internet
   Computer Science C++ A textbook for class XII        -By
    Sumita Arora




                         prepared by-
                            Seema kaushik,
                    Computer science department,
                 DAV public school, sector-14, gurgaon

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
Saqib Saeed
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
Tech_MX
 

Was ist angesagt? (20)

Queues
QueuesQueues
Queues
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
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
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 

Andere mochten auch (7)

16 combinatroics-2
16 combinatroics-216 combinatroics-2
16 combinatroics-2
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & Combination
 
Permutations and combinations examples
Permutations and combinations examplesPermutations and combinations examples
Permutations and combinations examples
 
Permutation and combination
Permutation and combinationPermutation and combination
Permutation and combination
 
Permutations & Combinations
Permutations & CombinationsPermutations & Combinations
Permutations & Combinations
 

Ähnlich wie Stack

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
KALPANAC20
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
Ramachandra Adiga G
 
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
haaamin01
 
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
arihantsherwani
 

Ähnlich wie Stack (20)

stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
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
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Data structures
Data structures Data structures
Data structures
 
Stacks
StacksStacks
Stacks
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
 
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
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
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
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
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
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Stack

  • 2. What is a Stack  Stack Operations  LIFO Stack  Animation Depicting Push Operation  Animation Depicting Pop Operation  Array Implementation of Stack  Applications of stack Checking for balanced braces Converting Infix Expression to Postfix Postfix calculator
  • 3. What is a Stack? A stack is a Last In, First Out (LIFO) data structure,objects inserted last are the first to come out of the stack Anything added to the stack goes on the “top” of the stack Anything removed from the stack is taken from the “top” of the stack Things are removed in the reverse order from that in which they were inserted
  • 4. Stack Operations  PUSH Adds the object to the top of the stack  POP Removes the object at the top of the stack and returns it  PEEK Returns the top object of the stack but does not remove it from the stack  ISEMPTY Returns True if Stack is empty
  • 5. A LIFO Stack Push Pop Stack Pointer Top Bottom
  • 6.
  • 7.
  • 8. array Implementation of Stack A[1] A[2] …. …. …. A[n] Push X w y x Stack pointer Stack pointer After many Stack pointer J=2 J=3 Push Ops J=n Before Push After Push Stack Full Pop w y After many Pop ops Stack Pointer Stack Pointer Stack Pointer J=0 J=2 J=3 Stack Empty After Pop Before Pop
  • 9. Checking for Balanced Braces  ([]({()}[()])) is balanced; ([]({()}[())]) is not  Simple counting is not enough to check balance  You can do it with a stack: going left to right,  If you see a (, [, or {, push it on the stack  If you see a ), ], or }, pop the stack and check whether you got the corresponding (, [, or {  When you reach the end, check that the stack is empty
  • 10.
  • 11. Converting Infix Expressions to Equivalent Postfix Expressions  Infix Expression a+b  Postfix Expression ab+  An infix expression can be evaluated by first being converted into an equivalent postfix expression  Facts about converting from infix to postfix  Operands always stay in the same order with respect to one another  All parentheses are removed
  • 12. Algorithm : Converting Infix Expression to Postfix using stack Suppose X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y. 1. Push "(" onto STACK, and add ")" to the end of X. 2. Scan X from left to right and REPEAT Steps 3 to 6 for each element of X UNTIL the STACK is empty : 3. If an operand is encountered, add it to Y. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator is encountered, then : (a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has the same precedence as or higher precedence than operator. (b) Add operator to STACK. /* End of If structure * / 6. If a right parenthesis is encountered, then : (a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to Y]. /* End of If structure * / /* End of Step 2 loop * / 7. END.
  • 13. A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form
  • 14. A postfix calculator  When an operand is entered, the calculator  Pushes it onto a stack  When an operator is entered, the calculator  Applies it to the top (one or two, depending on unary or binary operator) operand(s) of the stack  Pops the operands from the stack  Pushes the result of the operation on the stack
  • 15. The action of a postfix calculator when evaluating the expression 2 * (3 + 4) Postfix Notation--- 2 3 4 + *
  • 16. Acknowledgement  Data Structures BY Tanenbaum  Internet  Computer Science C++ A textbook for class XII -By Sumita Arora prepared by- Seema kaushik, Computer science department, DAV public school, sector-14, gurgaon