0% found this document useful (0 votes)
37 views

Week05 Lesson 05 Queues

The document discusses queues as a data structure. Queues follow a first-in, first-out (FIFO) approach where the first item inserted is the first removed. Items can be inserted at the rear of the queue and removed from the front. Circular queues are also described which allow the rear to wrap around the end of the array.

Uploaded by

Gavin Hemsada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Week05 Lesson 05 Queues

The document discusses queues as a data structure. Queues follow a first-in, first-out (FIFO) approach where the first item inserted is the first removed. Items can be inserted at the rear of the queue and removed from the front. Circular queues are also described which allow the rear to wrap around the end of the array.

Uploaded by

Gavin Hemsada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ITE 2142 – Data Structures & Algorithms Week 05

LESSON 05– The 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

5.1 The Queue

A queue is an ordered collection of items from which item may be


deleted at one end (called the front end of the queue) and into which
items may be inserted at the other end (called the rear of the queue).
The figure 1.1 illustrates a queue containing three elements A, B and
C. A is at the front of the queue and C is at the rear.

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.

5.2 Sequential representation of a Queue


Here we use an array to represent a queue. Variables front, rear have been used to represent front of
the queue and rear of the queue.
class Queue
{
private int maxSize; //defeiness the maximum number of element
private int[] queArray;
private int front;
private int rear;
private int nItems;
//------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new int[maxSize];

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

5.3 Circular Queue


It is clear from the code that, when new item is inserted rear arrow in figure 5.1 is increased. When
remove an item front also moves upward. You may find the arrangement counter-intuitive, because
the people in a line at the movies all move forward, toward the front, when the person leaves the
line. We could move all the items in a queue whenever we deleted one, but that wouldn’t be very
efficient. Instead we keep all the items in the same place and move the front and rear of the queue.
The trouble with this arrangement is that pretty soon the rear of the queue is at the end of the array.
Even if there are empty cells at the beginning of the array, because you’ve removed them, you still
can’t insert a new item because rear can’t be increased (See the figure 1.2).

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.3.1 Sequential Implementation of Circular Queues


A Circular queue can be implemented using an array.
class Queue
{
private int maxSize; //defines the maximum number of element
private int[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

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.

5.5 Priority Queues


A priority queue is a more specialized data structure than a stack or queue. However, it is useful in
astounding number of situations. Like an ordinary queue, a priority queue has a front and a rear, and
items are removed from the front. However, in a priority queue, items are ordered by key value, so

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.

5.5.1 Sequential Implementation of Priority Queue


Implementation of a priority queue using an array is shown here.

class PriorityQ

// array in sorted order, from max at 0 to min at size-1


private int maxSize;
private double[] queArray;
private int nItems;

//-------------------------------------------------------------
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

for(j=nItems-1; j>=0; j--) // start at end,


{
if( item > queArray[j] ) // if new item larger,
queArray[j+1] = queArray[j]; // shift upward
else // if smaller,
break; // done shifting
} // end for
queArray[j+1] = item; // insert it
nItems++;
} // end else (nItems > 0)
} // end insert()

//------------------------------------------------------------
public double remove() // remove minimum item
{ return queArray[--nItems]; }

public boolean isEmpty() // true if queue is empty


{ return (nItems==0); }

//--------------------------------------------------------
public boolean isFull() // true if queue is full
{ return (nItems == maxSize); }

//------------------------------------------------------------
} // end class PriorityQ
////////////////////////////////////////////////////////////////

9
Queues
ITE 2142 – Data Structures & Algorithms Week 05

Activity 5.1 what would be the output of following sample programme?

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

Activity 5.1- Answer 10 20 30 40 50

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

You might also like