SlideShare a Scribd company logo
1 of 51
Data Structures 
Stack and Queue 
Charanjit Ghai 
B.Tech , CSE
Pre-Requisites 
You must know what an array is and how to 
access the ith element in an array 
You must know what a linked list is and how to 
implement the basic operations in a Linked 
List
Motivation ? 
Sometimes the running time of our algorithm 
(as in Order notation) greatly varies according 
to how we store and retrieve our data. 
Example ? 
Palindrome Checking using start and end 
indices. Time Complexity 
string(char array) -> O(n) 
Singly Linked List -> O(n^2)
What is a Stack ? 
What all stuff can we do with this ? 
● We can Add one more plate onto 
the top. 
● We can Remove the top most 
plate. 
● We can See the top most plate 
Ever used a bucket for carrying 
clothes for washing ? The cloth you 
push at last on the bucket, comes 
out first. 
A Stack Of Plates
Stack in Programming 
Similar is the idea of stack in Programming 
● You can add an element onto the stack 
● You can remove the top most element 
● You can see the top most element
How to implement a stack then ? 
You must have 4 functions for the stack: 
isEmpty , Push , Pop and Peek(Top) 
isEmpty : Checks if the stack is empty 
Push : Push another element onto stack 
Pop : Pop the top most element out of stack 
Peek : Return the top most element
Demo #1 
Stack implementation using arrays
Approach 
Need to have a struct for stack having an array 
and top_of_stack variable. 
isEmpty : Condition for stack being empty. 
Push : Need to check for “over” push. 
Pop : Need to check for “over” pop. 
Peek : Need to check if stack is empty ? 
Confused ? Lets’ go step by step
Step #1 
Stack Definition ?
Step #2 
Initialize the stack ?
Step #3 
isEmpty function ?
Step #4 
Push Function ?
Step #5 
Pop Function ? 
Note: Can we use any function here ?
Step #6 
Peek Function ?
Phew !! Lets’ Test it now
Demo #2 
Stack implementation using Linked List
Approach 
isEmpty : Condition for stack being empty. 
Push : No need to check for bounds. Can add 
any number of elements until it fits in 
memory. 
Pop/Peek : Need to check if stack is empty ?
Step #1 
Stack Definition ?
Step #2 
Stack Initialization ?
Step #3 
isEmpty Function
Step #4 
Push Operation ? 
If we insert at the end, then each push operation will take 
O(n) time. ouch!! 
Solution : We can use the tail pointer as well, and update 
it accordingly. Then, push will be O(1). 
But, now pop operation will take O(n) time, since for 
popping you will need to update tail pointer and we don’t 
have prev pointer in node struct. 
Solution : Why not insert at head instead. In that case 
push (insert at head) , pop (delete head) , and peek (get 
head value) will be O(1)). Yess!!
Step #4 
Push Operation ? 
required
Step #5 
Pop Operation ?
Step #6 
Peek Operation ?
Phew !! Lets’ Test it now
What is a Queue ? 
What all stuff is possible in the queue ? 
● Another person may join the queue 
● The person at Front may leave the 
queue 
● The person at Front may deal with 
the person in-charge 
Ever stood in a queue for dosa ? 
A Queue of People
Queue in Programming 
Similar is the idea of queue in Programming 
● You can add an element at the back 
● You can remove an element from the front 
● You can see the element in front
How to implement a queue then ? 
You must have 4 functions for the queue: 
isEmpty , Push , Pop and Peek 
isEmpty : Check if the queue is empty 
Push : Push another element at the back 
Pop : Pop out the element at the front 
Peek : Return element at front
Demo #3 
Queue implementation using arrays
Approach 
Push : Need to check for overflow 
Pop/Peek : Need to check if queue is empty ?
Step #1 
Queue Definition ?
Step #2 
Queue Initialization ?
Step #3 
isEmpty Function ?
Step #4 
Push Operation ?
Step #5 
Pop Operation ?
Step #6 
Peek Operation ?
Phew !! Lets’ Test it now
Limitations and Solutions 
Note : once an element is popped, we are 
unable to use its’ space. 
Solution: Use Circular queue: 
front == end -> empty 
(end + 1)%size == front -> full 
Push -> end = (end + 1)%size 
Pop -> front = (front + 1)%size
Demo #4 
Queue implementation using Linked List
Approach 
Push : No erroneous case 
Pop/Peek : Need to check if queue is empty ?
Step #1 
Queue Definition ?
Step #2 
Queue Initialization ?
Step #3 
isEmpty Function?
Step #4 
Push Operation ?
Step #5 
Pop Operation ?
Step #6 
Peek Operation ?
Phew !! Lets’ Test it now
Using STL 
Using STL stack 
Note: Stack is empty while we check top. 
You are responsible for your stack.
Using STL 
Using STL queue 
Note: Queue is empty while we check front. 
You are responsible for your queue.
Problem: 
Check if a string is palindrome or not using 
only one pass through the string and only one 
index variable. You are allowed to use one 
stack and one queue. 
P.S.: I know the constraints do not make much 
sense, but I think we got the same problem in 
CS 101 End Sem.
Thank You

