×

Linear Queue VS Circular Queue

What is Queue?

A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It follows a particular order of execution of data for which operations are performed.  In this data structure, data enters from one end, i.e., REAR end, and next data enters in the queue after the previous one, and deletion operation is performed from another end, .i.e., FRONT end.

As compare with stacks, stacks are also a linear data structure. Although, it is based on LIFO (Last In First Out) principle, which implies that data enters one by one from one end into the stack till it reaches its end limit, and it popped out the data element from the stack from that end itself.

In a stack, only one pointer is needed for performing all operations, which is top, whereas, in the queue, there are two pointers needed, rear and front, for performing all operations.

Types of Queues

The types of queue are as follows:

  • Linear Queue
  • Circular Queue
  • Deque
  • Priority Queue
  • Multiple Queue

Linear queue

A linear queue is said to be the simple queue, and whenever we need to talk about the queue, it is by default set that we are considering a linear queue. A linear queue is a linear data structure based on FIFO (First In First Out) principle, in which data enters from the rear end and is deleted from the front end. We will discuss these ends in more detail further.  

With an example of the Movie Ticket counter, a customer comes first in the queue is served first, and the next customer to that one comes after. This process will continue till all the tickets for that movie have been booked.

Other examples includes:

  • People are waiting for the bus. The first person up standing in the queue will be the first to get on the bus.
  • Cars lined at a toll bridge. The first car to reach out the bridge will be the first to leave.
Difference between Linear Queue and Circular Queue

Operations performed on Linear Queue

There are certain basic operations has performed in the linear queue are as follows->

1. Enqueue

  • For the addition of data elements in the queue, this operation is used.
  • By applying this operation, data is entered in the queue according to the sequence after one another. En queue will be continued till the queue reaches its end limit.
  • After it reaches the endpoint, the data cannot add to the queue, and then such condition is said to be an Overflow condition.
  • En-queue operation is done from the rear end.

2. Dequeue

  • For deletion of a data element from the queue, this operation is used.
  • By Using this operation, that data element is deleted, which is enqueued first in the queue, or the elements are popped in the same order in which they are pushed in the queue.
  • This process will delete the data elements from the queue until the whole queue becomes empty. Once all the elements are deleted, then the deletion operation is unable to execute, and then such condition is said to be an Underflow condition.
  • It is done from the front end.

3. Peek

 This operation is used to find out the very first queue element, which is to be served first without dequeuing it.

Two Ends of Linear Queue

  1. Front end- It refers to the initial or starts position of the queue. The front end is mainly used to delete the data element from the queue or perform a de queue operation.
  2. Rear end- It refers to the last or back position of the queue. The rear end is mainly used to insert the data elements in the queue or to perform the en-queue operation.

Methods of Implementation of Linear queue

There are various methods of implementation of the queue:

  • Queue implementation using an array
  • Queue implementation using stack
  • Queue implementation using linked list

Implementation of Linear queue using an Array

Here we discuss the implementation of queue using an array->

Queues can easily be implemented by using linear arrays. As stated earlier, the queue has front and rear pointers that point to the position where deletion and insertion of data elements can be done.

Steps

  1. Initially, we need to create an array of size 'n' whether statically or dynamically depends on the user.
  2. Then declare two pointers named FRONT and REAR.
  3. Initialize REAR = -1 and FRONT = 0.
  4. Create three functions named Enqueue for adding data elements in the queue, Dequeue for deletion of a data element from the queue, and peek for finding out the first element from the queue without dequeuing it.
Difference between Linear Queue and Circular Queue

                                 Implementation of linear queue

Algorithms

Algorithms of Enqueue operation

Step 1- Check if REAR == FRONT.

Step 2- If the above statement holds, then such a condition is said to be an Overflow, and further insertion of data elements is not possible.

Step 3- If it holds false, then

  • set REAR = REAR + 1 and
  • Add the data element in queue at rear position. 
  • Queue[REAR] = data.

Algorithm of Dequeue operation

Step 1- Check if FRONT = REAR + 1

Step 2- If the above statement holds, then

  • Such condition is said to be Underflow condition.
  • Return -1.

Step 3- If it holds false, then

  • popped out the value from the queue present at the front position
  • Data = Queue[FRONT].
  • Return data.

     Step 4-> Set FRONT = FRONT + 1, it points next data element.

Algorithm of Peek operation

Step 1- Check if FRONT = REAR + 1.

Step 2- If the above statement holds, then display the message; the queue is empty.

Step 3- If it holds false,

  • Return the data element from the queue present at the front position.
  • Data = Queue[FRONT].
  • Return data.

Circular Queue

A circular queue is also one of the important types of a queue. It is similar to the linear queue but has some variations. The end position of a circular queue is connected back to the start position, forming the circular-like structure and making a circle. A circular queue is also based on the First In First Out (FIFO) principle. It is also known as 'Ring Buffer'.

