Download - Lecture 7 & 8: Stack & queue
![Page 1: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/1.jpg)
Data StructuresStack and Queue
Charanjit Ghai B.Tech , CSE
![Page 2: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/2.jpg)
Pre-Requisites
You must know what an array is and how to access the ith element in an arrayYou must know what a linked list is and how to implement the basic operations in a Linked List
![Page 3: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/3.jpg)
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 Complexitystring(char array) -> O(n)Singly Linked List -> O(n^2)
![Page 4: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/4.jpg)
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 plateEver used a bucket for carrying clothes for washing ? The cloth you push at last on the bucket, comes out first.
A Stack Of Plates
![Page 5: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/5.jpg)
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
![Page 6: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/6.jpg)
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
![Page 7: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/7.jpg)
Demo #1
Stack implementation using arrays
![Page 8: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/8.jpg)
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
![Page 9: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/9.jpg)
Step #1
Stack Definition ?
![Page 10: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/10.jpg)
Step #2
Initialize the stack ?
![Page 11: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/11.jpg)
Step #3
isEmpty function ?
![Page 12: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/12.jpg)
Step #4
Push Function ?
![Page 13: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/13.jpg)
Step #5
Pop Function ?Note: Can we use any function here ?
![Page 14: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/14.jpg)
Step #6
Peek Function ?
![Page 15: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/15.jpg)
Phew !! Lets’ Test it now
![Page 16: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/16.jpg)
Demo #2
Stack implementation using Linked List
![Page 17: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/17.jpg)
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 ?
![Page 18: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/18.jpg)
Step #1
Stack Definition ?
![Page 19: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/19.jpg)
Step #2
Stack Initialization ?
![Page 20: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/20.jpg)
Step #3
isEmpty Function
![Page 21: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/21.jpg)
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!!
![Page 22: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/22.jpg)
Step #4
Push Operation ?
required
![Page 23: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/23.jpg)
Step #5
Pop Operation ?
![Page 24: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/24.jpg)
Step #6
Peek Operation ?
![Page 25: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/25.jpg)
Phew !! Lets’ Test it now
![Page 26: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/26.jpg)
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
![Page 27: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/27.jpg)
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
![Page 28: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/28.jpg)
How to implement a queue then ?
You must have 4 functions for the queue:isEmpty , Push , Pop and PeekisEmpty : Check if the queue is emptyPush : Push another element at the backPop : Pop out the element at the frontPeek : Return element at front
![Page 29: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/29.jpg)
Demo #3
Queue implementation using arrays
![Page 30: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/30.jpg)
Approach
Push : Need to check for overflowPop/Peek : Need to check if queue is empty ?
![Page 31: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/31.jpg)
Step #1
Queue Definition ?
![Page 32: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/32.jpg)
Step #2
Queue Initialization ?
![Page 33: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/33.jpg)
Step #3
isEmpty Function ?
![Page 34: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/34.jpg)
Step #4
Push Operation ?
![Page 35: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/35.jpg)
Step #5
Pop Operation ?
![Page 36: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/36.jpg)
Step #6
Peek Operation ?
![Page 37: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/37.jpg)
Phew !! Lets’ Test it now
![Page 38: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/38.jpg)
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 -> fullPush -> end = (end + 1)%sizePop -> front = (front + 1)%size
![Page 39: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/39.jpg)
Demo #4
Queue implementation using Linked List
![Page 40: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/40.jpg)
Approach
Push : No erroneous casePop/Peek : Need to check if queue is empty ?
![Page 41: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/41.jpg)
Step #1
Queue Definition ?
![Page 42: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/42.jpg)
Step #2
Queue Initialization ?
![Page 43: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/43.jpg)
Step #3
isEmpty Function?
![Page 44: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/44.jpg)
Step #4
Push Operation ?
![Page 45: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/45.jpg)
Step #5
Pop Operation ?
![Page 46: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/46.jpg)
Step #6
Peek Operation ?
![Page 47: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/47.jpg)
Phew !! Lets’ Test it now
![Page 48: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/48.jpg)
Using STL
Using STL stackNote: Stack is empty while we check top.You are responsible for your stack.
![Page 49: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/49.jpg)
Using STL
Using STL queueNote: Queue is empty while we check front.You are responsible for your queue.
![Page 50: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/50.jpg)
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.
![Page 51: Lecture 7 & 8: Stack & queue](https://reader035.vdocuments.us/reader035/viewer/2022062319/55897f83d8b42a1a1d8b45e4/html5/thumbnails/51.jpg)
Thank You