Lect 7 Queue Using LLpptx
-
Upload
athanasious-ramsis -
Category
Documents
-
view
219 -
download
0
Transcript of Lect 7 Queue Using LLpptx
-
8/12/2019 Lect 7 Queue Using LLpptx
1/18
QUEUES USINGLINKED LIST
Lecture
7 Dr. Adil Yousif
-
8/12/2019 Lect 7 Queue Using LLpptx
2/18
Circular Arrays
Neat trick: use a circular arrayto insert and
remove items from a queue in constant time
The idea of a circular array is that the end of the
array wraps around to the start of the array0
1
3
2
4
5
6
7
-
8/12/2019 Lect 7 Queue Using LLpptx
3/18
The mod Operator
The modoperator (%) is used to calculateremainders: 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3
modcan be used to calculate the front and backpositions in a circular array, therefore avoidingcomparisons to the array size The back of the queue is:
(front + count - 1) % items.length
where countis the number of items currently in thequeue
After removing an item the front of the queue is: (front + 1) % items.length;
-
8/12/2019 Lect 7 Queue Using LLpptx
4/18
Array Queue Example
0 1 2 3 4 5
//Java Code
Queue q = newQueue();q.enqueue(6);
6
front= 0
insert item at (front + count) % items.length
count= 0
1
-
8/12/2019 Lect 7 Queue Using LLpptx
5/18
Array Queue Example
0 1 2 3 4 5
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
q.enqueue(8);
6
front= 0
4 7 3 8
count= 1
2
3
4
5
-
8/12/2019 Lect 7 Queue Using LLpptx
6/18
Array Queue Example
0 1 2 3 4 5
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
q.enqueue(8);
q.dequeue();//front = 1
q.dequeue();//front = 2
q.enqueue(9);
6
front= 0
4
make front = (0 + 1) % 6 = 1
1
7 3 8 9
count= 5
4
3
4
make front = (1 + 1) % 6 = 2
2
-
8/12/2019 Lect 7 Queue Using LLpptx
7/18
Array Queue Example
0 1 2 3 4 5
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
q.enqueue(8);
q.dequeue();//front = 1
q.dequeue();//front = 2
q.enqueue(9);q.enqueue(5);
front= 2
7 3 8 95
insert at (front + count) % 6
= (2 + 4) % 6 = 0
count= 45
-
8/12/2019 Lect 7 Queue Using LLpptx
8/18
Queue: Linked List
Implementation
Removing items from the front of the queue isstraightforward
But we need to insert items at the back of the queue
in constant time So cannot traverse through the list
By using an additional reference (and a little administration)we can keep track of the node at the back of the queue
-
8/12/2019 Lect 7 Queue Using LLpptx
9/18
List Queue Example
front
6
back
//Java Code
Queue q = newQueue();q.enqueue(6);
-
8/12/2019 Lect 7 Queue Using LLpptx
10/18
List Queue Example
front 4
6
back
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
-
8/12/2019 Lect 7 Queue Using LLpptx
11/18
List Queue Example
front 4
6
back
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
7
-
8/12/2019 Lect 7 Queue Using LLpptx
12/18
List Queue Example
front 4
6
back
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
7
3
-
8/12/2019 Lect 7 Queue Using LLpptx
13/18
List Queue Example
front 4
6
back
//Java Code
Queue q = newQueue();q.enqueue(6);
q.enqueue(4);
q.enqueue(7);
q.enqueue(3);
q.dequeue();7
3
-
8/12/2019 Lect 7 Queue Using LLpptx
14/18
Queue: Circular Linked List
Implementation
Possible implementations of a queue
A circular linked list with one external reference
A reference to the back
Figure 8-4b
A reference-based implementation of a queue: b) a circular linear linked list with one
external reference
-
8/12/2019 Lect 7 Queue Using LLpptx
15/18
Queue: Circular Linked List
Implementation
Figure 8-5
Inserting an item into a nonempty queue
-
8/12/2019 Lect 7 Queue Using LLpptx
16/18
Queue: Circular Linked List
Implementation
Figure 8-6
Inserting an item into an empty queue: a) before insertion; b) after insertion
-
8/12/2019 Lect 7 Queue Using LLpptx
17/18
Queue: Circular Linked List
Implementation
Figure 8-7
Deleting an item from a queue of more than one item
-
8/12/2019 Lect 7 Queue Using LLpptx
18/18
Questions