Unit-3 2
Unit-3 2
QUEUES
Queue is a linear data structure used in various applications of computer science. Like
people stand in a queue to get a particular service, various processes will wait in a queue for
their turn to avail a service.
Queue is a linear list in which insertions takes place at one end called
the rear or tail of the queue and deletions at the other end called as front or
head of the queue.
When an element is to join the queue, it is inserted at the rear end of
the queue and when an element is to be deleted, the one at the front end of
the queue is deleted automatically.
In queues always the first inserted element will be the first to be deleted. That‟s why it is
also called as FIFO – First-in First-Out data structure (or FCFS – First Come First Serve data
structure).
APPLICATIONS of QUEUE
CPU Scheduling (Round Robin Algorithm)
Printer Spooling
Tree & Graph Traversals
Palindrome Checker
Undo & Redo Operatio ns in some Software‟s
OPERATIONS ON QUEUE
The queue data structure supports two operations:
Enqueue: Inserting an element into the queue is called enqueuing the queue. After the data
have been inserted into the queue, the new element becomes the rear.
Dequeue: Deleting an element from the queue is called dequeuing the queue. The data at the
front of the queue are returned to the user and re move d from the queue.
TYPES OF QUEUES
1. Simple Queue: In this queue insertions take place at one end anddeletions take
place at other end.
2. Circular Queue: It is similar to single ended queue, but the front is connected back to
rear. Here the memory can be utilized effectively.
3. Double Ended Queue: In double ended queue both the insertions and deletions can
take place at both the ends.
4. Priority Queue: In priority queue the elements are not deleted according to the order
they entered into the queue, but according to the priorities associated with the
elements.
IMPLEMENTATION OF QUEUES
Queues can be implemented or represented in memory in two ways:
1. Using Arrays (Static Implementation).
2. Using Linked Lists (Dynamic Implementation).
However, the array implementation puts a limitation on the capacity of the queue. The
number of elements in the queue cannot exceed the maximum dimension of the one
dimensional array. Thus a queue that is accommodated in an array Q[1:n], cannot hold more
than n elements. Hence every insertion of an element into the queue has to necessarily test
for a QUEUE FULL condition before executing the insertion operation. Again, each deletion has
to ensure that it is not attempted on a queue which is already empty calling for the need to
test for a QUEUE EMPTY condition before executing the deletion operation.
CIRCULAR QUEUE
One of the major problems with the linear queue is the lack of proper utilization of space.
Suppose that the queue can store 10 elements and the entire queue is full. So, it means that
the queue is holding 10 elements. In case, some of the elements at the front are deleted, the
element at the last position in the queue continues to be at the same position and there is no
efficient way to find out that the queue is not full.
In this way, space utilization in the case of linear queues is not efficient. This problem
is arising due to the representation of the queue.
The alternative representation is to depict the queue as circular. In case, we are
representing the queue using arrays, then, a queue with n elements starts from index 0 and
ends at n- 1. So, clearly, the first element in the queue will be at index 0 and the last element
will be at n-1 when all the positions between index 0 and n-1 (both inclusive) are filled. Under
such circumstances, front will point to 0 and rear will point to n-1. However, when a new
element is to be added and if the rear is pointing to n-1, then, it needs to be checked if the
position at index 0 is free. If yes, then the element can be added to that position and rear can
be adjusted accordingly. In this way, the utilization of space is increased in the case of a
circular queue.
In a circular queue, front will point to one
position less to the first element. So, if the first
element is at position 4 in the array, then the front
will point to position 3. When the circular queue is
created, then both front and rear point t o index 1.
Also, we can conclude that the circular queue is
empty in case both front and rear point to the same
index. Figure depicts a circular queue.
Enqueue (value) - Inserting value into the
Circular Queue
Step 1: Check whether queue is FULL.
If ((REAR == SIZE-1 && FRONT == 0) || (FRONT == REAR+1))
Write “Queue is full ” and Exit
Step 2: If ( rear == SIZE - 1 && front != 0 ) then:
SET REAR := -1.
Step 4: SET REAR = REAR +1
Step 5: SET QUEUE[REAR] = VALUE
Step 6: Exit and check 'front == - 1' if it is TRUE, then set front = 0.
deQueue() - Deleting a value from the Circular Queue
Step 1: Check whether queue is EMPTY ?
If (FRONT == -1 && REAR == -1)
Write “Queue is empty” and Exit.
}
} /*End of display() */