Basic Operations for Queue in Data Structure
Last Updated :
03 Jan, 2023
Basic Operations on Queue:
Some of the basic operations for Queue in Data Structure are:
- enqueue() – Insertion of elements to the queue.
- dequeue() – Removal of elements from the queue.
- peek() or front()- Acquires the data element available at the front node of the queue without deleting it.
- rear() – This operation returns the element at the rear end without removing it.
- isFull() – Validates if the queue is full.
- isEmpty() – Checks if the queue is empty.
- size(): This operation returns the size of the queue i.e. the total number of elements it contains.

Queue Data Structure
Operation 1: enqueue()
Inserts an element at the end of the queue i.e. at the rear end.
The following steps should be taken to enqueue (insert) data into a queue:
- Check if the queue is full.
- If the queue is full, return overflow error and exit.
- If the queue is not full, increment the rear pointer to point to the next empty space.
- Add the data element to the queue location, where the rear is pointing.
- return success.

Enqueue representation
Below is the Implementation of the above approach:
C++
void queueEnqueue(int data)
{
if (capacity == rear) {
printf("\nQueue is full\n");
return;
}
else {
queue[rear] = data;
rear++;
}
return;
}
|
Java
void queueEnqueue(int data)
{
if (capacity == rear) {
System.out.println("\nQueue is full\n");
return;
}
else {
queue[rear] = data;
rear++;
}
return;
}
|
C
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}
|
Python3
def EnQueue(self, item):
if self.isFull():
print("Full")
return
self.rear = (self.rear + 1) % (self.capacity)
self.Q[self.rear] = item
self.size = self.size + 1
print("% s enqueued to queue" % str(item))
|
C#
public void enqueue(int item)
{
if (rear == max - 1) {
Console.WriteLine("Queue Overflow");
return;
}
else {
ele[++rear] = item;
}
}
|
Javascript
<script>
enqueue(element){
this.items.push(element);
}
</script>
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 2: dequeue()
This operation removes and returns an element that is at the front end of the queue.
The following steps are taken to perform the dequeue operation:
- Check if the queue is empty.
- If the queue is empty, return the underflow error and exit.
- If the queue is not empty, access the data where the front is pointing.
- Increment the front pointer to point to the next available data element.
- The Return success.

