2.1 Queue
2.1 Queue
Topic 2 Queue
Content
• Introduction to Queue
• Queue Interface in Collection Framework
• Queue Implementation
• Queue Application
• Circular Queue
• Priority Queue
2
Introduction To Queues
▪ Definition:
Data structure in which the elements are added at one end, called
the rear, and deleted from the other end, called the front or first.
Examples Of Queues
Front/First Back/Rear
4
Queue in Java Collection Framework
6
Queue Implementation
• Inherits methods from linkedlist class or arraylist class but with more
restrictions that are consistent with queue concept.
• Queue properties:
• Front node
• Rear node
FRONT REAR
20 13 8
Queue Operations
8
Java Implementation
Java Implementation
10
Queue Application (Primitive Data Type)
for(int i = 1;i<4;i++){
queueInt.enqueue(i*10);
}
while(!queueInt.isEmpty())
System.out.print(Integer.parseInt(queueInt.dequeue().toString())
);
}}
13
for(int i=0;i<2;i++){
model = sc.next();
price = sc.nextDouble();
c = new Car(price,model);
queueCar.enqueue(c);
}
14
Circular Queue Concept
• Circular queue is an extended queue where the last node of the queue is
connected to the first node in the queue.
• Also known as ring buffer
• With queue, once the queue is full, data cannot be added even if there is a
space in front of the queue.
• This occurs especially when the queue is implemented using array or
arraylist.
• Circular queue allows for flexible movement of front and rear node.
16
Circular Queue
FRONT REAR
20 13 8
FRONT REAR
New node?
20 13 8 56
33
REAR FRONT
33 20 13 8 56
CIRCULAR QUEUE
17
Priority Queue
▪ Restrictions during data insertion where data value if checked to see its
priority.
▪ To implement:
▪ use an ordinary linked list, which keeps the items in order from the
highest to lowest priority
▪ use a treelike structure
18
Summary
• Introduction to Queue
• Queue Interface in Collection Framework
• Queue Implementation
• Queue Application
• Circular Queue
• Priority Queue
19