Beruflich Dokumente
Kultur Dokumente
3. Queues
Queue is an ordered collection of elements from which elements may be deleted (i.e.
removed) from one end (called Front end of the queue) and elements may be inserted
at the other end (called Rear end of the queue).
The element that comes first in the queue is removed first thus Queue is also called
FIFO (First In First Out) list.
front
...
0
n-1
-1
-1
rear
Figure 1-a shows an empty queue. The next figure 1-b shows a queue after adding
three elements A, B and C to the queue. In this case q.rear=2 and q.front=0. The last
figure 5.1-c shows a queue after removing elements A and B from the queue.
Prepared by, Santosh Kabir
Mobile: 98336 29398
1 of 12
3.Queues
www.santoshkabirsir.com
0
1
2
.
.
SIZE-1
front= -1
A
B
C
.
.
C
.
.
front = 0
front =2
1-a
1-b
1-c
If array size is 8 then at time queue can store maximum 8 elements. Suppose, as we go on adding and removing
elements from the queue, a situation arrives where rear is 7 and front is at any position, say, 3 then further addition
of an element will generate queue overflow error. In this case actually queue is not full since the first 3 elements
are already removed from the queue.
One solution to this is to modify the remove operation. Assume there are total n elements present in a queue.
Whenever an element is removed from a queue, shift all the elements towards the front of the queue so that front
is always at position 0. The method is too inefficient since each removal of an element involves moving all the n-1
elements in the queue. If queue contains thousands of elements then its really very inefficient way of using a
queue.
The best solution to this problem is to consider a circular queue (the topic that we will discuss later in this chapter).
Note: We will consider front= -1 and rear= -1 at the beginning.
1. Linear Queue:
Elements are added into array one after other.
As last element of array is filled, queue becomes full. Adding further elements to
Queue is not permitted.
Different operations with their algorithms are mentioned below :
Empty queue:
Function returns true (i.e. 1 ), if queue has no elements.
A-1
Algo: Empty( )
1. If front= -1 then
a. Return 1
2. Else
a. Return 0
Full Queue:
This condition arises when the rear end item exists at the last element of the array
created for storing the queue items. The function can be written as follows that
returns true if queue is full.
A-2
Algo: FullQueue( )
2 of 12
3.Queues
www.santoshkabirsir.com
Remember that a function remove() should be called only if the queue is not
empty.
Displaying a queue:
Displaying queue is printing array elements from fe to re index, if queue is not empty.
A-5
Algo: ShowQueue( )
1. If queue is empty
Print queue is empty
1. Else
i. Read the front end position of queue.
Ii While rear end is not reached repeat following
a. Display data at the current position
3 of 12
3.Queues
www.santoshkabirsir.com
struct Queue q;
int IsEmpty(void);
int IsFull(void);
void Add( int v );
int Remove(void);
void Display(void);
int Front(void);
int IsEmpty() // returns 1 if Queue is empty
{
return q.fe == -1;
}
int IsFull() // returns 1 if Queue is full
{
return q.re == Size-1;
}
// Add new element at the rear end. Also called enqueue operation.
q.a[ ++q.re ] = v;
}
}
// Return and remove element from front of Q. Also called dequeue operation.
int Remove()
{
Prepared by, Santosh Kabir
Mobile: 98336 29398
4 of 12
3.Queues
www.santoshkabirsir.com
5 of 12
3.Queues
www.santoshkabirsir.com
}
}
getch();
}
Priority Queue:
( More notes on Priority Queue for IT in separate set)
A queue in which elements are organized into groups according to priority and
processed such that the highest priority elements are output (or processed and
removed from queue) first.
6 of 12
3.Queues
www.santoshkabirsir.com
Rest of the algorithms and program will be same as that for simple queue.
// Add new element in the Queue according to the priority of new element.
7 of 12
3.Queues
www.santoshkabirsir.com
Circular Queue:
This is the most popular queue because of its efficiency.
Movement of elements is not required. Circular queue uses an array as if its a
circular structure.
The front-end position and rear-end position is updated on every addition or removal
of element in a queue. Thus, the movement of elements in the list is not required.
If last element in the array is filled, new element will be added to beginning of array
(i.e. in 0th index).
Consider an array of size 5 is used to implement a circular queue. Thus, we have lower index 0 and higher index 4
or we can keep maximum 5 items in the queue at a time. After addition of four items (say A, B , C and D) the front
end is 0 and rear-end is at 3. See the figure 5.2 below,
0
1
2
3
4
A
B
C
D
Queue
queue
fig.2
Front
rear
Now, if an item is removed (i.e. A is removed from the front) then the front position changes to 1 (i.e. item B is at
the front).
In circular queue if a last array element is filled then the items are added at the beginning of the array. Thus, the
array is assumed to be bent and joined at the two ends to form a circular structure as follows,
4
0
3
2
1
Following table shows front and rear positions after additions and removal of items.
Assume, there are 3 items in the queue (B,C and D) with front position at 1 and rear at 3.
Operation
Front Rear
Result
Item E added
E added
Item removed
B removed
Item F added
F added
Item G added
G added
Item H added
Item removed
C removed
8 of 12
3.Queues
www.santoshkabirsir.com
ii.
9 of 12
3.Queues
www.santoshkabirsir.com
// Return and remove element from front of Q. Also called dequeue operation.
int Remove()
{
int v;
v = q.a[ q.fe++ ];
if( q.fe - q.re == 1 ) //Q has become empty
q.fe = q.re = -1;
if( q.fe == Size )
q.fe = 0;
return v;
}
void Display()
{
int i;
if( IsEmpty() )
printf("Queue is empty");
else
{
printf("Queue elements ...\n ");
i= q.fe;
while( 1 )
{
printf("%d\t" , q.a[i] );
i++;
if( i - q.re == 1)
break;
if( i == Size )
i =0;
}
printf("\n");
}
}
10 of 12
3.Queues
www.santoshkabirsir.com
struct ListNode
*fe=NULL, *re=NULL;
// Functions
int IsEmpty()
{
return fe == NULL;
}
void Insert( int v )
{
struct ListNode *nw;
nw = (struct ListNode*) malloc( sizeof(struct ListNode) );
nw->data = v;
nw->next = NULL;
if( fe == NULL )
fe = nw;
else
re->next = nw;
re = nw;
}
These notes are also available at :
www.santoshkabirsir.com
void Display()
{
Prepared by, Santosh Kabir
Mobile: 98336 29398
11 of 12
3.Queues
www.santoshkabirsir.com
Sem II
* With practicals *
Classes conducted by Santosh Kabir sir.
12 of 12
3.Queues
www.santoshkabirsir.com