Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Arab Academy for Science , Technology & Maritime Transport

College of Computing and Information Technology

Data Structures
and Algorithms (CS212)
Section 06 :
Queues
Eng. Mahmoud Osama Radwan
mhmoudko@msn.com – AAST CCIT
Queue ADT
 Queue:
A list with the restriction that insertion can be performed at one end (tail)
and deletion can be performed at the other end (head)

 FIFO: FIRST IN FIRST OUT

 Example of inserting :
1 , 2 , 3 , 4 , 5 to a queue =>

It can be implemented using


Arrays or Linked Lists
Queue Operations
 Initialize(q)
initializes a queue

 Add(q,x)
inserts an element to the queue
(in the tail)

 Remove(q,x,empty)
removes the first element and returns it as x ,
empty is a flag to indicate if the queue is empty
or not
Queue Operations

 Example:
head tail
• Add(q,1)
• Add(q,2) 1 2 3 4 5
• Add(q,3)
• Add(q,4)
• Add(q,5)
Queue Operations
x empty
• Remove(q,x,empty) 1 false
• Remove(q,x,empty) 2 false
• Remove(q,x,empty) 3 false
• Remove(q,x,empty) 4 false
• Remove(q,x,empty) 5 false
• Remove(q,x,empty) ? true
head tail

1 2 3 4 5
Print the Queue
 How to print all elements in the queue?
 It will be same as what we’ve done in
stacks with differences in names
head

1 2 3 4 5

tail
q
Print the Queue
 How to print all elements in the queue?
 First, create a temp queue (to save the original)
Initialize(t)
head

1 2 3 4 5

tail
q
head

tail
t
Print the Queue
 How to print all elements in the queue?
 Then, everytime remove an element & print it ,
then add it to the temp queue
Initialize(t)
head

tail
Output:
q Remove(s,x,empty)
while(!empty){ 1
head

1 2 3 4 5 tail print(x) 2
t Add(t,x) 3
Remove(s,x,empty) 4
5
}
Print the Queue
 After that restore the original queue
Initialize(t)
Remove(s,x,empty)
while(!empty){
print(x)
head

1 2 3 4 5

tail
Add(t,x)
q Remove(s,x,empty)
}
head

tail Remove(t,x,empty)
t while(!empty){
Add(s,x)
Remove(t,x,empty)
}
Count elements in the Queue
Initialize(t)

Remove(s,x,empty)
while(!empty){

Add(t,x)
Remove(s,x,empty)
}
Remove(t,x,empty)
while(!empty){
Add(s,x)
Remove(t,x,empty)
}
Count elements in the Queue
Initialize(t)
Counter = 0
Remove(s,x,empty)
while(!empty){
Counter++
Add(t,x)
Remove(s,x,empty)
}
Remove(t,x,empty)
while(!empty){
Add(s,x)
Remove(t,x,empty)
}
Return Counter
Stack Processing Vs. Queue
 So, Most of the processing like calculating
sum , avg , min , max , search , will be like this :
Initialize(t) Stack Initialize(t) Queue

Pop(s,x,empty) Remove(s,x,empty)
while(!empty){ while(!empty){
//PROCESSING_HERE //PROCESSING_HERE
Push(t,x) Add(t,x)
Pop(s,x,empty) Remove(s,x,empty)
} }
Pop(t,x,empty) Remove(t,x,empty)
while(!empty){ while(!empty){
Push(s,x) Add(s,x)
Pop(t,x,empty) Remove(t,x,empty)
} }
Calculate the Sum of elements
Sum = 0 Stack Sum = 0 Queue

Initialize(t) Initialize(t)
Pop(s,x,empty) Remove(s,x,empty)
while(!empty){ while(!empty){
Sum = Sum + x Sum = Sum + x
Push(t,x) Add(t,x)
Pop(s,x,empty) Remove(s,x,empty)
} }
Pop(t,x,empty) Remove(t,x,empty)
while(!empty){ while(!empty){
Push(s,x) Add(s,x)
Pop(t,x,empty) Remove(t,x,empty)
} }
Return Sum Return Sum
Calc Sum of even elements
Sum = 0 Stack Sum = 0 Queue

Initialize(t) Initialize(t)
Pop(s,x,empty) Remove(s,x,empty)
while(!empty){ while(!empty){
if(x%2==0){ if(x%2==0){
Sum = Sum + x} Sum = Sum + x}
Push(t,x) Add(t,x)
Pop(s,x,empty) Remove(s,x,empty)
} }
Pop(t,x,empty) Remove(t,x,empty)
while(!empty){ while(!empty){
Push(s,x) Add(s,x)
Pop(t,x,empty) Remove(t,x,empty)
} }
Return Sum Return Sum
Find the max
Initialize(t) Stack Initialize(t) Queue

