Stack & queue

51
Data Structures Stack and Queue Charanjit Ghai B.Tech , CSE

Transcript of Stack & queue

Data StructuresStack 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 arrayYou 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 Complexitystring(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 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

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 PeekisEmpty : Check if the queue is emptyPush : Push another element at the backPop : Pop out the element at the frontPeek : Return element at front

Demo #3

Queue implementation using arrays

Approach

Push : Need to check for overflowPop/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 -> fullPush -> end = (end + 1)%sizePop -> front = (front + 1)%size

Demo #4

Queue implementation using Linked List

Approach

Push : No erroneous casePop/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 stackNote: Stack is empty while we check top.You are responsible for your stack.

Using STL

Using STL queueNote: 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