SlideShare a Scribd company logo
1 of 10
Download to read offline
Stack
Dr. Shaukat Wasi and Dr. Syed Imran Jami
Stack defined…
ADT
Linear Data Structure
Last-In First-Out (LIFO)
• Pile of Books
• Access Limited to a specific Order
Operations
• Push (Insert)
• Pop (Remove)
• Peek (Access)
Traversal always produces items in a Reverse order by default
Stack Applications
• Breadth First Search Algorithms
• Python/Java – Function Call Handling
• Compilers – Expression Parsing
• Balanced Parenthesis Checking
• Back Tracking Algorithms
• Web Browsers – Back and Next Mechanism
• Applications – UNDO/REDO operations
Stack -
Complexity
• Time Complexity
• All above operations – O(1)
• Traversal – O(n)
Stack
Implementations
• Random Access Restricted – Is it really needed?
• Push and Pop Complexity reaches to O(n) –
WHY? If first index is set as Top of the stack.
• Else if last pointer is used to consider the
current filled slot as the top, complexity will
remain O(1).
• Search Complexity?
Array Implementation
• Push – Insert at Top/Head
• Pop – Delete from Top/Head
• Complexity remains same as expected
Linked List Implementation
Stack Problems ☺
• Convert one form of expression to another form
• Infix to Postfix
• Arithmetic Expression Evaluation
• Postfix Expression Evaluation
• Check for balanced parentheses in an expression
• Iterative Tower of Hanoi
• The Tower of Hanoi is a mathematical puzzle.
• It consists of three poles and a number of disks of different sizes which can slide onto
any pole.
• The puzzle starts with the disk in a neat stack in ascending order of size in one pole,
the smallest at the top thus making a conical shape.
• The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to
another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
• The puzzle has the following two rules.
• You can’t place a larger disk onto a smaller disk
• Only one disk can be moved at a time
Stack Problems ☺
• Convert one form of expression to another form
• Infix to Postfix
• Arithmetic Expression Evaluation
• Postfix Expression Evaluation
• Check for balanced parentheses in an expression
• Iterative Tower of Hanoi
• The Tower of Hanoi is a mathematical puzzle.
• It consists of three poles and a number of disks of different sizes which can slide onto
any pole.
• The puzzle starts with the disk in a neat stack in ascending order of size in one pole,
the smallest at the top thus making a conical shape.
• The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to
another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
• The puzzle has the following two rules.
• You can’t place a larger disk onto a smaller disk
• Only one disk can be moved at a time
Stack Problems ☺
• Convert one form of expression to another form
• Infix to Postfix
• Arithmetic Expression Evaluation
• Postfix Expression Evaluation
• Check for balanced parentheses in an expression
• Iterative Tower of Hanoi
• The Tower of Hanoi is a mathematical puzzle.
• It consists of three poles and a number of disks of different sizes which can slide onto
any pole.
• The puzzle starts with the disk in a neat stack in ascending order of size in one pole,
the smallest at the top thus making a conical shape.
• The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to
another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
• The puzzle has the following two rules.
• You can’t place a larger disk onto a smaller disk
• Only one disk can be moved at a time
Stack Problems ☺
• Convert one form of expression to another form
• Infix to Postfix
• Arithmetic Expression Evaluation
• Postfix Expression Evaluation
• Check for balanced parentheses in an expression
• Iterative Tower of Hanoi (Iterative)
• The Tower of Hanoi is a mathematical puzzle.
• It consists of three poles and several disks of different sizes which can slide onto any
pole.
• The puzzle starts with the disk in a neat stack in ascending order of size in one pole,
the smallest at the top thus making a conical shape.
• The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to
another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
• The puzzle has the following two rules.
• You can’t place a larger disk onto a smaller disk
• Only one disk can be moved at a time
Questions !!!

More Related Content

