SlideShare a Scribd company logo
1 of 10
Data Structures and
Algorithms for Information
        Processing
      Stacks & Queues



                             1
Stacks
• What is a Stack?
  – A stack is a data structure of ordered
    items such that items can be inserted and
    removed only at one end.
• Examples of Stacks:
  – Pez Dispenser
  – Cafeteria Trays



                                            2
Stacks
• What can we do with a stack?
  – push - place an item on the stack
  – peek - Look at the item on top of the stack,
    but do not remove it
  – pop - Look at the item on top of the stack
    and remove it




                                              3
Stacks
• A stack is a LIFO (Last-In/First-Out) data
  structure
• A stack is sometimes also called a
  pushdown store.
• What are some applications of stacks?
  – Program execution
  – Parsing
  – Evaluating postfix expressions

                                           4
Stacks
• Problem:
  – What happens if we try to pop an item off the
    stack when the stack is empty?
    • This is called a stack underflow. The pop method
      needs some way of telling us that this has
      happened. In java we use the
      java.util.EmptyStackException




                                                    5
Implementing a Stack
• There are two ways we can implement a
  stack:
  – Using an array
  – Using a linked list




                                          6
Implementing a Stack
• Implementing a stack using an array is
  fairly easy.
  – The bottom of the stack is at data[0]
  – The top of the stack is at data[numItems-1]
  – push onto the stack at data[numItems]
  – pop off of the stack at data[numItems-1]




                                                  7
Implementing a Stack
• Implementing a stack using a linked list
  isn’t that bad either…
  – Store the items in the stack in a linked list
  – The top of the stack is the head node, the
    bottom of the stack is the end of the list
  – push by adding to the front of the list
  – pop by removing from the front of the list



                                                    8
Reversing a Word
• We can use a stack to reverse the letters
  in a word.
• How?




                                          9
Reversing a Word
• Read each letter in the word and push it
  onto the stack
• When you reach the end of the word, pop
  the letters off the stack and print them out.




                                            10

More Related Content

Viewers also liked

Viewers also liked (7)

Geography of tourism
Geography of tourismGeography of tourism
Geography of tourism
 
เฟียเจท์ 1
เฟียเจท์ 1เฟียเจท์ 1
เฟียเจท์ 1
 
ฟรอยด์
ฟรอยด์ฟรอยด์
ฟรอยด์
 
Sample
SampleSample
Sample
 
Five
FiveFive
Five
 
Presentme
PresentmePresentme
Presentme
 
Presentme
PresentmePresentme
Presentme
 

Similar to Stack (Sandeep Aravali Udaipur)

stacks and queues.pptx
stacks and queues.pptxstacks and queues.pptx
stacks and queues.pptxAamirMaqsood8
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queueSunipa Bera
 
DSA_Lecture4-Stack.pptx
DSA_Lecture4-Stack.pptxDSA_Lecture4-Stack.pptx
DSA_Lecture4-Stack.pptxTahaIrfan14
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksHemanth Kumar
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxLECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSKUP1
 
Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,shaikhdaniyal8603
 
Lesson 3 - Stack ADT.pdf
Lesson 3 - Stack ADT.pdfLesson 3 - Stack ADT.pdf
Lesson 3 - Stack ADT.pdfLeandroJrErcia
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked listsAfriyieCharles
 

Similar to Stack (Sandeep Aravali Udaipur) (20)

Lecture9_StackQueue.ppt
Lecture9_StackQueue.pptLecture9_StackQueue.ppt
Lecture9_StackQueue.ppt
 
StackQueue.ppt
StackQueue.pptStackQueue.ppt
StackQueue.ppt
 
ppt.pptx
ppt.pptxppt.pptx
ppt.pptx
 
stacks and queues.pptx
stacks and queues.pptxstacks and queues.pptx
stacks and queues.pptx
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
DSA_Lecture4-Stack.pptx
DSA_Lecture4-Stack.pptxDSA_Lecture4-Stack.pptx
DSA_Lecture4-Stack.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_Stacks
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,
 
Data structure.pdf
Data structure.pdfData structure.pdf
Data structure.pdf
 
Lesson 3 - Stack ADT.pdf
Lesson 3 - Stack ADT.pdfLesson 3 - Stack ADT.pdf
Lesson 3 - Stack ADT.pdf
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Unit I.pptx
Unit I.pptxUnit I.pptx
Unit I.pptx
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

Stack (Sandeep Aravali Udaipur)

  • 1. Data Structures and Algorithms for Information Processing Stacks & Queues 1
  • 2. Stacks • What is a Stack? – A stack is a data structure of ordered items such that items can be inserted and removed only at one end. • Examples of Stacks: – Pez Dispenser – Cafeteria Trays 2
  • 3. Stacks • What can we do with a stack? – push - place an item on the stack – peek - Look at the item on top of the stack, but do not remove it – pop - Look at the item on top of the stack and remove it 3
  • 4. Stacks • A stack is a LIFO (Last-In/First-Out) data structure • A stack is sometimes also called a pushdown store. • What are some applications of stacks? – Program execution – Parsing – Evaluating postfix expressions 4
  • 5. Stacks • Problem: – What happens if we try to pop an item off the stack when the stack is empty? • This is called a stack underflow. The pop method needs some way of telling us that this has happened. In java we use the java.util.EmptyStackException 5
  • 6. Implementing a Stack • There are two ways we can implement a stack: – Using an array – Using a linked list 6
  • 7. Implementing a Stack • Implementing a stack using an array is fairly easy. – The bottom of the stack is at data[0] – The top of the stack is at data[numItems-1] – push onto the stack at data[numItems] – pop off of the stack at data[numItems-1] 7
  • 8. Implementing a Stack • Implementing a stack using a linked list isn’t that bad either… – Store the items in the stack in a linked list – The top of the stack is the head node, the bottom of the stack is the end of the list – push by adding to the front of the list – pop by removing from the front of the list 8
  • 9. Reversing a Word • We can use a stack to reverse the letters in a word. • How? 9
  • 10. Reversing a Word • Read each letter in the word and push it onto the stack • When you reach the end of the word, pop the letters off the stack and print them out. 10