Professional Documents
Culture Documents
CSC 220
Data Structures and Algorithms
Lecture # 8
Queues
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Lecture Outlines 2
Lecture Outlines
• Queue ADT
• Real-life Queues
• Using a Queue
• Queue Operations
• Examples:
Queues using ArrayList
Queues using Array
• Implementation of Queues:
Using ArrayList
Using Array
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Lecture Outlines 3
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue ADT 4
Queue ADT
• Queue: is an abstract data type (ADT) in which items are
added at one end (the rear) and removed from the other
end (the front).
• Queue is a FIFO “First In, First Out” structure.
The first item in is the first item served (out).
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Real-life Queues 5
Real-life Queues
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Using a Queue 6
Using a Queue
• “Buffer” for printer uses a queue.
• “Buffer” for a mail server uses a queue.
• Operating System (OS) uses “Priority Queues”.
• Simulation applications use queues to model customers
waiting in a line.
• Any problem requiring FIFO property should use a
queue.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 7
Queue Operations
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 8
Transformers
Enqueue Change state
Dequeue
MakeEmpty
Observers
IsEmpty Observe state
IsFull
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 9
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Example 1 – Queues using Arrays 10
Implementation of Queues
• Let us now see how to use ArrayList and Array to implement a queue.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using ArrayList 13
public CQueue() {
pqueue = new ArrayList();
}
.
.
.
};
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using ArrayList 14
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using Array 15
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using Array 16
The second pass: sorts the numbers based on the 10’s digit.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Sorting Data with Queues: Radix Sort 18
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 20
Priority Queues
• As you know, a queue is a data structure where the first item
placed in the structure is the first item taken out of the
structure.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 21
getNext: removes the item from the queue that has the highest
priority, and return it.
(also known as: “PopElementOff” or “GetMinimum”)
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 22
Student 0
Lecturer 1
Doctor 2
Head of Department 3
Dean 4
1. An empty queue:
Rear
Front
Ali
2. Enqueue("Ali",0): 0
Rear
Front
Ali Omer
3. Enqueue("Omer",0): 0 0
Rear
Front
Sami Ali Omer
4. Enqueue("Sami",2): 2 0 0
Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 24
Front
Front
Ahmed Fadi Ali Omer
7. Dequeue(): 2 1 0 0
Rear
Front
Fadi Ali Omer
8. Dequeue(): 1 0 0
Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 25
Front Rear
A B C
Front Rear
after 1 dequeue, and 1 enqueue: B C D
Front Rear
after 1 dequeue, and 1 enqueue: C D E
The problem here is that the rear index cannot move beyond the last
element in the array.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 26
Rear Front
after 1 enqueue: F C D E
Rear Front
after 2 dequeue and 2 enqueue: F G H E
Front Rear
after 1 dequeue: F G H
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 27
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 28
Run-time complexity:
• Enqueue(), Dequeue(), … are all O(1)
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Allocation of Memory 29
Allocation of Memory
Static Dynamic
Allocation Allocation
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 30
front rear
X Y Z
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 31
front rear
front rear
front rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 32
front rear
(6) Dequeue():
'S'
front rear
front rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)