More Related Content

What's hot

What's hot (20)

Stack
StackStack
Stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
stack
stackstack
stack
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Stacks
StacksStacks
Stacks
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Stack application
Stack applicationStack application
Stack application
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 
computer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfixcomputer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfix
 
Pop operation
Pop operationPop operation
Pop operation
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
stack presentation
stack presentationstack presentation
stack presentation
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 

Similar to Lecture 7 & 8: Stack & queue

Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptxskilljiolms
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
stack and queue array implementation, java.
stack and queue array implementation, java.stack and queue array implementation, java.
stack and queue array implementation, java.CIIT Atd.
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queueSunipa Bera
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptxDdushb
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEswathirajstar
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdfTobyWtf
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 

Similar to Lecture 7 & 8: Stack & queue (20)

Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
Stacks
StacksStacks
Stacks
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
stack and queue array implementation, java.
stack and queue array implementation, java.stack and queue array implementation, java.
stack and queue array implementation, java.
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
Stack
StackStack
Stack
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Lecture9_StackQueue.ppt
Lecture9_StackQueue.pptLecture9_StackQueue.ppt
Lecture9_StackQueue.ppt
 
StackQueue.ppt
StackQueue.pptStackQueue.ppt
StackQueue.ppt
 

More from Vivek Bhargav

Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sortingVivek Bhargav
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heapsVivek Bhargav
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2Vivek Bhargav
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basicsVivek Bhargav
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked listVivek Bhargav
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsVivek Bhargav
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: FunctionsVivek Bhargav
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationVivek Bhargav
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointersVivek Bhargav
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntaxVivek Bhargav
 

More from Vivek Bhargav (10)

Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 

