Data Structures 
Stack 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 array 
You 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 Complexity 
string(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 plate 
Ever 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 Peek 
isEmpty : Check if the queue is empty 
Push : Push another element at the back 
Pop : Pop out the element at the front 
Peek : Return element at front
Demo #3 
Queue implementation using arrays
Approach 
Push : Need to check for overflow 
Pop/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 -> full 
Push -> end = (end + 1)%size 
Pop -> front = (front + 1)%size
Demo #4 
Queue implementation using Linked List
Approach 
Push : No erroneous case 
Pop/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 stack 
Note: Stack is empty while we check top. 
You are responsible for your stack.
Using STL 
Using STL queue 
Note: 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

Lecture 7 & 8: Stack & queue

  • 1.
    Data Structures Stackand Queue Charanjit Ghai B.Tech , CSE
  • 2.
    Pre-Requisites You mustknow what an array is and how to access the ith element in an array You must know what a linked list is and how to implement the basic operations in a Linked List
  • 3.
    Motivation ? Sometimesthe 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 Complexity string(char array) -> O(n) Singly Linked List -> O(n^2)
  • 4.
    What is aStack ? 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 plate Ever used a bucket for carrying clothes for washing ? The cloth you push at last on the bucket, comes out first. A Stack Of Plates
  • 5.
    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
  • 6.
    How to implementa 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
  • 7.
    Demo #1 Stackimplementation using arrays
  • 8.
    Approach Need tohave 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
  • 9.
    Step #1 StackDefinition ?
  • 10.
    Step #2 Initializethe stack ?
  • 11.
    Step #3 isEmptyfunction ?
  • 12.
    Step #4 PushFunction ?
  • 13.
    Step #5 PopFunction ? Note: Can we use any function here ?
  • 14.
    Step #6 PeekFunction ?
  • 15.
    Phew !! Lets’Test it now
  • 16.
    Demo #2 Stackimplementation using Linked List
  • 17.
    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 ?
  • 18.
    Step #1 StackDefinition ?
  • 19.
    Step #2 StackInitialization ?
  • 20.
  • 21.
    Step #4 PushOperation ? 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!!
  • 22.
    Step #4 PushOperation ? required
  • 23.
    Step #5 PopOperation ?
  • 24.
    Step #6 PeekOperation ?
  • 25.
    Phew !! Lets’Test it now
  • 26.
    What is aQueue ? 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
  • 27.
    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
  • 28.
    How to implementa queue then ? You must have 4 functions for the queue: isEmpty , Push , Pop and Peek isEmpty : Check if the queue is empty Push : Push another element at the back Pop : Pop out the element at the front Peek : Return element at front
  • 29.
    Demo #3 Queueimplementation using arrays
  • 30.
    Approach Push :Need to check for overflow Pop/Peek : Need to check if queue is empty ?
  • 31.
    Step #1 QueueDefinition ?
  • 32.
    Step #2 QueueInitialization ?
  • 33.
    Step #3 isEmptyFunction ?
  • 34.
    Step #4 PushOperation ?
  • 35.
    Step #5 PopOperation ?
  • 36.
    Step #6 PeekOperation ?
  • 37.
    Phew !! Lets’Test it now
  • 38.
    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 -> full Push -> end = (end + 1)%size Pop -> front = (front + 1)%size
  • 39.
    Demo #4 Queueimplementation using Linked List
  • 40.
    Approach Push :No erroneous case Pop/Peek : Need to check if queue is empty ?
  • 41.
    Step #1 QueueDefinition ?
  • 42.
    Step #2 QueueInitialization ?
  • 43.
    Step #3 isEmptyFunction?
  • 44.
    Step #4 PushOperation ?
  • 45.
    Step #5 PopOperation ?
  • 46.
    Step #6 PeekOperation ?
  • 47.
    Phew !! Lets’Test it now
  • 48.
    Using STL UsingSTL stack Note: Stack is empty while we check top. You are responsible for your stack.
  • 49.
    Using STL UsingSTL queue Note: Queue is empty while we check front. You are responsible for your queue.
  • 50.
    Problem: Check ifa 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.
  • 51.