Stack & queue

Download Stack & queue

Post on 17-Jul-2015

82 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

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