×

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

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.

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list This article will explain how to delete a node without a head pointer from the linked list. We have given a...

2 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

6 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Difference between B-tree and Binary Tree

What is B-TREE? The nodes of B-tree are sorted during in-order traversal, and it is called self-balancing tree. A node in a B-tree can have more than two offspring, in contrast...

3 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

How to Start Learning DSA

All programmer experiences a point along the way where they wish they could approach a problem in a more effective manner. They finally learn about the terminology DSA while trying...

10 minutes read.

Given a Binary Tree Return All Root-to-Leaf Paths

Implementation #include <bits/stdc++.h> using namespace std; // A binary tree node generally consists of data, a pointer to the left and right child, and a pointer to the right child.  class __nod { public: int record; __nod* Lft; __nod*...

9 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Strings in Data Structures

Strings and functions in C A string is a collection of characters. We'll learn how to declare strings, operate with strings in C programming, and use pre-defined string handling routines. We'll look...

7 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.