Queues: by Dr. A.Ashok Kumar Department of Computer Science, Alagappa Government Arts College, Karaikudi

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

QUEUES

By
Dr. A.Ashok Kumar
Department Of Computer Science,
Alagappa Government Arts College,
Karaikudi
Introduction
• Queue refers to the collection of elements ,
accessed in a First In First Out (FIFO) order

• Elements are added to one end(rear) and


deleted from other end(front) of the queue

rear
Front
• The basic opertaions on a queue are
– Insertion
– Deletion
• Insertion operation insert elements at rear
• Deletion operation delete elements from front
• Various forms of queues are defined based on the
elements are accessed
– Circular queue
– Dequeue
– Priority queue
Implementation of Queues
• Two forms of implementation
– Array-based implementation
– Linked list based implementation
• Both ends of the queue are used
– rear
– front
Array Based implementation
• An array of a predefined size and the indices
of the front and the rear end to be maintained
• Creation
• Declaration of an array and initializing front
and rear to 0
Algorithm CreateQ(int Q[])
1. front=0
2. rear=0
• Insertion
Algorithm Insert(Q, front, rear, item)
1. If rear >= SIZE
Then
a. Write “Queue Overflow”
b. Exit
Else
a. rear  rear +1
b. Q[rear]  item
c. If front = 0
Then
Front  1
2. end
• Deletion
• The first element of the queue is at the index front
Algorithm Delete(Q, front, rear)
1. If front=0
Then
a. Write “Queue Underflow”
b. Exit
2. item  Q[front]
3. If front = rear
Then
a. Front = rear = 0
Else
a. Front  front +1
4. return
Linked List Representation
• Two pointers as front and rear used in this representation
• Creation
• Creation of queue involves initializing the front and rear pointers to
be NULL
• A queue structure can be defined as
struct node
{ int data;
struct node *next;
} *rear,*front;

create()
{
front = rear = NULL;
}
• Insertion
Algorithm insertQ()
1. Create a newNode with the given value and set the
node's pointer to NULL.
2. Check whether queue is EMPTY.
3. If it is EMPTY, set FRONT and REAR to newNode.
4. Else, set the pointer of REAR to newNode and make
REAR as the newNode
• Deletion
Algorithm deleteQ()
1. Check if the queue is EMPTY.
2. If it is EMPTY, then display "EMPTY QUEUE" and exit.
3. Else, create a temporary node and set it to FRONT.
4. Make the next node as FRONT and delete the
temporary node
Circular Queue
• The locations of the queue are viewed in a
circular form
• The first location is viewed after the last one
• The overflow error occurs only when all
locations are filled
• When rear pointer reaches the end , it check
the first location
• Elements are inserted by increasing the
variable rear to the next free location
• When rear=n-1, the next element is entered at
q[0] if the spot is free.
• The variable front always points one position
counterclockwise from the first element in the
queue.
• The variable front=rear if and only if the queue is
empty, initially front=rear=0

You might also like