SlideShare a Scribd company logo
1 of 206
Download to read offline
Data Structures
Queues
Andres Mendez-Vazquez
May 6, 2015
1 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
2 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
3 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
2
4 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
2
3
5 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
2
6 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
2
4
7 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
2
8 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
9 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
5
10 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
5
6
11 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
5
12 / 116
Images/cinvestav-
Imagine the following
Stack Order
1
2
3 4
5
6 1
13 / 116
Images/cinvestav-
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a dierent order?
14 / 116
Images/cinvestav-
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a dierent order?
14 / 116
Images/cinvestav-
Level Order
How?
1
2
3 4
5
6
15 / 116
Images/cinvestav-
Queues
Denition of Queues
A queue is a abstract data structure that models/enforces the
rst-come rst-serve order, or equivalently the First-In First-Out
(FIFO) order.
Thus
Using ADT Which is the rst thing that comes to your mind to implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-
Queues
Denition of Queues
A queue is a abstract data structure that models/enforces the
rst-come rst-serve order, or equivalently the First-In First-Out
(FIFO) order.
Thus
Using ADT Which is the rst thing that comes to your mind to implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-
Queues
Denition of Queues
A queue is a abstract data structure that models/enforces the
rst-come rst-serve order, or equivalently the First-In First-Out
(FIFO) order.
Thus
Using ADT Which is the rst thing that comes to your mind to implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-
Denition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-
Denition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-
Denition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-
Example
Enqueue - Put
A B C D E
18 / 116
Images/cinvestav-
Example
Enqueue - Put
A B C D E
19 / 116
Images/cinvestav-
Example
Enqueue - Put
A B C D
E
20 / 116
Images/cinvestav-
Example
Enqueue - Put
A B C
DE
21 / 116
Images/cinvestav-
Example
Enqueue - Put
A B
CDE
22 / 116
Images/cinvestav-
Example
Enqueue - Put
A
BCDE
23 / 116
Images/cinvestav-
Example
Enqueue - Put
BCDE A
24 / 116
Images/cinvestav-
Example
Dequeue - Remove
BCD
E
A
25 / 116
Images/cinvestav-
Example
Dequeue - Remove
BC
D E
A
26 / 116
Images/cinvestav-
Example
Dequeue - Remove
B
C D E
A
27 / 116
Images/cinvestav-
Example
Dequeue - Remove
B C D E
A
28 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
29 / 116
Images/cinvestav-
Queue Interface
Code
p u b l i c i n t e r f a c e QueueItem {
p u b l i c b o o l e a n empty ( ) ;
p u b l i c I t e m f r o n t ( ) ;
p u b l i c I t e m r e a r ( ) ;
p u b l i c I t e m Dequeue ( ) ;
p u b l i c v o i d Enqueue ( I t e m t h e O b j e c t ) ;
p u b l i c i n t s i z e ( ) ;
}
30 / 116
Images/cinvestav-
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
public Item front()
Return the value of the item at the font of the queue without
removing it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
BC A
public Item front()
Return the value of the item at the font of the queue without
removing it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
public Item front()
Return the value of the item at the font of the queue without
removing it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
Remove the item from the front of the queue.
Precondition: The queue is not empty.
Postcondition: The element at the front of the queue is the element that
was added immediately after the element just popped or the
queue is empty.
33 / 116
Images/cinvestav-
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
Remove the item from the front of the queue.
Precondition: The queue is not empty.
Postcondition: The element at the front of the queue is the element that
was added immediately after the element just popped or the
queue is empty.
33 / 116
Images/cinvestav-
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
Remove the item from the front of the queue.
Precondition: The queue is not empty.
Postcondition: The element at the front of the queue is the element that
was added immediately after the element just popped or the
queue is empty.
33 / 116
Images/cinvestav-
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
Remove the item from the front of the queue.
Precondition: The queue is not empty.
Postcondition: The element at the front of the queue is the element that
was added immediately after the element just popped or the
queue is empty.
33 / 116
Images/cinvestav-
Explanation
Example
B
C
A
Dequeue()
1
public int size()
It returns the size.
34 / 116
Images/cinvestav-
Explanation
Example
B
C
A
Dequeue()
1
public int size()
It returns the size.
34 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
36 / 116
Images/cinvestav-
Change the Order of Recursion
Remember
1
2
3 4
5
6
37 / 116
Images/cinvestav-
Thus, Using a Trick
and Queue
We can change the direction of the recursion!!!
Look at the board
38 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
39 / 116
Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-
Example
Pass 0: Distribute the digits into bins according to the 1's digit (10
0
).
0 1 2 3 4 5 6 7 8 9
41 / 116
Images/cinvestav-
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-
|
43 / 116
Images/cinvestav-
Code
We need something else
p u b l i c s t a t i c R a d i x S o r t ( L i n e a r L i s t Element , i n t k ) {
i n t R a d i x = 10
i n t power = 1
i n t d i g i t ;
QueueI n t e g e r  [ ] d i g i t Q u e u e = ( QueueI n t e g e r  [ ] )
new Queue [ 1 0 ] ;
f o r ( i n t i =0; i k ; i ++)
{
f o r ( i n t j =0; j  E l e m e n t . s i z e ( ) ; j ++){
d i g i t = E l e m e n t . remove ( 0 ) ;
d i g i t Q u e u e [ ( d i g i t / power ) % 1 0 ] .
e n q u e u e ( d i g i t ) ;
}
f o r ( i n t j =0; j  R a d i x ; j ++){
// WHAT?
}
power ∗= 1 0 ;
}
}
43 / 116
Images/cinvestav-
Radix Sort: Complexity
Lemma 1
Given n d-digit numbers in which each digit can take on up to k possible
values, RADIX-SORT correctly sorts these numbers in O(d(n +k)) time.
44 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
45 / 116
Images/cinvestav-
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (real) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (real) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (real) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer ticks, from 0
At each tick
Frequent yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent nishes, then serves next FF passenger
2 Agent nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-
Thus
Desired Output
Statistics on waiting times, agent idle time, etc.
Optionally, a detailed trace
48 / 116
Images/cinvestav-
Thus
Desired Output
Statistics on waiting times, agent idle time, etc.
Optionally, a detailed trace
48 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
49 / 116
Images/cinvestav-
Example
Circuit Board
50 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable squares 1 unit from start
51 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable unlabeled squares 2 units from
start.
52 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable unlabeled squares 3 units from
start.
53 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable unlabeled squares 4 units from
start.
54 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable unlabeled squares 5 units from
start.
55 / 116
Images/cinvestav-
Example
Circuit Board - Label all reachable unlabeled squares 6 units from
start.
56 / 116
Images/cinvestav-
Example
Circuit Board - Traceback.
57 / 116
Images/cinvestav-
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
59 / 116
Images/cinvestav-
Derive from ArrayLinearList
Here
We do not extend our data structure.
Simply use
Whatever is available in the base class.
60 / 116
Images/cinvestav-
Derive from ArrayLinearList
Here
We do not extend our data structure.
Simply use
Whatever is available in the base class.
60 / 116
Images/cinvestav-
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-
Moral of the Story
We have
to perform each operation in O(1) time (excluding array doubling),
we need a customized array representation.
We need to extend the data structure
We can do that using the circular idea!!!
63 / 116
Images/cinvestav-
Moral of the Story
We have
to perform each operation in O(1) time (excluding array doubling),
we need a customized array representation.
We need to extend the data structure
We can do that using the circular idea!!!
63 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
64 / 116
Images/cinvestav-
What if we derive directly from the Chain List
We have
What about the operations?
We might decide that the rst node is the front and the last node is
the rear!!!
65 / 116
Images/cinvestav-
What if we derive directly from the Chain List
We have
What about the operations?
We might decide that the rst node is the front and the last node is
the rear!!!
65 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
Images/cinvestav-
Not Good At ALL
Even
If we change the front and the rear we do not get the performance we
want!!!
67 / 116
Images/cinvestav-
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
Images/cinvestav-
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
Images/cinvestav-
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
Images/cinvestav-
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
Images/cinvestav-
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
Images/cinvestav-
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
72 / 116
Images/cinvestav-
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
firstNode lastNode
73 / 116
Images/cinvestav-
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-
The Code For this Implementation
Sketch of the Code - Question: What is the problem with private
class Node?
p u b l i c c l a s s LinkedQueueItem i m p l e m e n t s QueueItem
{
p r i v a t e Node f i r s t N o d e ;
p r i v a t e Node l a s t N o d e ;
p r i v a t e i n t s i z e ;
p u b l i c L i n k e d Q u e u e ( ) {
f i r s t N o d e = n u l l ;
l a s t N o d e = n u l l ;
s i z e = 0 ;
} // end d e f a u l t c o n s t r u c t o r
p r i v a t e c l a s s Node
{
p r i v a t e I t e m d a t a ; // entry in queue
p r i v a t e Node n e x t ; // l i n k to next node
} // end Node
} // end LinkedQueue
74 / 116
Images/cinvestav-
Yes!!!
With private elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-
Yes!!!
With private elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-
Yes!!!
With private elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-
Some Operations: Enqueue
We have always two cases
1 Adding to an empty Queue
2 Adding to a non-empty Queue
76 / 116
Images/cinvestav-
Some Operations: Enqueue
We have always two cases
1 Adding to an empty Queue
2 Adding to a non-empty Queue
76 / 116
Images/cinvestav-
Example: Adding to an Empty List
Before Inserting
firstNode lastNode
newNode
After Inserting
77 / 116
Images/cinvestav-
Example: Adding to an Empty List
Before Inserting
firstNode lastNode
newNode
After Inserting
firstNode lastNode
77 / 116
Images/cinvestav-
Example: Adding to a Non-Empty List
Create New Node
firstNode
lastNode
newNode
Make next from lastNode...
78 / 116
Images/cinvestav-
Example: Adding to a Non-Empty List
Create New Node
firstNode
lastNode
newNode
Make next from lastNode...
firstNode
lastNode
newNode
78 / 116
Images/cinvestav-
Example: Adding to a Non-Empty List
Move lastNode
firstNode
lastNode
newNode
79 / 116
Images/cinvestav-
Final Code
Code
p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y )
{
Node newNode = new Node ( newEntry , n u l l ) ;
i f ( empty ( ) )
f i r s t N o d e = newNode ;
e l s e
l a s t N o d e . s e t N e x t N o d e ( newNode ) ;
l a s t N o d e = newNode ;
} // end enqueue
80 / 116
Images/cinvestav-
What about Dequeue?
Point a temporary node to the front node
firstNode lastNode
temp
Point rstNode to temp.next
81 / 116
Images/cinvestav-
What about Dequeue?
Point a temporary node to the front node
firstNode lastNode
temp
Point rstNode to temp.next
firstNode lastNode
temp
81 / 116
Images/cinvestav-
Empty
Make temp.next = null
firstNode lastNode
temp
Return Node
82 / 116
Images/cinvestav-
Empty
Make temp.next = null
firstNode lastNode
temp
Return Node
firstNode lastNode
temp
Return Node
82 / 116
Images/cinvestav-
Thus...
The Rest of Operations
You can think about them... they are not complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
83 / 116
Images/cinvestav-
Thus...
The Rest of Operations
You can think about them... they are not complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
83 / 116
Images/cinvestav-
From Scratch Using an Array!!!
If we can do the following...
first last
first
last
84 / 116
Images/cinvestav-
A closer Look...
Somebody can notice something?
first
last
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
85 / 116
Images/cinvestav-
Direction of Reading
Repeating numbers in a certain range
first
last
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
86 / 116
Images/cinvestav-
How we can simulate this number repetition?
Actually there is function that can help
mod m : N → {0,1,2,...,m −1} (1)
Example for m = 5
n n mod m
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
Images/cinvestav-
How we can simulate this number repetition?
Actually there is function that can help
mod m : N → {0,1,2,...,m −1} (1)
Example for m = 5
n n mod m
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
Images/cinvestav-
Thus, we still we have two indexes
frontIndex
We need to know where to remove!!!
backIndex
We need to know where to add!!
88 / 116
Images/cinvestav-
Thus, we still we have two indexes
frontIndex
We need to know where to remove!!!
backIndex
We need to know where to add!!
88 / 116
Images/cinvestav-
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
Images/cinvestav-
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
Images/cinvestav-
Example
Adding stu into the back
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
a
b
c d
e
90 / 116
Images/cinvestav-
Example
Adding stu into the back
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
a
b
c d
e
f
91 / 116
Images/cinvestav-
Example
Adding stu into the back
frontIndex
backIndex0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
a
b
c d
e
f
g
92 / 116
Images/cinvestav-
Example
Remove stu from the front
frontIndex
backIndex0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
b
c d
e
f
g
93 / 116
Images/cinvestav-
Example
Remove stu from the front
frontIndex
backIndex0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
94 / 116
Images/cinvestav-
Example
Keep Adding
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
95 / 116
Images/cinvestav-
Example
The modulo helps here
frontIndex
backIndex=16 0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
o
p
96 / 116
Images/cinvestav-
It looks Cool, but...
Problem 1 when full
frontIndex= 3
backIndex=18%16=2
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
o
p
q
s
97 / 116
Images/cinvestav-
But when we remove all elements
We go...
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15 h
i
j
kl
m
n
o
p
q
s
98 / 116
Images/cinvestav-
But when we remove all elements
We go...
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
n
o
p
q
s
99 / 116
Images/cinvestav-
This is the main problem
And here is the problem when removing s
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
s
100 / 116
Images/cinvestav-
This is the main problem
Then full an empty cases cannot be identied!!!
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
101 / 116
Images/cinvestav-
Thus
The Problem
frontIndex == (backIndex + 1) % queue.length
102 / 116
Images/cinvestav-
A possible solution
Somewhat simple
Use an extra eld size
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
Then do something what?
103 / 116
Images/cinvestav-
A possible solution
Somewhat simple
Use an extra eld size
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
Then do something what?
103 / 116
Images/cinvestav-
Another solution!!!
Assume an space between the frontIndex and backIndex
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
o
p
q
104 / 116
Images/cinvestav-
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
Images/cinvestav-
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
Images/cinvestav-
A solution!!!
Then for an empty queue
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
106 / 116
Images/cinvestav-
Class Members
Code
p u b l i c c l a s s ArrayQueueItem i m p l e m e n t s Q u e u e I n t e r f a c e Item
{
p r i v a t e I t e m [ ] queue ; // c i r c u l a r array of queue e n t r i e s
// and one unused l o c a t i o n
p r i v a t e i n t f r o n t I n d e x ;
p r i v a t e i n t b a c k I n d e x ;
p r i v a t e s t a t i c f i n a l i n t DEFAULT_INITIAL_CAPACITY = 5 0 ;
}
107 / 116
Images/cinvestav-
Basic Constructors
Code
p u b l i c A r r ay Q u eu e ( )
{
t h i s ( DEFAULT_INITIAL_CAPACITY ) ;
} // end d e f a u l t c o n s t r u c t o r
p u b l i c A r r ay Q u eu e ( i n t i n i t i a l C a p a c i t y )
{
// the cast i s s a f e because the new
// array c o n t a i n s n u l l e n t r i e s
// The 1 i s f o r empty entry
I t e m [ ] tempQueue = ( I t e m [ ] ) new O b j e c t [ i n i t i a l C a p a c i t y + 1 ] ;
queue = tempQueue ;
f r o n t I n d e x = 0 ;
b a c k I n d e x = i n i t i a l C a p a c i t y ;
} // end c o n s t r u c t o r
108 / 116
Images/cinvestav-
At the Beginning
At the Instantiation of the Object Queue
firstIndex==0
bastIndex==15
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
109 / 116
Images/cinvestav-
Enqueue in a circular array
Code
p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y )
{
i f ( f r o n t I n d e x == ( ( b a c k I n d e x + 2 ) % queue . l e n g t h ) )
{
S o m e t h i n g Here . . . . 
}
b a c k I n d e x = ( b a c k I n d e x + 1 ) % queue . l e n g t h ;
queue [ b a c k I n d e x ] = n e w E n t r y ;
} // end enqueue
110 / 116
Images/cinvestav-
What is this Something Here?
Expanding Array and Copying values
frontIndex
backIndex
0 1 2 3 4 6 7
0 1 2 3 4 6 7 8 9 11 12 13 14 15
3
1
5
COPY Empty Space
111 / 116
Images/cinvestav-
Dequeue
At the beginning
0 1 2 3 4 6 7
frontIndex
backIndex
2
6
112 / 116
Images/cinvestav-
Dequeue
Use a temporary pointer front
0 1 2 3 4 6 7
frontIndex
backIndex
2
6
front
to be returned
113 / 116
Images/cinvestav-
Dequeue
Remove the element
0 1 2 3 4 6 7
frontIndex
backIndex
3
6
front
to be returned
null
114 / 116
Images/cinvestav-
Enqueue in a circular array
Code
p u b l i c I t e m d e q u e u e ( )
{
I t e m f r o n t = n u l l ;
i f ( ! empty ( ) )
{
f r o n t = queue [ f r o n t I n d e x ] ;
queue [ f r o n t I n d e x ] = n u l l ;
f r o n t I n d e x = ( f r o n t I n d e x + 1 ) % queue . l e n g t h ;
} // End I f
r e t u r n f r o n t ;
} // end dequeue
115 / 116
Images/cinvestav-
Thus...
The Rest of Operations
You can think about them... they are not so complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
116 / 116
Images/cinvestav-
Thus...
The Rest of Operations
You can think about them... they are not so complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
116 / 116

