0% found this document useful (0 votes)
3 views27 pages

Week6 Stack Uisng Queue

The document discusses the implementation of stack and queue data structures using each other, highlighting the differences between FIFO (queue) and LIFO (stack) operations. It outlines the steps to implement a stack using two queues and a queue using two stacks, detailing the enqueue and dequeue operations along with their time complexities. The document serves as a guide for understanding how to manipulate these data structures effectively in programming.

Uploaded by

logify99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Week6 Stack Uisng Queue

The document discusses the implementation of stack and queue data structures using each other, highlighting the differences between FIFO (queue) and LIFO (stack) operations. It outlines the steps to implement a stack using two queues and a queue using two stacks, detailing the enqueue and dequeue operations along with their time complexities. The document serves as a guide for understanding how to manipulate these data structures effectively in programming.

Uploaded by

logify99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Stack using

Queues
Queue using Stack

Department of Computer Science and Software


Engineering
Capital University of Sciences & Technology,
Islamabad Pakistan
CS2143 Data Structures
Stack using Queues

 We have to implement functionalities of stack


data structure using queue data structure

 Queue is a FIFO – first in first out data


structure.
 Stack is a LIFO – last in first out data
structure.
Operations of Stacks VS
Queues
• For an empty queue set rear
• For an empty stack set and front to -1
top to -1 • isempty() : Return true if
• isempty() : Return true stack is empty, return false
if stack is empty, return otherwise
false otherwise • isfull(): returns full if the
• Is full(): returns full if queue is full
the stack is full • Peek(): Returns the
element at front
• top() : Return top
• dequeue() : Remove an
element of stack
element from the front of
• pop() : Remove the top the queue
element from the stack • enqueue(x) : Add element
• push(x) : Add element x x at the rear of the queue
at the top of the stack
CS2143 Data Structures
Steps to implement a Stack
using Queue:
Steps to implement a push() method:

1. Using two queues primary_queue &


secondary_queue.
2. enqueue the element in secondary_queue.
3. While primary_queue not empty, enqueue the
element at front of primary_queue to the
secondary_queue and dequeue that element from the
primary_queue.
4. Swap primary_queue with secondary_queue.
Implementation of the stack using
Queue
Example
Cont…
Cont…
Cont…
Cont…
Implementation:: Stack
using Queues
Build in Queues
Queue Using Stack

 To implement a queue, we can follow two


approaches:

 By making the enqueue operation costly


 By making the dequeue operation costly
Queue Using Stack: Enqueue
operation costly
1. Using two stacks primary_stack &
secondary_stack.
2. Push the element in primary_stack.
3. If the primary_stack(queue) is not empty, move
all the elements present in the
primary_stack(S1) to the
secondary_stack(S2), one by one. Then add the
new element to the primary_stack, then move
back all the elements from the
secondary_stack back to the primary_stack.
4. Doing so will always maintain the right order of
the elements in the stack, with the 1st data
element staying always at the top, with 2nd
data element right below it and the new data
element will be added to the bottom.
Queue Using Stack: Enqueue
operation

costly
enQueue(q, x):
 While stack1 is not empty, push everything from
stack1 to stack2.
 Push x to stack1 (assuming size of stacks is
unlimited).
 Push everything back to stack1.
Here time complexity will be O(n)

 deQueue(q):
 If stack1 is empty then error
 Pop an item from stack1 and return it
Here time complexity will be O(1)
Queue Using Stack: Enqueue rear
operation
Create two stacks
1 2 3
0 1 2
Insert/Push 1,2,3 in to the stack front

Primary Stack Secondary Stack


Queue Using Stack: Enqueue rear
operation
1 2 3
0 1 2
front

Push(1)
in to primary
stack

Primary Stack Secondary Stack


Queue Using Stack: Enqueue rear
operation
1 2 3
0 1 2
front

Pop all the elements from


primary stack and push in
secondary stack then
push second element in to
the stack.

2 1 Pop() Primary Stack


Push(1) Secondary Stack

Primary Stack Secondary Stack


Push(2)
in to primary stack
Queue Using Stack: Enqueue rear

operation
1 2 3
0 1 2
front
Pop all the elements from
primary stack and push in
secondary stack then
push second element in to
the stack.

1 Pop() Primary Stack


Push(1) Secondary Stack
2
Push(2)
Primary Stack Secondary Stack in to primary stack
Pop() secondary Stack
Push(1) primary Stack
Queue Using Stack: Enqueue rear
operation
1 2 3
0 1 2
front
Pop all the elements from
primary stack and push in
secondary stack then
push second element in to
the stack.
2
Pop() Primary Stack
3 1
Push(1) Secondary Stack

Primary Stack Secondary Stack Push(3)


in to primary stack
Queue Using Stack: Enqueue rear

operation
Which element is dequeue from queue ? 1 2 3
Which element is pop from primary stack?
0 1 2
front
Pop all the elements from
primary stack and push in
secondary stack then
push second element in to
the stack.
1
Pop() Primary Stack
2
Push(1) Secondary Stack
3
Push(3)
in to primary stack
Primary Stack Secondary/temp
Stack Pop() secondary Stack
Push(2) primary Stack
Push(1) primary Stack
Queue Using Stack: Enqueue
operation costly
Queue Using Stack: Dequeue
operation costly
1. Using two stacks primary_stack &
secondary_stack.
2. Push the element in primary_stack.
3. If the primary_stack is not empty, move all the
elements present in the primary_stack(S1) to
the secondary_stack(S2), one by one. Then
remove the element at the top from the
secondary stack, and then move back all the
elements from the secondary_stack to the
primary_stack.
4. The purpose of moving all the elements present
in the primary stack to the secondary stack is to
reverse the order of the elements, because of
which the first element inserted into the
primary_stack(queue), is positioned at the top
Queue Using Stack: Dequeue
operation costly
 enQueue(q, x)

 1) Push x to stack1 (assuming size of stacks is


unlimited).
 Here time complexity will be O(1)

 deQueue(q)
 1) If both stacks are empty then error.
 2) If stack2 is empty
 While stack1 is not empty, push everything
from stack1 to stack2.
 3) Pop the element from stack2 and return it.
 Here time complexity will be O(n)
Queue Using Stack: Enqueue rear
operation
Create two stacks
Insert/Push 1,2,3 in to the stack 1 2 3
0 1 2
front

3 1

2 2

1 3

Primary Stack Secondary Stack


Queue Using Stack: Dequeue
operation
Queue Using Stack: Dequeue
operation costly

You might also like