Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Data Structure & Algorithm

Queue Implementation (Linear & Circular)


LAB 07
1. Linear Queue InC++
In software terms, the queue can be viewed as a set or collection of elements as shown below.
The elements are arrangedlinearly.

We have two ends i.e. “front” and “rear” of the queue. When the queue is empty, then both
the pointers are set to-1.

The “rear” end pointer is the place from where the elements are inserted in the queue. The
operation of adding /inserting elements in the queue is called “enqueue”.

The “front” end pointer is the place from where the elements are removed from the queue.
The operation to remove/delete elements from the queue is called “dequeue”.

When the rear pointer value is size-1, then we say that the queue is full. When the front is
null, then the queue is empty.

Basic Operations
The queue data structure includes the following operations:
 EnQueue: Adds an item to the queue. Addition of an item to the queue is always
done at the rear of thequeue.
 DeQueue: Removes an item from the queue. An item is removed or de-queued
always from the front of thequeue.
 isEmpty: Checks if the queue isempty.
 isFull: Checks if the queue isfull.
 peek: Gets an element at the front of the queue without removingit.
Enqueue
In this process, the following steps are performed:
 Check if the queue isfull.
 If full, produce overflow error andexit.
 Else, increment‘rear’.
 Add an element to the location pointed by‘rear’.
 Returnsuccess.
Dequeue
Dequeue operation consists of the following steps:
 Check if the queue isempty.
 If empty, display an underflow error andexit.
 Else, the access element is pointed out by‘front’.
 Increment the ‘front’ to point to the next accessibledata.
 Returnsuccess.
Next, we will see a detailed illustration of insertion and deletion operations in queue.

Illustration

This is an empty queue and thus we have rear and empty set to -1.

Next, we add 1 to the queue and as a result, the rear pointer moves ahead by one location.

In the next figure, we add element 2 to the queue by moving the rear pointer ahead by another
increment.
In the following figure, we add element 3 and move the rear pointer by 1.

At this point, the rear pointer has value 2 while the front pointer is at the 0th location.
Next, we delete the element pointed by the front pointer. As the front pointer is at 0, the
element that is deleted is 1.

Thus the first element entered in the queue i.e. 1 happens to be the first element removed
from the queue. As a result, after the first dequeue, the front pointer now will be moved ahead
t0 the next location which is1.

2. Circular Queue InC++


The following diagram shows a circular queue.
The above image shows a circular data structure of size 10. The first six elements are already
in the queue and we see that the first position and last position are joined. Due to this
arrangement, space doesn’t go wasted as it happens in a linear queue.

In a linear queue after the queue is full, we delete the elements from another end, and the
status of the queue is still shown as full and we cannot insert more elements.

In the circular queue, when the queue is full, and when we remove elements from the front
since last and first positions are connected, we can insert the elements at the rear which was
vacated by deleting the element.

Front: Returns the front position in the circular queue.


Rear: Returns the rear position in the circular queue.

Basic Operations:
Some of the basic operations of the circular queue are as follows:
Enqueue: Enqueue (value) is used to insert an element in the circular queue. The element is
always inserted at the rear end of the queue.
Steps to insert a new element in the circular Queue:
1. Check if the circular queue is full: test ((rear == SIZE-1 && front == 0) || (rear ==
front-1)), where ‘SIZE’ is the size of the circularqueue.
2. If the circular queue is full then it displays a message as “Queue is full”. If queue is
not full then, check if (rear == SIZE – 1 && front != 0). If it is true then set rear=0
and insertelement.
Dequeue: Dequeue function is used to delete an element from the queue. In the circular
queue, the element is always deleted from the front end. Given below is the sequence for
dequeue operation in a circular queue.
Steps to delete element from circular queue:
1. Check if the circular queue is Empty: check if(front==-1).
2. If it is empty then display the message “Queue is empty”. If queue is not empty then
perform step3.
3. Check if (front==rear). If it is true then set front=rear= -1 else check if (front==size-
1), if it is true then set front=0 and return theelement.
Illustration
In this section, we will go through a detailed illustration of adding/removing elements in the
circular queue.
Consider the following circular queue of 5 elements as shown below:

Next, we insert item 1 in the queue.

Next, we insert an item with value 3.

When we insert the elements to make a queue full, the representation will be as shown below.

Now we delete the two elements i.e. item 1 and item 3 from the queue as shown below.

Next, we insert or enqueue element 11 in the circular queue as represented below.


Again let us insert element 13 in the circular queue. The queue will look as shown below.

We see that in the circular queue we move or insert elements in a circle. Hence we can
consume the entire space of the queue till it becomes full.
Data Structure & Algorithm

Queue Implementation (Linear & Circular)

LAB 07

LAB TASK

Lab Task 01: Create a menu driven program, to form a Linear Queue using array max size
=5, which perform the following operations on queue:
addq( ) // add element into queue
delq( ) // delete element from queue.
displayq( ) // display the elements of queue

Lab Task 02: Create a menu driven program, to form a Circular Queue using array max
size =5, which perform the following operations on queue:
addq( ) // add element into queue
delq( ) // delete element from queue.
displayq( ) // display the elements of queue

You might also like