lecture 7 & 8: stack & queue

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

Upload: vivek-bhargav

Post on 23-Jun-2015

91 views

Category:

Engineering


1 download

DESCRIPTION

This slide is part of course designed for placement preparation for students of IIT Guwahati.

TRANSCRIPT

Page 1: Lecture 7 & 8: Stack  & queue

Data StructuresStack and Queue

Charanjit Ghai B.Tech , CSE

Page 2: Lecture 7 & 8: Stack  & queue

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

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

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

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

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

Demo #1

Stack implementation using arrays

Page 8: Lecture 7 & 8: Stack  & queue

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

Step #1

Stack Definition ?

Page 10: Lecture 7 & 8: Stack  & queue

Step #2

Initialize the stack ?

Page 11: Lecture 7 & 8: Stack  & queue

Step #3

isEmpty function ?

Page 12: Lecture 7 & 8: Stack  & queue

Step #4

Push Function ?

Page 13: Lecture 7 & 8: Stack  & queue

Step #5

Pop Function ?Note: Can we use any function here ?

Page 14: Lecture 7 & 8: Stack  & queue

Step #6

Peek Function ?

Page 15: Lecture 7 & 8: Stack  & queue

Phew !! Lets’ Test it now

Page 16: Lecture 7 & 8: Stack  & queue

Demo #2

Stack implementation using Linked List

Page 17: Lecture 7 & 8: Stack  & queue

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

Step #1

Stack Definition ?

Page 19: Lecture 7 & 8: Stack  & queue

Step #2

Stack Initialization ?

Page 20: Lecture 7 & 8: Stack  & queue

Step #3

isEmpty Function

Page 21: Lecture 7 & 8: Stack  & queue

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

Step #4

Push Operation ?

required

Page 23: Lecture 7 & 8: Stack  & queue

Step #5

Pop Operation ?

Page 24: Lecture 7 & 8: Stack  & queue

Step #6

Peek Operation ?

Page 25: Lecture 7 & 8: Stack  & queue

Phew !! Lets’ Test it now

Page 26: Lecture 7 & 8: Stack  & queue

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

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

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

Demo #3

Queue implementation using arrays

Page 30: Lecture 7 & 8: Stack  & queue

Approach

Push : Need to check for overflowPop/Peek : Need to check if queue is empty ?

Page 31: Lecture 7 & 8: Stack  & queue

Step #1

Queue Definition ?

Page 32: Lecture 7 & 8: Stack  & queue

Step #2

Queue Initialization ?

Page 33: Lecture 7 & 8: Stack  & queue

Step #3

isEmpty Function ?

Page 34: Lecture 7 & 8: Stack  & queue

Step #4

Push Operation ?

Page 35: Lecture 7 & 8: Stack  & queue

Step #5

Pop Operation ?

Page 36: Lecture 7 & 8: Stack  & queue

Step #6

Peek Operation ?

Page 37: Lecture 7 & 8: Stack  & queue

Phew !! Lets’ Test it now

Page 38: Lecture 7 & 8: Stack  & queue

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

Demo #4

Queue implementation using Linked List

Page 40: Lecture 7 & 8: Stack  & queue

Approach

Push : No erroneous casePop/Peek : Need to check if queue is empty ?

Page 41: Lecture 7 & 8: Stack  & queue

Step #1

Queue Definition ?

Page 42: Lecture 7 & 8: Stack  & queue

Step #2

Queue Initialization ?

Page 43: Lecture 7 & 8: Stack  & queue

Step #3

isEmpty Function?

Page 44: Lecture 7 & 8: Stack  & queue

Step #4

Push Operation ?

Page 45: Lecture 7 & 8: Stack  & queue

Step #5

Pop Operation ?

Page 46: Lecture 7 & 8: Stack  & queue

Step #6

Peek Operation ?

Page 47: Lecture 7 & 8: Stack  & queue

Phew !! Lets’ Test it now

Page 48: Lecture 7 & 8: Stack  & queue

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

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

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

Thank You