As we have seen previously, in a linear queue, further insertion of data elements is not possible when REAR reaches the end, and FRONT also reaches to end. The queue is still empty, but it shows that the queue is full, so to overcome the above problem concept of circular is introduced. In the circular queue, this problem will not arise. We will further discuss it in more detail in the below section.

Difference between Linear Queue and Circular Queue

                                            Circular queue

Applications of Circular queue

  • Round Robin scheduling is one of the major applications of the circular queue. This algorithm is employed by process and network schedulers in computing.
  • It is also used in computer-controlled Traffic Signal Systems.
  • It is also used in CPU scheduling and memory management.

Operations performed on Circular queue

There are certain basic operations has performed in the linear queue are as follows->

1. Enqueue

  • This operation is used for the insertion of a data element in the queue.
  • In a circular queue, the addition of a data element is always done from the rear position.

2. Dequeue

  • This operation is used for the removal of a data element from the queue.
  • In a circular queue, deletion of a data element is always done from the front position.

Two ends of Circular queue

In a circular queue, there is a circle like structure; hence there are no two ends distinguished but still, to differentiate the insertion and deletion criteria of data elements, we refer to these two ends:

  1. Front end-> it is used to delete the data elements from the queue.
  2. Rear end-> it is used to insert the data elements in the queue.

Implementation of Circular queue using an array

A circular queue can easily be implemented using arrays. It works on the circular increment process. As stated earlier, it has two pointers, front, and rear. From the rear end, insertion of data elements is possible. If the rear pointer reaches the end, it again increments back to the initial position of the queue so that further insertions can also be possible; hence this implies an increment process.

From the front end, deletion of data elements is possible. If the front pointer reaches to end, it returns to the queue's initial position so that further deletions can be possible.   

By using modulo division with the queue size, the circular increment is performed.

Steps

  1. Initially, we need to create an array of size 'n' whether statically or dynamically depends on the user.
  2. Then declare two pointers named FRONT and REAR.
  3. Initialize REAR = -1 and FRONT = -1.
  4. Create two functions named Enqueue for adding data elements in the queue,  Dequeue for the deletion of data elements from the queue.
Difference between Linear Queue and Circular Queue

                                 Implementation of Circular queue

Algorithm of Enqueue operation

Step 1-> Check if FRONT == 0 && REAR == (n-1) || REAR == (FRONT – 1) % (n-1).

Step 2- If the above statement holds, then such condition is said to be an Overflow, and further insertion of data elements is not possible.

Step 3- Else if FRONT == -1 then,

  • set REAR = 0, FRONT = 0 and
  • Add the data element in queue at rear position. 
  • Queue[REAR] = data.

Step 4- Else if REAR == (n-1) &&  FRONT != 0

  • set REAR = 0 and
  • Add the data element in queue at rear position. 
  • Queue[REAR] = data.

      Step 5- Else

  • set REAR = REAR + 1
  • Add the data element in queue at rear position. 
  • Queue[REAR] = data.

Algorithm of Dequeue operation

     Step 1-Check if FRONT == -1

Step 2- If the above statement holds, then

  • Such condition is said to be an Underflow condition.
  • Return -1.

     Step 3- If it holds false, then

  • popped out value from the queue present at the front position
  • Data = Queue[FRONT].
  • Return data.

     Step 4- Set FRONT = FRONT + 1, it points next data element.

      Step 5-If FRONT == REAR then, set FRONT = -1 and REAR = -1.

     Step 6-> Else if FRONT == n-1 then, set FRONT = 0.

Advantages of Circular queue over Linear queue->

  • Linear queue consumes more memory as compared to circular queue.
  • A circular queue uses an efficient way for memory utilization.
  • In a circular queue, new data can be inserted again at a particular position after deleting previous data on that position.
  • In a linear queue, if the rear pointer reaches last and the front pointer deletes all data from the queue, it remains to show the message of Overflow, which is the main drawback of the linear queue.
  •  In a circular queue, an overflow message is shown when the queue is full.
  • Easy to perform dequeue operation and enqueue operation.
On the basis of comparisonLinear QueueCircular queue
 Arrangement It arranges data in linear order.It arranges data in circular order.
DefinitionIt is a linear data structure that contains the data in linear sequential order. Here rear end is not connected with the front end.It is also a linear data structure in which the rear end is connected with the front end.
Insertion and Deletion operationInsertion is done from the rear end, whereas deletion is done from the front end. Hence here it is fixed.     
Difference between Linear Queue and Circular Queue
Due to a circular structure, we can not distinguish the exact position of insertion and deletion. Hence here, it is not fixed.
Difference between Linear Queue and Circular Queue
Peek operationIn a linear queue, we can easily fetch out the peek value.As it is circularly arranged, we cannot fetch out the peek value.
Memory spaceIt requires more memory space.It requires less memory space.
Utilization of memoryIn a linear queue, there is an inefficient way of the utilization of memory.In a circular queue, there is an efficient way of the utilization of memory.

Related Topics

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Stack Using Array

Stack – A Stack is a linear abstract data type used to store elements. It is also called last in first out or first in last out data structure because...

6 minutes read.

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.