Lecture 7 & 8: Stack & queue

  • 1. Data Structures Stack and Queue Charanjit Ghai B.Tech , CSE
  • 2. Pre-Requisites You must know what an array is and how to access the ith element in an array You must know what a linked list is and how to implement the basic operations in a Linked List
  • 3. Motivation ? Sometimes the running time of our algorithm (as in Order notation) greatly varies according to how we store and retrieve our data. Example ? Palindrome Checking using start and end indices. Time Complexity string(char array) -> O(n) Singly Linked List -> O(n^2)
  • 4. What is a Stack ? What all stuff can we do with this ? ● We can Add one more plate onto the top. ● We can Remove the top most plate. ● We can See the top most plate Ever used a bucket for carrying clothes for washing ? The cloth you push at last on the bucket, comes out first. A Stack Of Plates
  • 5. Stack in Programming Similar is the idea of stack in Programming ● You can add an element onto the stack ● You can remove the top most element ● You can see the top most element
  • 6. How to implement a stack then ? You must have 4 functions for the stack: isEmpty , Push , Pop and Peek(Top) isEmpty : Checks if the stack is empty Push : Push another element onto stack Pop : Pop the top most element out of stack Peek : Return the top most element
  • 7. Demo #1 Stack implementation using arrays
  • 8. Approach Need to have a struct for stack having an array and top_of_stack variable. isEmpty : Condition for stack being empty. Push : Need to check for “over” push. Pop : Need to check for “over” pop. Peek : Need to check if stack is empty ? Confused ? Lets’ go step by step
  • 9. Step #1 Stack Definition ?
  • 10. Step #2 Initialize the stack ?
  • 11. Step #3 isEmpty function ?
  • 12. Step #4 Push Function ?
  • 13. Step #5 Pop Function ? Note: Can we use any function here ?
  • 14. Step #6 Peek Function ?
  • 15. Phew !! Lets’ Test it now
  • 16. Demo #2 Stack implementation using Linked List
  • 17. Approach isEmpty : Condition for stack being empty. Push : No need to check for bounds. Can add any number of elements until it fits in memory. Pop/Peek : Need to check if stack is empty ?
  • 18. Step #1 Stack Definition ?
  • 19. Step #2 Stack Initialization ?
  • 20. Step #3 isEmpty Function
  • 21. Step #4 Push Operation ? If we insert at the end, then each push operation will take O(n) time. ouch!! Solution : We can use the tail pointer as well, and update it accordingly. Then, push will be O(1). But, now pop operation will take O(n) time, since for popping you will need to update tail pointer and we don’t have prev pointer in node struct. Solution : Why not insert at head instead. In that case push (insert at head) , pop (delete head) , and peek (get head value) will be O(1)). Yess!!
  • 22. Step #4 Push Operation ? required
  • 23. Step #5 Pop Operation ?
  • 24. Step #6 Peek Operation ?
  • 25. Phew !! Lets’ Test it now
  • 26. What is a Queue ? What all stuff is possible in the queue ? ● Another person may join the queue ● The person at Front may leave the queue ● The person at Front may deal with the person in-charge Ever stood in a queue for dosa ? A Queue of People
  • 27. Queue in Programming Similar is the idea of queue in Programming ● You can add an element at the back ● You can remove an element from the front ● You can see the element in front
  • 28. How to implement a queue then ? You must have 4 functions for the queue: isEmpty , Push , Pop and Peek isEmpty : Check if the queue is empty Push : Push another element at the back Pop : Pop out the element at the front Peek : Return element at front
  • 29. Demo #3 Queue implementation using arrays
  • 30. Approach Push : Need to check for overflow Pop/Peek : Need to check if queue is empty ?
  • 31. Step #1 Queue Definition ?
  • 32. Step #2 Queue Initialization ?
  • 33. Step #3 isEmpty Function ?
  • 34. Step #4 Push Operation ?
  • 35. Step #5 Pop Operation ?
  • 36. Step #6 Peek Operation ?
  • 37. Phew !! Lets’ Test it now
  • 38. Limitations and Solutions Note : once an element is popped, we are unable to use its’ space. Solution: Use Circular queue: front == end -> empty (end + 1)%size == front -> full Push -> end = (end + 1)%size Pop -> front = (front + 1)%size
  • 39. Demo #4 Queue implementation using Linked List
  • 40. Approach Push : No erroneous case Pop/Peek : Need to check if queue is empty ?
  • 41. Step #1 Queue Definition ?
  • 42. Step #2 Queue Initialization ?
  • 43. Step #3 isEmpty Function?
  • 44. Step #4 Push Operation ?
  • 45. Step #5 Pop Operation ?
  • 46. Step #6 Peek Operation ?
  • 47. Phew !! Lets’ Test it now
  • 48. Using STL Using STL stack Note: Stack is empty while we check top. You are responsible for your stack.
  • 49. Using STL Using STL queue Note: Queue is empty while we check front. You are responsible for your queue.
  • 50. Problem: Check if a string is palindrome or not using only one pass through the string and only one index variable. You are allowed to use one stack and one queue. P.S.: I know the constraints do not make much sense, but I think we got the same problem in CS 101 End Sem.