Queue With Types
Queue With Types
• One end is always used to insert data (enqueue) and the other is used
to remove data (dequeue).
• Enqueue:
• The enqueue operation is used to insert the element at the rear end of the queue. It
returns void.
• Dequeue:
• The dequeue operation performs the deletion from the front-end of the queue. It also
returns the element which has been removed from the front-end. It returns an integer
value. The dequeue operation can also be designed to void.
• Sequential allocation:
• The sequential allocation in a Queue can be implemented using an array.
• There are two variables i.e. front and rear, that are implemented in the case
of every queue.
• Front and rear variables point to the position from where insertions and
deletions are performed in a queue.
• Initially, the value of front and rear is -1 which represents an empty queue.
• If the item is to be inserted as the first element in the list, in that case
set the value of front and rear to 0 and insert the element at the rear
end.
• Otherwise keep increasing the value of rear and insert each element
one by one having rear as the index.
Algorithm
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] NUM
Step 4: EXIT
Algorithm to delete an element from the queue
• If, the value of front is -1 or value of front is greater than rear , write
an underflow message and exit.
• Otherwise, keep increasing the value of front and return the item
stored at the front end of the queue at each time.
Algorithm
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Applications of Queue
• In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
• Linear Queue
• Circular Queue
• Priority Queue
• Dequeue
Linear Queue
• In Linear Queue, an insertion takes place from one end while the
deletion occurs from another end.
• The end at which the insertion takes place is known as the rear end,
and the end at which the deletion takes place is known as front end.
• It strictly follows the FIFO rule. The linear Queue can be represented,
as shown in the below figure:
Continue
• The elements are inserted from the rear end, and if we insert more
elements in a Queue, then the rear value gets incremented on every
insertion.
• If we want to show the deletion, then it can be represented as:
• In the above figure, we can observe that the front pointer points to
the next element, and the element which was previously pointed by
the front pointer was deleted.
Drawback
• The major drawback of using a linear Queue is that insertion is done
only from the rear end.
• If the first three elements are deleted from the Queue, we cannot
insert more elements even though the space is available in a Linear
Queue.
• In this case, the linear Queue shows the overflow condition as the
rear is pointing to the last element of the Queue.
Circular Queue
• It is similar to the linear Queue except that the last element of the
queue is connected to the first element.
•
• It is also known as Ring Buffer as all the ends are connected to
another end.
• Front:
• It is used to get the front element from the Queue.
• Rear:
• It is used to get the rear element from the Queue.
• enQueue(value):
• This function is used to insert the new value in the Queue. The new element
is always inserted from the rear end.
• deQueue():
• This function deletes an element from the Queue. The deletion in a Queue
always takes place from the front end.
Applications of Circular Queue
• Memory management:
• The circular queue provides memory management. As we have already seen
that in linear queue, the memory is not managed very efficiently. But in case
of a circular queue, the memory is managed efficiently by placing the
elements in a location which is unused.
• CPU Scheduling:
• The operating system also uses the circular queue to insert the processes and
then execute them.
• Traffic system:
• In a computer-control traffic system, traffic light is one of the best examples
of the circular queue. Each light of traffic light gets ON one by one after every
interval of time. Like red light gets ON for one minute then yellow light for
one minute and then green light. After green light, the red light gets ON.
Algorithm to insert an element in a circular queue
Step 1: IF (FRONT == 0 and REAR == N-1) or (FRONT == REAR + 1)
Write " OVERFLOW "
Goto step 4
[End OF IF]