Similar to Stack - Data structures - Dr. Shaukat Wasi

ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksHemanth Kumar
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfShiwani Gupta
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting AlgorithmAl Amin
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxPankaj Debbarma
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentationFahadQaiser1
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacksgrahamwell
 

Similar to Stack - Data structures - Dr. Shaukat Wasi (14)

ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_Stacks
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
l2.pptx
l2.pptxl2.pptx
l2.pptx
 
Data structure lab
Data structure labData structure lab
Data structure lab
 
l2.pptx
l2.pptxl2.pptx
l2.pptx
 
02LocalSearch.pdf
02LocalSearch.pdf02LocalSearch.pdf
02LocalSearch.pdf
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
september6.ppt
september6.pptseptember6.ppt
september6.ppt
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptx
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentation
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

Stack - Data structures - Dr. Shaukat Wasi

  • 1. Stack Dr. Shaukat Wasi and Dr. Syed Imran Jami
  • 2. Stack defined… ADT Linear Data Structure Last-In First-Out (LIFO) • Pile of Books • Access Limited to a specific Order Operations • Push (Insert) • Pop (Remove) • Peek (Access) Traversal always produces items in a Reverse order by default
  • 3. Stack Applications • Breadth First Search Algorithms • Python/Java – Function Call Handling • Compilers – Expression Parsing • Balanced Parenthesis Checking • Back Tracking Algorithms • Web Browsers – Back and Next Mechanism • Applications – UNDO/REDO operations
  • 4. Stack - Complexity • Time Complexity • All above operations – O(1) • Traversal – O(n)
  • 5. Stack Implementations • Random Access Restricted – Is it really needed? • Push and Pop Complexity reaches to O(n) – WHY? If first index is set as Top of the stack. • Else if last pointer is used to consider the current filled slot as the top, complexity will remain O(1). • Search Complexity? Array Implementation • Push – Insert at Top/Head • Pop – Delete from Top/Head • Complexity remains same as expected Linked List Implementation
  • 6. Stack Problems ☺ • Convert one form of expression to another form • Infix to Postfix • Arithmetic Expression Evaluation • Postfix Expression Evaluation • Check for balanced parentheses in an expression • Iterative Tower of Hanoi • The Tower of Hanoi is a mathematical puzzle. • It consists of three poles and a number of disks of different sizes which can slide onto any pole. • The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. • The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole). • The puzzle has the following two rules. • You can’t place a larger disk onto a smaller disk • Only one disk can be moved at a time
  • 7. Stack Problems ☺ • Convert one form of expression to another form • Infix to Postfix • Arithmetic Expression Evaluation • Postfix Expression Evaluation • Check for balanced parentheses in an expression • Iterative Tower of Hanoi • The Tower of Hanoi is a mathematical puzzle. • It consists of three poles and a number of disks of different sizes which can slide onto any pole. • The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. • The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole). • The puzzle has the following two rules. • You can’t place a larger disk onto a smaller disk • Only one disk can be moved at a time
  • 8. Stack Problems ☺ • Convert one form of expression to another form • Infix to Postfix • Arithmetic Expression Evaluation • Postfix Expression Evaluation • Check for balanced parentheses in an expression • Iterative Tower of Hanoi • The Tower of Hanoi is a mathematical puzzle. • It consists of three poles and a number of disks of different sizes which can slide onto any pole. • The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. • The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole). • The puzzle has the following two rules. • You can’t place a larger disk onto a smaller disk • Only one disk can be moved at a time
  • 9. Stack Problems ☺ • Convert one form of expression to another form • Infix to Postfix • Arithmetic Expression Evaluation • Postfix Expression Evaluation • Check for balanced parentheses in an expression • Iterative Tower of Hanoi (Iterative) • The Tower of Hanoi is a mathematical puzzle. • It consists of three poles and several disks of different sizes which can slide onto any pole. • The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. • The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole). • The puzzle has the following two rules. • You can’t place a larger disk onto a smaller disk • Only one disk can be moved at a time