Week05 Lesson 05 Queues
Week05 Lesson 05 Queues
Introduction
Last lesson you have learnt about Stack. This lesson introduces the queue as another important data
structure. The word queue is British for line. In Britain, to “queue up” means to get in line. In
computer science a queue is a data structure that is similar to a stack, except that in a queue the first
item inserted is the first to be removed.
Learning outcome
On successful completion of this lesson, you would be able to define queue abstract data type and
operations which can be performed using them.
Define Queue abstract data type
Apply operations which can be performed using queues
Describe different types of queues
front
A B C
Figure 5.1
rear
1
Queues
ITE 2142 – Data Structures & Algorithms Week 05
The first element inserted into a queue is the first element to be removed. Because of this reason a
queue is sometimes called as First in First Out (FIFO) or Last In Last Out (LILO) data structure as
opposed to a stack, which is Last In First Out (LIFO) data structure. Examples of a queue abound
in the real world. A line at a bank or at a bus stop and a group of cars waiting at a toll booth are
familiar examples of queues.
Three primitive operations can be applied to a queue. They are insert, remove, isFull and isEmpty.
The operation insert inserts new item x at the rear of the queue. The operation remove deletes the
front element from the queue. The isEmpty operation checks whether the queue is empty or not. The
isFull operation checks whether the queue is full or not.
The insert operation can always be performed, if the queue in not full or when there is no limit to
the number of elements a queue may contain. The remove operation, however, can be applied only
if the queue is nonempty; there is no way to remove an element from a queue containing no elements.
The result of an illegal attempt to remove an element from an empty queue is called underflow. The
empty operation is, of course, always applicable.
front = 0;
2
Queues
ITE 2142 – Data Structures & Algorithms Week 05
rear = -1;
nItems = 0;
//--------------------------------------------------------------
public void insert(int j) // put item at rear of queue
{ if !isFull()) {
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
}
//--------------------------------------------------------------
public int remove() // take item from front of queue
{
if (!isEmpty()) {
int temp = queArray[front++]; // get value and increase front
nItems--; // one less item
return temp;
}
return null;
}
//------------------------------------------------------------
public int peekFront() // peek at front of queue
{
return queArray[front];
}
//------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
3
Queues
ITE 2142 – Data Structures & Algorithms Week 05
//------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//------------------------------------------------------------
} // end class Queue
Figure 5.2 : new items cannot be inserted since the rear is at its maximum value.
4
Queues
ITE 2142 – Data Structures & Algorithms Week 05
To avoid the problem of not being able to insert more items into the queue even when it’s not full,
the front and rear arrows wrap around to the beginning of the array (See Figure 5.3). The result is a
Circular Queue.
Figure 5.3: rear arrows wrap around to the beginning of the array to insert new item 658.
5
Queues
ITE 2142 – Data Structures & Algorithms Week 05
//--------------------------------------------------------------
public void insert(int j) // put item at rear of queue
{ if (!isFull()) {
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
}
//--------------------------------------------------------------
public int remove() // take item from front of queue
{
if (!isEmpty()) {
int temp = queArray[front++]; // get value and incr front
if(front == maxSize) // deal with wraparound .
front = 0;
nItems--; // one less item
return temp;
}
return null;
}
//--------------------------------------------------------------
public int peekFront() // peek at front of queue {
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
6
Queues
ITE 2142 – Data Structures & Algorithms Week 05
//--------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//--------------------------------------------------------------
} // end class Queue
5.4 DEQUES
A Deque (Doubly Ended Queue) an ordered set of items from which items may be deleted at either
end and into which items may be inserted at either end called two ends of a deque ‘front’ and ‘rear’.
There are few operations such as insertLeft, insertRight, removeLeft, removeRight and isEmpty()
associated with deque. The insertLeft and insertRight operations insert new items to deque from
the left and right respectively. In contrast removeLeft and removeRight removes new items from
the left and right of deque. If you restrict yourself to insertLeft and removeLeft (or their equivalents
on the right), then the deques acts like a stack. If you restrict yourself insertRight and removeRight
(or the opposite pair), then it acts like a queue.
A deque provides a more versatile data structure than either a stack or queue, and is sometimes used
in container class libraries to serve both purpose.
7
Queues
ITE 2142 – Data Structures & Algorithms Week 05
that the item with the lowest key (or some implementations the highest key) is always at the front.
Items are inserted at the proper position to maintain the order.
Here’s how the mail sorting analogy applies to a priority queue. Every time the postman hands you
a letter, you insert it into your pile of pending letters according to its priority. If it must be answered
immediately (a bank is asking you to collect your ATM card ) it goes top, while if it can wait for a
leisurely answer ( A letter from your uncle Ron) ,it goes on the bottom.
class PriorityQ
//-------------------------------------------------------------
public PriorityQ(int s) // constructor
{
maxSize = s;
queArray = new double[maxSize];
nItems = 0;
}
//------------------------------------------------------------
public void insert(double item) // insert item
{
int j;
if(nItems==0) // if no items,
queArray[nItems++] = item; // insert at 0
else // if items are there,
{
8
Queues
ITE 2142 – Data Structures & Algorithms Week 05
//------------------------------------------------------------
public double remove() // remove minimum item
{ return queArray[--nItems]; }
//--------------------------------------------------------
public boolean isFull() // true if queue is full
{ return (nItems == maxSize); }
//------------------------------------------------------------
} // end class PriorityQ
////////////////////////////////////////////////////////////////
9
Queues
ITE 2142 – Data Structures & Algorithms Week 05
class PriorityQApp
{
public static void main(String[] args) throws IOException
{
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while( !thePQ.isEmpty() )
{
double item = thePQ.remove();
System.out.print(item + " ");
} // end while
System.out.println("");
} // end main()
//-------------------------------------------------------------
} // end class PriorityQApp
Summary
During this lesson you learnt about Queues which implements FIFO protocol. You also got
know about different types of queue such as circular queues, deques and priority queues and
how do they differ from each other as well as what types of real world scenario they can be
applied. Next lesson you will about Tree data structure.
10
Queues