Pop(s,x,empty) Remove(s,x,empty)
Max = x Max = x
while(!empty){ while(!empty){
if(x > Max){ if(x > Max){
Max=x} Max=x}
Push(t,x) Add(t,x)
Pop(s,x,empty) Remove(s,x,empty)
} }
Pop(t,x,empty) Remove(t,x,empty)
while(!empty){ while(!empty){
Push(s,x) Add(s,x)
Pop(t,x,empty) Remove(t,x,empty)
} }
Return Max Return Max
Find the min
Initialize(t) Stack Initialize(t) Queue

Pop(s,x,empty) Remove(s,x,empty)
min = x min = x
while(!empty){ while(!empty){
if(x < min){ if(x < min){
min =x} min =x}
Push(t,x) Add(t,x)
Pop(s,x,empty) Remove(s,x,empty)
} }
Pop(t,x,empty) Remove(t,x,empty)
while(!empty){ while(!empty){
Push(s,x) Add(s,x)
Pop(t,x,empty) Remove(t,x,empty)
} }
Return min Return min
Delete the nth element
Initialize(t) Stack Initialize(t) Queue

Pop(s,x,empty) Int count = 1


while(!empty){ Remove(q,x,empty)
Push(t,x) while(!empty){
Pop(s,x,empty) if(count != n){
} Add(s,x)
Int count = 1 } count++;
Pop(t,x,empty) Remove(q,x,empty)
while(!empty){ }
if(count != n){ Remove(t,x,empty)
Push(s,x) while(!empty){
} count++; Add(t,x,empty)
Pop(t,x,empty) Remove(q,x,empty)
} }
Swap the first with the last in a queue
 How we can swap the first element and the
last element in a queue ?
head

head
1 2 3 4 5 5 2 3 4 1

tail

tail
?

q q
Swap the first with the last in a queue
 First , we will need 2 temp stacks , one to save
the values , the other to restore the queue
Initialize(ts1)
head

1 2 3 4 5

tail
Initialize(ts2)
q

ts1 ts2
Swap the first with the last in a queue
 Then, Remove the first element and save it on
a temp variable (First)
Initialize(ts1)
head

2 3 4 5

tail
Initialize(ts2)
q Remove(q,x,empty)
First First = x

ts1 ts2
Swap the first with the last in a queue
 After that , move the elements from the queue
to the temp stack 1
Initialize(ts1)
head

tail
Initialize(ts2)
q Remove(q,x,empty)
First First = x
5 Remove(q,x,empty)
1 4 while(!empty){
Push(ts1,x)
3 Remove(q,x,empty)
2 }
ts1 ts2
Swap the first with the last in a queue
 Now we can access the last element , pop &
save it in a temp variable (Last)
Initialize(ts1)
head

tail
Initialize(ts2)
q Remove(q,x,empty)
First First = x
Remove(q,x,empty)
1 4 while(!empty){
Push(ts1,x)
Last
3 Remove(q,x,empty)
2 }
5 ts1 ts2
Pop(ts1,x,empty)
Last = x
Swap the first with the last in a queue
 Then, move the elements Initialize(ts1)
from ts1 to ts2 to avoid Initialize(ts2)
reversing the queue Remove(q,x,empty)
First = x
Remove(q,x,empty)
head

tail
while(!empty){
q Push(ts1,x)
First Remove(q,x,empty)
}
1 2 Pop(ts1,x,empty)
Last = x
Last
3 Pop(ts1,x,empty)
while(!empty){
4
5 ts1 ts2
Push(ts2,x)
Pop(ts1,x,empty)
}
Swap the first with the last in a queue
 Finally , add (Last) to the queue
.
.
head

5 Add(q,Last)

tail
q

First
1 2
3
4
ts1 ts2
Swap the first with the last in a queue
 Then, move the elements from ts2 to the queue
.
.
head

5 2 3 4 Add(q,Last)

tail
q Pop(ts2,x,empty)
while(!empty){
First
Add(q,x)
1 Pop(ts2,x,empty)
}

ts1 ts2
Swap the first with the last in a queue
 Then, add (First) to the queue
.
.
head

5 2 3 4 1 Add(q,Last)

tail
q Pop(ts2,x,empty)
while(!empty){
Add(q,x)
Pop(ts2,x,empty)
}
Add(q,First)

ts1 ts2
Initialize(ts1)
Initialize(ts2)

Remove(q,x,empty)
First = x
Remove(q,x,empty)
Add(q,Last)
while(!empty){
Push(ts1,x) Pop(ts2,x,empty)
Remove(q,x,empty) while(!empty){
} Add(q,x)
Pop(ts2,x,empty)
Pop(ts1,x,empty) }
Last = x Add(q,First)
Pop(ts1,x,empty)
while(!empty){
Push(ts2,x)
Pop(ts1,x,empty)
}
Section Homework
 In Stacks & Queues ,
Create functions for the following : Submit it to
sum , avg , min , max , mhmoudko@msn.com
search , delete the nth element (Pseudo-code)

 Check if 2 Stacks are reverse to each other

 Extract even values


from a queue to
another queue
END of Section
Thank You 
Eng. Mahmoud Osama Radwan
mhmoudko@msn.com – AAST CCIT

You might also like