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)

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

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
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

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
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

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