lecture 7 & 8: stack & queue
Post on 23-Jun-2015
Embed Size (px)
DESCRIPTIONThis slide is part of course designed for placement preparation for students of IIT Guwahati.
- 1. Data StructuresStack and QueueCharanjit GhaiB.Tech , CSE
2. Pre-RequisitesYou must know what an array is and how toaccess the ith element in an arrayYou must know what a linked list is and how toimplement the basic operations in a LinkedList 3. Motivation ?Sometimes the running time of our algorithm(as in Order notation) greatly varies accordingto how we store and retrieve our data.Example ?Palindrome Checking using start and endindices. Time Complexitystring(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 ontothe top. We can Remove the top mostplate. We can See the top most plateEver used a bucket for carryingclothes for washing ? The cloth youpush at last on the bucket, comesout first.A Stack Of Plates 5. Stack in ProgrammingSimilar 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 emptyPush : Push another element onto stackPop : Pop the top most element out of stackPeek : Return the top most element 7. Demo #1Stack implementation using arrays 8. ApproachNeed to have a struct for stack having an arrayand 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 #1Stack Definition ? 10. Step #2Initialize the stack ? 11. Step #3isEmpty function ? 12. Step #4Push Function ? 13. Step #5Pop Function ?Note: Can we use any function here ? 14. Step #6Peek Function ? 15. Phew !! Lets Test it now 16. Demo #2Stack implementation using Linked List 17. ApproachisEmpty : Condition for stack being empty.Push : No need to check for bounds. Can addany number of elements until it fits inmemory.Pop/Peek : Need to check if stack is empty ? 18. Step #1Stack Definition ? 19. Step #2Stack Initialization ? 20. Step #3isEmpty Function 21. Step #4Push Operation ?If we insert at the end, then each push operation will takeO(n) time. ouch!!Solution : We can use the tail pointer as well, and updateit accordingly. Then, push will be O(1).But, now pop operation will take O(n) time, since forpopping you will need to update tail pointer and we donthave prev pointer in node struct.Solution : Why not insert at head instead. In that casepush (insert at head) , pop (delete head) , and peek (gethead value) will be O(1)). Yess!! 22. Step #4Push Operation ?required 23. Step #5Pop Operation ? 24. Step #6Peek 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 thequeue The person at Front may deal withthe person in-chargeEver stood in a queue for dosa ?A Queue of People 27. Queue in ProgrammingSimilar 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 PeekisEmpty : Check if the queue is emptyPush : Push another element at the backPop : Pop out the element at the frontPeek : Return element at front 29. Demo #3Queue implementation using arrays 30. ApproachPush : Need to check for overflowPop/Peek : Need to check if queue is empty ? 31. Step #1Queue Definition ? 32. Step #2Queue Initialization ? 33. Step #3isEmpty Function ? 34. Step #4Push Operation ? 35. Step #5Pop Operation ? 36. Step #6Peek Operation ? 37. Phew !! Lets Test it now 38. Limitations and SolutionsNote : once an element is popped, we areunable to use its space.Solution: Use Circular queue:front == end -> empty(end + 1)%size == front -> fullPush -> end = (end + 1)%sizePop -> front = (front + 1)%size 39. Demo #4Queue implementation using Linked List 40. ApproachPush : No erroneous casePop/Peek : Need to check if queue is empty ? 41. Step #1Queue Definition ? 42. Step #2Queue Initialization ? 43. Step #3isEmpty Function? 44. Step #4Push Operation ? 45. Step #5Pop Operation ? 46. Step #6Peek Operation ? 47. Phew !! Lets Test it now 48. Using STLUsing STL stackNote: Stack is empty while we check top.You are responsible for your stack. 49. Using STLUsing STL queueNote: Queue is empty while we check front.You are responsible for your queue. 50. Problem:Check if a string is palindrome or not usingonly one pass through the string and only oneindex variable. You are allowed to use onestack and one queue.P.S.: I know the constraints do not make muchsense, but I think we got the same problem inCS 101 End Sem. 51. Thank You