Queue ADT
Queue ADT
• Def : A queue is a list of items from which items
q
may be deleted at one end called “front” and
which items may be added at the other end
called “rear”
called rear .
• Queue organizes entries according to order of
entry
entry
• Queue types:
– Regular queueg q
Regular queue
Regular queue
• In this type of queue the first element
In this type of queue the first element
inserted is the first element to be removed.
So it is a FIFO structure
So it is a FIFO structure.
• Logical view
rear
N h i i
A B C
rear
Note: among the items in a queue, the one added first, or earliest, is the front of the queue, and the one added
front
queue, and the one added most recently is the back of the queue.
Queue ADT specification
Queue ADT specification
• Data
Data
– A collection of objects in a specific order
• OPERATIONS
OPERATIONS
– Enqueue Task: Adds a new entry to the back of the queue. Input: newEntry is the new entry.
Output: None.
– Dequeue() Task: Removes and returns the front of the queue.
I t N
Input: None.
Queue ADT specification
Queue ADT specification
• getFront() Task: Retrieves the front of the queue without h i h i
changing the queue in any way. Input: None.
Output: Returns either the front of the queue or null if the queue is empty
the queue is empty.
• isEmpty() Task: Determines whether the queue is empty. Input: None.
Output: Returns true if the queue is empty Output: Returns true if the queue is empty. • Clear() Task: Removes all entries from the queue.
Input: None. Output: None.p
• Note: Alternate names for methods:
Queue Interface
Queue Interface
Public interface queueInterface{q {
/** Task: Adds a new entry to the back of the queue. *@param newentry an Object to be added*/
public Void enqueue(Object newEntry);
/** f f
/** Task: Removes and returns the front of the queue.
*@return either the Object at the front of the queue or null * if the queue was empty /if the queue was empty*/
Queue Interface
Queue Interface
/** Task: Retrieves the front of the queue
*@return either the Object at the front of the queue or null * if the queue was empty*/
public Object getFront(); public Object getFront();
/* Task: Determines whether the queue is empty. *@return true if the queue is empty */
*@return true if the queue is empty */
public Boolean isEmpty();
/** Task: Removes all entries from the queue.*/
public Void clear();
Example how to use Queue ADT
QueueInterface myQueue = new LQueue(); myQueue.enqueue(“Rami”);
myQueue enqueue(“Firas”); myQueue.enqueue( Firas ); myQueue.enqueue(“Ruslan”); myQueue.enqueue(“Hadi”);
Q (“S i”) myQueue.enqueue(“Sami”);
String front = (String) myQueue.getFront();
System.out.printIn(front + “ is at the front of the queue.”) F t (St i ) Q d ()
Front = (String) myQueue.dequeue();
System.out.printIn(front + “ is removed from the queue.”); myQueue.enqueue(“Amer”);
frnt = (String) myQueue.getFront();
System.out.printIn(front +” is at the front of the queue.”); Front = (String) miQueue.dequeue();
A Linked Implementation of a Queue
A Linked Implementation of a Queue
• Use chain of linked nodes for the queue
Use chain of linked nodes for the queue
– Two ends at opposite ends of chain
Accessing last node inefficient
– Accessing last node inefficient
– Could keep a reference to the tail of the chain
Pl
f
t f
t b
i
i
f h i
• Place front of queue at beginning of chain
• Place back of queue at end of chain
A Linked Implementation of a Queue
public class LQueue implements QueueInterface{ private Node frontNode; // references node for front of queue
private Node backNode; // references node for back of queue
private Node backNode; // references node for back of queue
public LQueue()
{
frontNode = null; backNode = null;
} // end default constructor
public Object getFront() { Object front = null;
{ j
if (!isEmpty())
front = frontNode. data; return front;
A Linked Implementation of a Queue
A Linked Implementation of a Queue
public void enqueue(Object newEntry)
p
q
(
j
y)
{ Node newNode = new Node(newEntry, null);
if (isEmpty())
(
p y())
frontNode = newNode;
else
backNode. next = newNode;
backNode = newNode;
A Linked Implementation of a Queue
A Linked Implementation of a Queue
public Object dequeue()
p j q ()
{ Object front = null; if (!isEmpty())
{ front = firontNode. data;
frontNode = frontNode.next ;
f (f )
if (frontNode == null) backNode = null; } // end if
A Linked Implementation of a Queue
A Linked Implementation of a Queue
public boolean isEmpty()
public boolean isEmpty()
{ return frontNode == null;
} // end isEmpty
} // end isEmpty
public void clear()
public void clear()
{ firontNode = null;
Circular Linked Implementations of a Queue
C cu a
ed
p e e tat o s o a Queue
• Last node references first node
Last node references first node
– Now we have a single reference to last node
And still locate first node quickly
– And still locate first node quickly
• No node contains a null
A circular linked chain with an
external reference to its last node that (a) has more than one node; (b) has one node; (c) is empty node; (c) is empty.
A Two-Part Linked Chain
A Two Part Linked Chain
• Linked nodes that form the queue followed by
Linked nodes that form the queue followed by
linked nodes available for use in the queue
– queueNode references front of queue node
– freeNode references first available node following
end of queue
• In essence we have two chains
– One for the queue
O f il bl d
A Two-Part Linked Chain
A Two Part Linked Chain
A two-part circular linked chain that represents both a queue and the nodes available to the queue.
A Two-Part Linked Chain
A Two Part Linked Chain
A two-part circular linked chain that represents a queue: (a)
when it is empty; (b) after adding one entry; (c) after adding three one entry; (c) after adding three more entries.
If freeNode.next== queueNode then we to add a new Node we then we to add a new Node, we have to leave one Node Unused -The Queue is empty if
queueNode==freeNode
-The Queue is full if
A Two-Part Linked Chain
A Two Part Linked Chain
A two-part circular linked chain that represents a queue: (d) after removing the front;
(e) after adding one (e) after adding one more entry
A Two-Part Linked Chain
A Two Part Linked Chain
A Two-Part Linked Chain
A Two Part Linked Chain
A chain with a node available for an addition to a queue: (a) before the addition; (b) after the addition
A Two-Part Linked Chain implementation
A Two Part Linked Chain implementation
public class TwoPartCircularLinkedQueue implements
p Q p
QueueInterface{
private Node queueNode; // references first node in queue private Node freeNode; // references node after back of queue public TwoPartCircularLinkedQueue()
{{
freeNode = new Node(null, null); freeNode next=freeNode;
A Two-Part Linked Chain implementation
A Two Part Linked Chain implementation
public void enqueue(Object newEntry) {
if (isChainFull()) {
// allocate a new node and insert it after node that freeNode references
Node newNode = new Node(null, freeNode.next); freeNode.next= newNode;
} // end if
freeNode.data= newEntry; freeNode = freeNode.next;; } // end enqueue
Array Based implementation of a Queue ADT
ay
ased
p e e tat o o a Queue
1. Fixed front: when the operation dequeue used,
p
q
then the all elements of the array must be
shifted. [inefficient]
2
Use two indexes: [frontIndex and backIndex]
2. Use two indexes: [frontIndex and backIndex] .
In this case the indexes moved instead of
moving elements. so when we dequeue the
queue we move the frontIndex and when we
queue we move the frontIndex and when we
enqueue the queue we move the backIndex.
This type of implementation has some
problems as follows:
problems as follows:
– the queue may become full by backIndex,
Problems with two indexes implementation of a queuep q
• Example: the array size of queue is 4 then
Example: the array size of queue is 4 then
enqueue(“A”); enqueue(“B”); enqueue(“C”); enqueue(“D”);
Not allowed the queue is full but it is not , one location is available, or double the array to allow this operation
A A
A B C D
dequeue(); enqueue(“E”);
Array Based implementation of a Queue ADT
3 Ci l th id i t t li i t
3. Circular array: the idea is to convert linear queue into circular queue such that the element queue[0],
immediately follows the element queue[queuesize-1].
O th h th d f th
• Once the queue reaches the end of the array, we can add entries to the beginning of the array.
• In order to implement the above idea we can use one of the following:
of the following:
– Using if statement
• If (backIndex == size-1) bakcindex=0; • If(frontIndex == size-1) frontIndex=0;If(frontIndex size 1) frontIndex 0;
– Using reminder operation
• frontIndex = (frontIndex + 1)%size • backIndex = (backIndex +1)%size
• In this type of implementation the best condition for indicating empty queue is to make the
Array Based implementation of a Queue ADT
ay
ased
p e e tat o o a Queue
4. Leaving one of array locations unused
4. Leaving one of array locations unused
(reserved location): which is follows the
backIndex of the queue
q
–
In this type of implementation the full
condetion will be:
(backIndex+1)% size==frontIndex
5. Use counter: if its value=0, then the
Array Based implementation of a Queue ADT
public class AQueue implements QueueInterface {
private Object[] queue; // circular array of queue entries private int frontIndex;
private int frontIndex; private int backIndex;
private static final int _MAX-SIZE = 50; public AQueue()
p () {
queue = new Object [MAX-SIZE]; frontIndex = MAX-SIZE-1;
backIndex = MAX-SIZE-1; } // end default constructor
public ArrayQueue(int initialMaxSize) {{
Array Based implementation of a Queue ADT
public void enqueue(Object newEntry) { if (isFull()) // isFull is private
{ ( ()) p
doubleArray();
backIndex = (backIndex + 1) % queue.length; queue[backIndex] = newEntry;
queue[backIndex] = newEntry; } // end enqueue
bli Obj t tF t()
public Object getFront() { Object front = null;
if (!isEmpty())( p y())
Int x=(frontIndex + 1) % queue.length; front = queue[x];
Array Based implementation of a Queue ADT
public Object dequeue()
{ Object front = null; if (!isEmpty())
{ frontIndex =(frontIndex + 1) % queue.length; front = queue[frontIndex];
queue[frontIndex] = null; } // end if
return front; } // end dequeue
public boolean isEmpty()
{ return frontIndex == backIndex ;
{ ;
} // end isEmpty
Array Based implementation of a Queue ADT
ay
ased
p e e tat o o a Queue
private void doubleArray()
{ Object[] oldQueue = queue; int oldSize = oldQueue.length; queue = new Object[2*oldSize]; queue new Object[2 oldSize]; int index
for (index = 0; index < oldSize - 1; index++) { frontIndex = (frontIndex + 1) % oldSize; { frontIndex = (frontIndex + 1) % oldSize;
queue[index] = oldQueue[frontIndex]; } // end for
frontIndex = queue.length-1; backIndex = index - 1;
Vector-Based Implementation of a Queue
ecto
ased
p e e tat o o a Queue
• Maintain front of queue at beginning of
Maintain front of queue at beginning of
vector
• Use
Use
addElement
addElement
method to add entry at
method to add entry at
back
– Vector expands as necessary
p
y
• When remove front element, remaining
elements move so new front is at
beginning of vector
Vector-Based Implementation of a Queue
Vector Based Implementation of a Queue
Vector-Based Implementation of a Queue
import java.util.Vector;
public class VQueue implements queueInterface { private Vector queue;// queue front is first in the vector
public VQueue()
{ queue = new Vector(); // t d bl i i if
{ queue = new Vector(); // vector doubles in size if ecessary
} // end default constructor
public VQueue(int maxSize) public VQueue(int maxSize) {
queue = new Vector(maxSize);
q ( )
Vector-Based Implementation of a Queue
Vector Based Implementation of a Queue
public void enqueue(Object newEntry)
p q ( j y)
{ queue.addElement(newEntry); } // end enqueue
public Object getFront()
{ O f
{ Object front = null; if (!isEmpty())
front = queue firstElement(); front = queue.firstElement(); return front;
Vector-Based Implementation of a Queue
bli Obj t d ()
public Object dequeue() { Object front = null;
if (!isEmpty())( p y())
{ front = queue.firstElement(); queue.removeElementAt(0); } // d if
} // end if return front; } // end dequeue } // end dequeue
public boolean isEmpty()
Specifications of the ADT Deque
Specifications of the ADT Deque
• A Double ended queue
A Double ended queue
• Has operations that
Add
t i
t i
– Add, remove, or retrieve entries
– At both its front and back
DeQue Interface
DeQue Interface
public interface DequeInterface {
p q {
public void addToFront(Object newEntry); public Void addToBack(Object newEntry); public Object removeFront();
public Object removeBack();
O ()
public Object getFront(); public Object getBack(); public Boolean isEmpty(); public Boolean isEmpty(); public Void clear();
Examp;e how to use DeQue ADT
Examp;e how to use DeQue ADT
DequeInterface myDeque = new LDeque();
q
y
q
q
();
myDeque.addToFront(“Firas”);
myDequeue.addToBack(“Ruslan”);
y
q
(
);
myDeque.addToFront(“Amer”);
myDeque.addToBack(“Sami”);
String name = myDeque.getFront();
myDeque.addToFront(name);
myDeque.removeFront();
Specifications of the ADT
P i it Q
Priority Queue
• Organizes objects according to priorities
Organizes objects according to priorities
– Contrast to regular queue in order of arrival
• Priority queue example – a hospital ER
Priority queue example a hospital ER
• Priority can be specified by an integer
– Must define whether high number is high
– Must define whether high number is high
priority or …
– Low number (say 0) is high priority
(
y )
g p
y
• Other objects can specify priority
Priority Queue Interface
Priority Queue Interface
public interface
PriorityQueueInterface
public interface
PriorityQueueInterface
{
public void
add(Comparable newEntry);
public
Comparable remove();
public
Comparable get();
public boolean
isEmpty();
bli i t
tSi
()
public int
getSize();
public void