Dequeue operation
Below is the Implementation of above approach:
C++
void queueDequeue()
{
if (front == rear) {
printf("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
rear--;
}
return;
}
|
Java
void queueDequeue()
{
if (front == rear) {
System.out.println("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
rear--;
}
return;
}
|
C
int dequeue(struct Queue* queue)
{
if (isEmpty(queue)) {
printf("\nQueue is empty\n");
return;
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
|
Python3
def DeQueue(self):
if self.isEmpty():
print("Queue is empty")
return
print("% s dequeued from queue" % str(self.Q[self.front]))
self.front = (self.front + 1) % (self.capacity)
self.size = self.size - 1
|
C#
public int dequeue()
{
if (front == rear + 1) {
Console.WriteLine("Queue is Empty");
return -1;
}
else {
int p = ele[front++];
return p;
}
}
|
Javascript
<script>
dequeue(){
if(this.isEmpty()){
document.write("<br>Queue is empty<br>");
return -1;
}
return this.items.shift();
}
</script>
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 3: front()
This operation returns the element at the front end without removing it.
The following steps are taken to perform the front operation:
- If the queue is empty return the most minimum value.
- otherwise, return the front value.
Below is the Implementation of the above approach:
C++
int front(Queue* queue)
{
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->front];
}
|
Java
int front(Queue queue)
{
if (isempty(queue))
return Integer.MIN_VALUE;
return queue.arr[queue.front];
}
|
C
int front(struct Queue* queue)
{
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->front];
}
|
Python3
def que_front(self):
if self.isempty():
return "Queue is empty"
return self.Q[self.front]
|
C#
public int front()
{
if (isempty())
return INT_MIN;
return arr[front];
}
|
Javascript
<script>
front(){
if(this.isEmpty())
return "No elements in Queue<br>";
return this.items[0];
}
<script>
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 4 : rear()
This operation returns the element at the rear end without removing it.
The following steps are taken to perform the rear operation:
- If the queue is empty return the most minimum value.
- otherwise, return the rear value.
Below is the Implementation of the above approach:
C++
int rear(Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->arr[queue->rear];
}
|
Java
int rear(Queue queue)
{
if (isEmpty(queue))
return Integer.MIN_VALUE;
return queue.arr[queue.rear];
}
|
C
int front(struct Queue* queue)
{
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->rear];
}
|
Python3
def que_rear(self):
if self.isEmpty():
return "Queue is empty"
return self.Q[self.rear]
|
C#
public int front()
{
if (isempty())
return INT_MIN;
return arr[rear];
}
|
Javascript
<script>
rear(){
if(this.isEmpty())
return "No elements in Queue<br>";
return this.items[this.items.length-1];
}
<script>
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 5: isEmpty():
This operation returns a boolean value that indicates whether the queue is empty or not.
The following steps are taken to perform the Empty operation:
- check if front value is equal to -1 or not, if yes then return true means queue is empty.
- Otherwise return false, means queue is not empty
Below is the implementation of the above approach:
C++
bool isEmpty()
{
if (front == -1)
return true;
else
return false;
}
|
Java
boolean isEmpty()
{
if (front == -1)
return true;
else
return false;
}
|
C#
bool isEmpty()
{
if (front == -1)
return true;
else
return false;
}
|
C
bool isEmpty(struct Queue* queue)
{
return (queue->size == 0);
}
|
Python3
def isEmpty(self):
return self.size == 0
|
Javascript
</script>
isEmpty(){
return this.items.length == 0;
}
</script>
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 6 : isFull()
This operation returns a boolean value that indicates whether the queue is full or not.
The following steps are taken to perform the isFull() operation:
- Check if front value is equal to zero and rear is equal to the capacity of queue if yes then return true.
- otherwise return false
Below is the Implementation of the above approach:
C++
bool isFull()
{
if (front == 0 && rear == MAX_SIZE - 1) {
return true;
}
return false;
}
|
Java
boolean isFull()
{
if (front == 0 && rear == MAX_SIZE - 1) {
return true;
}
return false;
}
|
C
bool isFull(struct Queue* queue)
{
return (queue->size == queue->capacity);
}
|
C#
public bool isFull(int item) { return (rear == max - 1); }
|
Python3
def isFull(self):
return self.size == self.capacity
|
Javascript
function isFull() {
if (front == 0 && rear == MAX_SIZE - 1) {
return true;
}
return false;
}
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 7: size()
This operation returns the size of the queue i.e. the total number of elements it contains.
queuename.size()
Parameters :
No parameters are passed
Returns :
Number of elements in the container
C++
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int sum = 0;
queue<int> myqueue;
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);
cout << myqueue.size();
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
int sum = 0;
Queue<Integer> myqueue = new LinkedList<>();
myqueue.add(1);
myqueue.add(8);
myqueue.add(3);
myqueue.add(6);
myqueue.add(2);
System.out.println(myqueue.size());
}
}
|
Python
from collections import deque
def main():
sum = 0
myqueue = deque()
myqueue.append(1)
myqueue.append(8)
myqueue.append(3)
myqueue.append(6)
myqueue.append(2)
print(len(myqueue))
main()
|
C#
using System;
using System.Collections.Generic;
namespace ConsoleApp1 {
class MainClass {
public static void Main(string[] args)
{
int sum = 0;
Queue<int> myqueue = new Queue<int>();
myqueue.Enqueue(1);
myqueue.Enqueue(8);
myqueue.Enqueue(3);
myqueue.Enqueue(6);
myqueue.Enqueue(2);
Console.WriteLine(myqueue.Count);
}
}
}
|
Javascript
let sum = 0;
let myqueue=[];
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);
console.log(myqueue.length);
|
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Similar Reads
Basic Operations for Queue in Data Structure
Basic Operations on Queue: Some of the basic operations for Queue in Data Structure are: enqueue() - Insertion of elements to the queue.dequeue() - Removal of elements from the queue.peek() or front()- Acquires the data element available at the front node of the queue without deleting it.rear() - Th
11 min read
Introduction to Queue Data Structure
Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is the first to be popped out. FIFO Principle in Queue: FIFO Principle states that the first element added to the Queue will be the first one to be removed or processed. So, Queue is like
6 min read
Design an efficient data structure for given operations
To design an efficient data structure for a specific set of operations, it's important to consider the time and space complexity of different data structures and choose the one that is best suited for the specific requirements. For example, if you need to perform operations such as inserting element
15+ min read
Applications of Queue Data Structure
Introduction : A queue is a linear data structure that follows the "first-in, first-out" (FIFO) principle. It is a collection of elements that supports two primary operations - enqueue and dequeue. In the enqueue operation, an element is added to the back of the queue, while in the dequeue operation
5 min read
Common operations on various Data Structures
Data Structure is the way of storing data in computer's memory so that it can be used easily and efficiently. There are different data-structures used for the storage of data. It can also be defined as a mathematical or logical model of a particular organization of data items. The representation of
15+ min read
STL Priority Queue for Structure or Class
STL priority_queue is the implementation of Heap Data-structure. By default, it's a max heap and we can easily use it for primitive datatypes. There are some important applications of it which can be found here Prerequisite: Prioirty_queue Basics In this article, we will see how can we use priority_
3 min read
What is an in-memory Queue in Data Structures
What is an in-memory Queue? An in-memory queue is a queue that stores data in memory. In-memory queues are used to improve application performance by providing a fast, low-latency way to access data. They are often used in conjunction with other data storage mechanisms, such as databases, to provide
5 min read
What is Queue Data Structure?
What is Queue Data Structure?A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at
2 min read
Queue Data Structure
A Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. Queues are commonly used in various algorit
2 min read
Top 50 Problems on Queue Data Structure asked in SDE Interviews
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element w
3 min read