More Related Content

What's hot

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryIlio Catallo
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)Sanjay Saha
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Netguru
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURESUsha Mahalingam
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...La Cuisine du Web
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 

What's hot (20)

Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Arrays
ArraysArrays
Arrays
 
Pointers [compatibility mode]
Pointers [compatibility mode]Pointers [compatibility mode]
Pointers [compatibility mode]
 
Arrays
ArraysArrays
Arrays
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 
L7 pointers
L7 pointersL7 pointers
L7 pointers
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
05 queues
05 queues05 queues
05 queues
 
Arrays
ArraysArrays
Arrays
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...
Apprentissage statistique et analyse prédictive en Python avec scikit-learn p...
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 

Viewers also liked

Viewers also liked (7)

Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queues
QueuesQueues
Queues
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 

Similar to Preparation Data Structures 08 queues

queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9ecomputernotes
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problemecomputernotes
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptOliverKane3
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 

Similar to Preparation Data Structures 08 queues (20)

queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
stack & queue
stack & queuestack & queue
stack & queue
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
queues.pptx
queues.pptxqueues.pptx
queues.pptx
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
2 b queues
2 b queues2 b queues
2 b queues
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

Preparation Data Structures 08 queues

  • 2. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 2 / 116
  • 3. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 3 / 116
  • 4. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 2 4 / 116
  • 5. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 2 3 5 / 116
  • 6. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 2 6 / 116
  • 7. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 2 4 7 / 116
  • 8. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 2 8 / 116
  • 9. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 9 / 116
  • 10. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 5 10 / 116
  • 11. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 5 6 11 / 116
  • 12. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 5 12 / 116
  • 13. Images/cinvestav- Imagine the following Stack Order 1 2 3 4 5 6 1 13 / 116
  • 14. Images/cinvestav- Recursion ≈ Depth First Search Actually This is the classic order when recursion is done!!! What if... We need a dierent order? 14 / 116
  • 15. Images/cinvestav- Recursion ≈ Depth First Search Actually This is the classic order when recursion is done!!! What if... We need a dierent order? 14 / 116
  • 17. Images/cinvestav- Queues Denition of Queues A queue is a abstract data structure that models/enforces the rst-come rst-serve order, or equivalently the First-In First-Out (FIFO) order. Thus Using ADT Which is the rst thing that comes to your mind to implement a queue? IMPORTANT IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT) 16 / 116
  • 18. Images/cinvestav- Queues Denition of Queues A queue is a abstract data structure that models/enforces the rst-come rst-serve order, or equivalently the First-In First-Out (FIFO) order. Thus Using ADT Which is the rst thing that comes to your mind to implement a queue? IMPORTANT IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT) 16 / 116
  • 19. Images/cinvestav- Queues Denition of Queues A queue is a abstract data structure that models/enforces the rst-come rst-serve order, or equivalently the First-In First-Out (FIFO) order. Thus Using ADT Which is the rst thing that comes to your mind to implement a queue? IMPORTANT IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT) 16 / 116
  • 20. Images/cinvestav- Denition We have then A linear list. Entry Points One end is called front. Other end is called rear. Insertion and Deletions Additions are done at the rear only. Removals are made from the front only. 17 / 116
  • 21. Images/cinvestav- Denition We have then A linear list. Entry Points One end is called front. Other end is called rear. Insertion and Deletions Additions are done at the rear only. Removals are made from the front only. 17 / 116
  • 22. Images/cinvestav- Denition We have then A linear list. Entry Points One end is called front. Other end is called rear. Insertion and Deletions Additions are done at the rear only. Removals are made from the front only. 17 / 116
  • 34. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 29 / 116
  • 35. Images/cinvestav- Queue Interface Code p u b l i c i n t e r f a c e QueueItem { p u b l i c b o o l e a n empty ( ) ; p u b l i c I t e m f r o n t ( ) ; p u b l i c I t e m r e a r ( ) ; p u b l i c I t e m Dequeue ( ) ; p u b l i c v o i d Enqueue ( I t e m t h e O b j e c t ) ; p u b l i c i n t s i z e ( ) ; } 30 / 116
  • 36. Images/cinvestav- Explanation public boolean empty() Check whether the queue is empty. Return TRUE if it is empty and FALSE otherwise. Example public Item front() Return the value of the item at the font of the queue without removing it. Precondition: The queue is not empty. 31 / 116
  • 37. Images/cinvestav- Explanation public boolean empty() Check whether the queue is empty. Return TRUE if it is empty and FALSE otherwise. Example BC A public Item front() Return the value of the item at the font of the queue without removing it. Precondition: The queue is not empty. 31 / 116
  • 38. Images/cinvestav- Explanation public boolean empty() Check whether the queue is empty. Return TRUE if it is empty and FALSE otherwise. Example public Item front() Return the value of the item at the font of the queue without removing it. Precondition: The queue is not empty. 31 / 116
  • 39. Images/cinvestav- Explanation Example BC AC public Item rear() Return the value of the item at the rear of the queue without removing it. Precondition: The queue is not empty. public void Enqueue(Item theObject) Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back 32 / 116
  • 40. Images/cinvestav- Explanation Example BC AC public Item rear() Return the value of the item at the rear of the queue without removing it. Precondition: The queue is not empty. public void Enqueue(Item theObject) Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back 32 / 116
  • 41. Images/cinvestav- Explanation Example BC AC public Item rear() Return the value of the item at the rear of the queue without removing it. Precondition: The queue is not empty. public void Enqueue(Item theObject) Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back 32 / 116
  • 42. Images/cinvestav- Explanation Example BC A 1Enqueue( ) 1 public Item Dequeue() Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. 33 / 116
  • 43. Images/cinvestav- Explanation Example BC A 1Enqueue( ) 1 public Item Dequeue() Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. 33 / 116
  • 44. Images/cinvestav- Explanation Example BC A 1Enqueue( ) 1 public Item Dequeue() Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. 33 / 116
  • 45. Images/cinvestav- Explanation Example BC A 1Enqueue( ) 1 public Item Dequeue() Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. 33 / 116
  • 48. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 49. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 50. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 51. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 52. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 53. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 54. Images/cinvestav- Applications of Queues Direct applications Waiting lists Queue Theory for Networking Bureaucracy Access to shared resources (e.g., printer) Multiprogramming Schedulers Indirect applications Auxiliary data structure for algorithms Component of other data structures 35 / 116
  • 55. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 36 / 116
  • 56. Images/cinvestav- Change the Order of Recursion Remember 1 2 3 4 5 6 37 / 116
  • 57. Images/cinvestav- Thus, Using a Trick and Queue We can change the direction of the recursion!!! Look at the board 38 / 116
  • 58. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 39 / 116
  • 59. Images/cinvestav- Radix Sort Using Bins Example Order ten 2 digit numbers in 10 bins (0-9) from least signicative number to most signicative number. Digits 91,06,85,15,92,35,30,22,39 Let us do it In the board!!! 40 / 116
  • 60. Images/cinvestav- Radix Sort Using Bins Example Order ten 2 digit numbers in 10 bins (0-9) from least signicative number to most signicative number. Digits 91,06,85,15,92,35,30,22,39 Let us do it In the board!!! 40 / 116
  • 61. Images/cinvestav- Radix Sort Using Bins Example Order ten 2 digit numbers in 10 bins (0-9) from least signicative number to most signicative number. Digits 91,06,85,15,92,35,30,22,39 Let us do it In the board!!! 40 / 116
  • 62. Images/cinvestav- Example Pass 0: Distribute the digits into bins according to the 1's digit (10 0 ). 0 1 2 3 4 5 6 7 8 9 41 / 116
  • 63. Images/cinvestav- Finally Next Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. Next Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1) Finally Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. 42 / 116
  • 64. Images/cinvestav- Finally Next Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. Next Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1) Finally Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. 42 / 116
  • 65. Images/cinvestav- Finally Next Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. Next Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1) Finally Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. 42 / 116
  • 66. Images/cinvestav- Finally Next Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. Next Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1) Finally Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. 42 / 116
  • 67. Images/cinvestav- Finally Next Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. Next Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1) Finally Dequeue the values from the queue 0 to queue 9. Put values in a list in that order. 42 / 116
  • 69. Images/cinvestav- Code We need something else p u b l i c s t a t i c R a d i x S o r t ( L i n e a r L i s t Element , i n t k ) { i n t R a d i x = 10 i n t power = 1 i n t d i g i t ; QueueI n t e g e r [ ] d i g i t Q u e u e = ( QueueI n t e g e r [ ] ) new Queue [ 1 0 ] ; f o r ( i n t i =0; i k ; i ++) { f o r ( i n t j =0; j E l e m e n t . s i z e ( ) ; j ++){ d i g i t = E l e m e n t . remove ( 0 ) ; d i g i t Q u e u e [ ( d i g i t / power ) % 1 0 ] . e n q u e u e ( d i g i t ) ; } f o r ( i n t j =0; j R a d i x ; j ++){ // WHAT? } power ∗= 1 0 ; } } 43 / 116
  • 70. Images/cinvestav- Radix Sort: Complexity Lemma 1 Given n d-digit numbers in which each digit can take on up to k possible values, RADIX-SORT correctly sorts these numbers in O(d(n +k)) time. 44 / 116
  • 71. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 45 / 116
  • 72. Images/cinvestav- Simulating Waiting Lines Simulation is used to study the performance Of a physical (real) system. By using a physical, mathematical, or computer model of the system. Simulation allows designers to estimate performance Before building a system Simulation can lead to design improvements Giving better expected performance of the system 46 / 116
  • 73. Images/cinvestav- Simulating Waiting Lines Simulation is used to study the performance Of a physical (real) system. By using a physical, mathematical, or computer model of the system. Simulation allows designers to estimate performance Before building a system Simulation can lead to design improvements Giving better expected performance of the system 46 / 116
  • 74. Images/cinvestav- Simulating Waiting Lines Simulation is used to study the performance Of a physical (real) system. By using a physical, mathematical, or computer model of the system. Simulation allows designers to estimate performance Before building a system Simulation can lead to design improvements Giving better expected performance of the system 46 / 116
  • 75. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 76. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 77. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 78. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 79. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 80. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 81. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 82. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 83. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 84. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 85. Images/cinvestav- Queuing Theory Constraints We will maintain a simulated clock Counts in integer ticks, from 0 At each tick Frequent yer (FF) passenger arrives in line Regular (R) passenger arrives in line One agent with priorities 1 Agent nishes, then serves next FF passenger 2 Agent nishes, then serves next R passenger Agent is idle (both lines empty) Using some other constraints Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time 47 / 116
  • 86. Images/cinvestav- Thus Desired Output Statistics on waiting times, agent idle time, etc. Optionally, a detailed trace 48 / 116
  • 87. Images/cinvestav- Thus Desired Output Statistics on waiting times, agent idle time, etc. Optionally, a detailed trace 48 / 116
  • 88. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 49 / 116
  • 90. Images/cinvestav- Example Circuit Board - Label all reachable squares 1 unit from start 51 / 116
  • 91. Images/cinvestav- Example Circuit Board - Label all reachable unlabeled squares 2 units from start. 52 / 116
  • 92. Images/cinvestav- Example Circuit Board - Label all reachable unlabeled squares 3 units from start. 53 / 116
  • 93. Images/cinvestav- Example Circuit Board - Label all reachable unlabeled squares 4 units from start. 54 / 116
  • 94. Images/cinvestav- Example Circuit Board - Label all reachable unlabeled squares 5 units from start. 55 / 116
  • 95. Images/cinvestav- Example Circuit Board - Label all reachable unlabeled squares 6 units from start. 56 / 116
  • 97. Images/cinvestav- What Implementations Directly Extending Classes From ArrayLinearList Note: Not a so good idea!!! Extending from class but adding some pointers From Scratch Circular Array 58 / 116
  • 98. Images/cinvestav- What Implementations Directly Extending Classes From ArrayLinearList Note: Not a so good idea!!! Extending from class but adding some pointers From Scratch Circular Array 58 / 116
  • 99. Images/cinvestav- What Implementations Directly Extending Classes From ArrayLinearList Note: Not a so good idea!!! Extending from class but adding some pointers From Scratch Circular Array 58 / 116
  • 100. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 59 / 116
  • 101. Images/cinvestav- Derive from ArrayLinearList Here We do not extend our data structure. Simply use Whatever is available in the base class. 60 / 116
  • 102. Images/cinvestav- Derive from ArrayLinearList Here We do not extend our data structure. Simply use Whatever is available in the base class. 60 / 116
  • 103. Images/cinvestav- Derive from ArrayLinearList We have then Front Rear When the front is the left end of list and the rear is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(0) O(1) rear() get(size()-1) O(1) Enqueue(TheObject) add(size(),theObject) O(1) Dequeue() remove(0) O(size) 61 / 116
  • 104. Images/cinvestav- Derive from ArrayLinearList We have then Front Rear When the front is the left end of list and the rear is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(0) O(1) rear() get(size()-1) O(1) Enqueue(TheObject) add(size(),theObject) O(1) Dequeue() remove(0) O(size) 61 / 116
  • 105. Images/cinvestav- Derive from ArrayLinearList We have then Front Rear When the front is the left end of list and the rear is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(0) O(1) rear() get(size()-1) O(1) Enqueue(TheObject) add(size(),theObject) O(1) Dequeue() remove(0) O(size) 61 / 116
  • 106. Images/cinvestav- Shift the front and rear pointers!!! We have Front Rear When the rear is the left end of list and the front is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(size()-1) O(1) rear() get(0) O(1) Enqueue(TheObject) add(0,theObject) O(size) Dequeue() remove(size()-1) O(1) 62 / 116
  • 107. Images/cinvestav- Shift the front and rear pointers!!! We have Front Rear When the rear is the left end of list and the front is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(size()-1) O(1) rear() get(0) O(1) Enqueue(TheObject) add(0,theObject) O(size) Dequeue() remove(size()-1) O(1) 62 / 116
  • 108. Images/cinvestav- Shift the front and rear pointers!!! We have Front Rear When the rear is the left end of list and the front is the right end Operation in Queue Supporting method from parent class Complexity empty() super.isEmpty() O(1) front() get(size()-1) O(1) rear() get(0) O(1) Enqueue(TheObject) add(0,theObject) O(size) Dequeue() remove(size()-1) O(1) 62 / 116
  • 109. Images/cinvestav- Moral of the Story We have to perform each operation in O(1) time (excluding array doubling), we need a customized array representation. We need to extend the data structure We can do that using the circular idea!!! 63 / 116
  • 110. Images/cinvestav- Moral of the Story We have to perform each operation in O(1) time (excluding array doubling), we need a customized array representation. We need to extend the data structure We can do that using the circular idea!!! 63 / 116
  • 111. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 64 / 116
  • 112. Images/cinvestav- What if we derive directly from the Chain List We have What about the operations? We might decide that the rst node is the front and the last node is the rear!!! 65 / 116
  • 113. Images/cinvestav- What if we derive directly from the Chain List We have What about the operations? We might decide that the rst node is the front and the last node is the rear!!! 65 / 116
  • 114. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 115. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 116. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 117. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 118. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 119. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 120. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 121. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 122. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 123. Images/cinvestav- We have the following Operations Queue.empty() = super.isEmpty() O(1) time front() = get(0) No problem here O(1) time What about these ones rear() = get(size()-1) O(size) time Enqueue(theObject) = add(0,TheObject) O(1) time Dequeue() = remove(size()-1) O(size) time 66 / 116
  • 124. Images/cinvestav- Not Good At ALL Even If we change the front and the rear we do not get the performance we want!!! 67 / 116
  • 125. Images/cinvestav- Better Derive From Chain We need to extend the data structure To have another pointer to the last node!!! We need to add other methods to the extended class getLast() This returns the element pointed by the lastNode append(TheObject) append elements at the end of the list 68 / 116
  • 126. Images/cinvestav- Better Derive From Chain We need to extend the data structure To have another pointer to the last node!!! We need to add other methods to the extended class getLast() This returns the element pointed by the lastNode append(TheObject) append elements at the end of the list 68 / 116
  • 127. Images/cinvestav- Better Derive From Chain We need to extend the data structure To have another pointer to the last node!!! We need to add other methods to the extended class getLast() This returns the element pointed by the lastNode append(TheObject) append elements at the end of the list 68 / 116
  • 128. Images/cinvestav- Better Derive From Chain We need to extend the data structure To have another pointer to the last node!!! We need to add other methods to the extended class getLast() This returns the element pointed by the lastNode append(TheObject) append elements at the end of the list 68 / 116
  • 129. Images/cinvestav- Better Derive From Chain We need to extend the data structure To have another pointer to the last node!!! We need to add other methods to the extended class getLast() This returns the element pointed by the lastNode append(TheObject) append elements at the end of the list 68 / 116
  • 130. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end Queue.empty() = super.isEmpty() O(1) time front() = get(0) O(1) time 69 / 116
  • 131. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end Queue.empty() = super.isEmpty() O(1) time front() = get(0) O(1) time 69 / 116
  • 132. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end Queue.empty() = super.isEmpty() O(1) time front() = get(0) O(1) time 69 / 116
  • 133. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end Queue.empty() = super.isEmpty() O(1) time front() = get(0) O(1) time 69 / 116
  • 134. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end Queue.empty() = super.isEmpty() O(1) time front() = get(0) O(1) time 69 / 116
  • 135. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 136. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 137. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 138. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 139. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 140. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 141. Images/cinvestav- Derive From Chain We have When the front is the left end of list and the rear is the right end rear() = getLast() . . . new method O(1) time Enqueue(theObject) = append(theObject) . . . new method O(1) time Dequeue() = remove(0) O(1) time 70 / 116
  • 142. Images/cinvestav- But even with this implementation Problems We have do still some code that does not belong to the ADT queue!!! Example For example the idea of index checking!!! Moral of the Story Better to implement from scratch... when possible!!! 71 / 116
  • 143. Images/cinvestav- But even with this implementation Problems We have do still some code that does not belong to the ADT queue!!! Example For example the idea of index checking!!! Moral of the Story Better to implement from scratch... when possible!!! 71 / 116
  • 144. Images/cinvestav- But even with this implementation Problems We have do still some code that does not belong to the ADT queue!!! Example For example the idea of index checking!!! Moral of the Story Better to implement from scratch... when possible!!! 71 / 116
  • 145. Images/cinvestav- Outline 1 A little bit more about Recursion 2 The Queues The Queue Interface 3 Basic Applications Change the Order of Recursion Radix Sort Simulating Waiting Lines Wire Routing 4 Implementation Derive From ArrayLinearList Derive From Chain List From Scratch 72 / 116
  • 146. Images/cinvestav- A Linked Implementation of a Queue From our experience extending the Chain Class We use two pointer for the front and the back of the chain: rstNode == at the beginning of the Chain lastNode == the end of the Chain Thus... 73 / 116
  • 147. Images/cinvestav- A Linked Implementation of a Queue From our experience extending the Chain Class We use two pointer for the front and the back of the chain: rstNode == at the beginning of the Chain lastNode == the end of the Chain Thus... firstNode lastNode 73 / 116
  • 148. Images/cinvestav- A Linked Implementation of a Queue From our experience extending the Chain Class We use two pointer for the front and the back of the chain: rstNode == at the beginning of the Chain lastNode == the end of the Chain Thus... 73 / 116
  • 149. Images/cinvestav- A Linked Implementation of a Queue From our experience extending the Chain Class We use two pointer for the front and the back of the chain: rstNode == at the beginning of the Chain lastNode == the end of the Chain Thus... 73 / 116
  • 150. Images/cinvestav- The Code For this Implementation Sketch of the Code - Question: What is the problem with private class Node? p u b l i c c l a s s LinkedQueueItem i m p l e m e n t s QueueItem { p r i v a t e Node f i r s t N o d e ; p r i v a t e Node l a s t N o d e ; p r i v a t e i n t s i z e ; p u b l i c L i n k e d Q u e u e ( ) { f i r s t N o d e = n u l l ; l a s t N o d e = n u l l ; s i z e = 0 ; } // end d e f a u l t c o n s t r u c t o r p r i v a t e c l a s s Node { p r i v a t e I t e m d a t a ; // entry in queue p r i v a t e Node n e x t ; // l i n k to next node } // end Node } // end LinkedQueue 74 / 116
  • 151. Images/cinvestav- Yes!!! With private elds It is necessary to have public methods to access them!!! setDataNode(Item Object) setNextNode(Node newNode); 75 / 116
  • 152. Images/cinvestav- Yes!!! With private elds It is necessary to have public methods to access them!!! setDataNode(Item Object) setNextNode(Node newNode); 75 / 116
  • 153. Images/cinvestav- Yes!!! With private elds It is necessary to have public methods to access them!!! setDataNode(Item Object) setNextNode(Node newNode); 75 / 116
  • 154. Images/cinvestav- Some Operations: Enqueue We have always two cases 1 Adding to an empty Queue 2 Adding to a non-empty Queue 76 / 116
  • 155. Images/cinvestav- Some Operations: Enqueue We have always two cases 1 Adding to an empty Queue 2 Adding to a non-empty Queue 76 / 116
  • 156. Images/cinvestav- Example: Adding to an Empty List Before Inserting firstNode lastNode newNode After Inserting 77 / 116
  • 157. Images/cinvestav- Example: Adding to an Empty List Before Inserting firstNode lastNode newNode After Inserting firstNode lastNode 77 / 116
  • 158. Images/cinvestav- Example: Adding to a Non-Empty List Create New Node firstNode lastNode newNode Make next from lastNode... 78 / 116
  • 159. Images/cinvestav- Example: Adding to a Non-Empty List Create New Node firstNode lastNode newNode Make next from lastNode... firstNode lastNode newNode 78 / 116
  • 160. Images/cinvestav- Example: Adding to a Non-Empty List Move lastNode firstNode lastNode newNode 79 / 116
  • 161. Images/cinvestav- Final Code Code p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y ) { Node newNode = new Node ( newEntry , n u l l ) ; i f ( empty ( ) ) f i r s t N o d e = newNode ; e l s e l a s t N o d e . s e t N e x t N o d e ( newNode ) ; l a s t N o d e = newNode ; } // end enqueue 80 / 116
  • 162. Images/cinvestav- What about Dequeue? Point a temporary node to the front node firstNode lastNode temp Point rstNode to temp.next 81 / 116
  • 163. Images/cinvestav- What about Dequeue? Point a temporary node to the front node firstNode lastNode temp Point rstNode to temp.next firstNode lastNode temp 81 / 116
  • 164. Images/cinvestav- Empty Make temp.next = null firstNode lastNode temp Return Node 82 / 116
  • 165. Images/cinvestav- Empty Make temp.next = null firstNode lastNode temp Return Node firstNode lastNode temp Return Node 82 / 116
  • 166. Images/cinvestav- Thus... The Rest of Operations You can think about them... they are not complex... Complexities Operation in Scratch Queue using Chains Complexity empty() O(1) front() O(1) rear() O(1) Enqueue(TheObject) O(1) Dequeue() O(1) 83 / 116
  • 167. Images/cinvestav- Thus... The Rest of Operations You can think about them... they are not complex... Complexities Operation in Scratch Queue using Chains Complexity empty() O(1) front() O(1) rear() O(1) Enqueue(TheObject) O(1) Dequeue() O(1) 83 / 116
  • 168. Images/cinvestav- From Scratch Using an Array!!! If we can do the following... first last first last 84 / 116
  • 169. Images/cinvestav- A closer Look... Somebody can notice something? first last 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 85 / 116
  • 170. Images/cinvestav- Direction of Reading Repeating numbers in a certain range first last 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 86 / 116
  • 171. Images/cinvestav- How we can simulate this number repetition? Actually there is function that can help mod m : N → {0,1,2,...,m −1} (1) Example for m = 5 n n mod m 0 0 1 1 2 2 3 3 4 4 5 0 6 1 7 2 8 3 etc... ... 87 / 116
  • 172. Images/cinvestav- How we can simulate this number repetition? Actually there is function that can help mod m : N → {0,1,2,...,m −1} (1) Example for m = 5 n n mod m 0 0 1 1 2 2 3 3 4 4 5 0 6 1 7 2 8 3 etc... ... 87 / 116
  • 173. Images/cinvestav- Thus, we still we have two indexes frontIndex We need to know where to remove!!! backIndex We need to know where to add!! 88 / 116
  • 174. Images/cinvestav- Thus, we still we have two indexes frontIndex We need to know where to remove!!! backIndex We need to know where to add!! 88 / 116
  • 175. Images/cinvestav- Thus If we want to add backIndex = (backIndex + 1)% queue.length If we want to remove frontIndex = (frontIndex + 1)% queue.length 89 / 116
  • 176. Images/cinvestav- Thus If we want to add backIndex = (backIndex + 1)% queue.length If we want to remove frontIndex = (frontIndex + 1)% queue.length 89 / 116
  • 177. Images/cinvestav- Example Adding stu into the back frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 a b c d e 90 / 116
  • 178. Images/cinvestav- Example Adding stu into the back frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 a b c d e f 91 / 116
  • 179. Images/cinvestav- Example Adding stu into the back frontIndex backIndex0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 a b c d e f g 92 / 116
  • 180. Images/cinvestav- Example Remove stu from the front frontIndex backIndex0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 b c d e f g 93 / 116
  • 181. Images/cinvestav- Example Remove stu from the front frontIndex backIndex0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 c d e f g 94 / 116
  • 183. Images/cinvestav- Example The modulo helps here frontIndex backIndex=16 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 c d e f g h i j kl m n o p 96 / 116
  • 184. Images/cinvestav- It looks Cool, but... Problem 1 when full frontIndex= 3 backIndex=18%16=2 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 c d e f g h i j kl m n o p q s 97 / 116
  • 185. Images/cinvestav- But when we remove all elements We go... frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 h i j kl m n o p q s 98 / 116
  • 186. Images/cinvestav- But when we remove all elements We go... frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 n o p q s 99 / 116
  • 187. Images/cinvestav- This is the main problem And here is the problem when removing s frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 s 100 / 116
  • 188. Images/cinvestav- This is the main problem Then full an empty cases cannot be identied!!! frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 101 / 116
  • 189. Images/cinvestav- Thus The Problem frontIndex == (backIndex + 1) % queue.length 102 / 116
  • 190. Images/cinvestav- A possible solution Somewhat simple Use an extra eld size Then each time If frontIndex == (backIndex + 1) % queue.length ⇒ check size Then do something what? 103 / 116
  • 191. Images/cinvestav- A possible solution Somewhat simple Use an extra eld size Then each time If frontIndex == (backIndex + 1) % queue.length ⇒ check size Then do something what? 103 / 116
  • 192. Images/cinvestav- Another solution!!! Assume an space between the frontIndex and backIndex frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 c d e f g h i j kl m n o p q 104 / 116
  • 193. Images/cinvestav- Thus, When the queue is full frontIndex == (backIndex + 2) % queue.length When the queue is empty frontIndex == (backIndex + 1) % queue.length 105 / 116
  • 194. Images/cinvestav- Thus, When the queue is full frontIndex == (backIndex + 2) % queue.length When the queue is empty frontIndex == (backIndex + 1) % queue.length 105 / 116
  • 195. Images/cinvestav- A solution!!! Then for an empty queue frontIndex backIndex 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 106 / 116
  • 196. Images/cinvestav- Class Members Code p u b l i c c l a s s ArrayQueueItem i m p l e m e n t s Q u e u e I n t e r f a c e Item { p r i v a t e I t e m [ ] queue ; // c i r c u l a r array of queue e n t r i e s // and one unused l o c a t i o n p r i v a t e i n t f r o n t I n d e x ; p r i v a t e i n t b a c k I n d e x ; p r i v a t e s t a t i c f i n a l i n t DEFAULT_INITIAL_CAPACITY = 5 0 ; } 107 / 116
  • 197. Images/cinvestav- Basic Constructors Code p u b l i c A r r ay Q u eu e ( ) { t h i s ( DEFAULT_INITIAL_CAPACITY ) ; } // end d e f a u l t c o n s t r u c t o r p u b l i c A r r ay Q u eu e ( i n t i n i t i a l C a p a c i t y ) { // the cast i s s a f e because the new // array c o n t a i n s n u l l e n t r i e s // The 1 i s f o r empty entry I t e m [ ] tempQueue = ( I t e m [ ] ) new O b j e c t [ i n i t i a l C a p a c i t y + 1 ] ; queue = tempQueue ; f r o n t I n d e x = 0 ; b a c k I n d e x = i n i t i a l C a p a c i t y ; } // end c o n s t r u c t o r 108 / 116
  • 198. Images/cinvestav- At the Beginning At the Instantiation of the Object Queue firstIndex==0 bastIndex==15 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 109 / 116
  • 199. Images/cinvestav- Enqueue in a circular array Code p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y ) { i f ( f r o n t I n d e x == ( ( b a c k I n d e x + 2 ) % queue . l e n g t h ) ) { S o m e t h i n g Here . . . . } b a c k I n d e x = ( b a c k I n d e x + 1 ) % queue . l e n g t h ; queue [ b a c k I n d e x ] = n e w E n t r y ; } // end enqueue 110 / 116
  • 200. Images/cinvestav- What is this Something Here? Expanding Array and Copying values frontIndex backIndex 0 1 2 3 4 6 7 0 1 2 3 4 6 7 8 9 11 12 13 14 15 3 1 5 COPY Empty Space 111 / 116
  • 201. Images/cinvestav- Dequeue At the beginning 0 1 2 3 4 6 7 frontIndex backIndex 2 6 112 / 116
  • 202. Images/cinvestav- Dequeue Use a temporary pointer front 0 1 2 3 4 6 7 frontIndex backIndex 2 6 front to be returned 113 / 116
  • 203. Images/cinvestav- Dequeue Remove the element 0 1 2 3 4 6 7 frontIndex backIndex 3 6 front to be returned null 114 / 116
  • 204. Images/cinvestav- Enqueue in a circular array Code p u b l i c I t e m d e q u e u e ( ) { I t e m f r o n t = n u l l ; i f ( ! empty ( ) ) { f r o n t = queue [ f r o n t I n d e x ] ; queue [ f r o n t I n d e x ] = n u l l ; f r o n t I n d e x = ( f r o n t I n d e x + 1 ) % queue . l e n g t h ; } // End I f r e t u r n f r o n t ; } // end dequeue 115 / 116
  • 205. Images/cinvestav- Thus... The Rest of Operations You can think about them... they are not so complex... Complexities Operation in Scratch Queue using Chains Complexity empty() O(1) front() O(1) rear() O(1) Enqueue(TheObject) O(1) Dequeue() O(1) 116 / 116
  • 206. Images/cinvestav- Thus... The Rest of Operations You can think about them... they are not so complex... Complexities Operation in Scratch Queue using Chains Complexity empty() O(1) front() O(1) rear() O(1) Enqueue(TheObject) O(1) Dequeue() O(1) 116 / 116