Queue is a linear data structure that follows the FIFO (First In First Out) principle, where insertion is done at the rear end and deletion is done from the front end.
The following are some fundamental operations that allow us to add, remove, and access elements efficiently.
- enqueue() - Insertion of elements to the queue.
- dequeue() - Removal of elements from the queue.
- getFront()- Acquires the data element available at the front node of the queue without deleting it.
- getRear() - This operation returns the element at the rear end without removing it.
- 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.
We will now see how to perform these operations on Queue.
enqueue()
Enqueue operation inserts an element at the end of the queue i.e. at the rear end.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
// inserting elements at rear
q.push(1);
q.push(8);
q.push(3);
return 0;
}
import java.util.LinkedList;
import java.util.Queue;
class GfG {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
// inserting elements at rear
q.add(10);
q.add(20);
q.add(30);
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
# inserting elements at rear
q.append(10)
q.append(20)
q.append(30)
using System;
using System.Collections.Generic;
class GfG {
static void Main() {
Queue<int> q = new Queue<int>();
// inserting elements at rear
q.Enqueue(10);
q.Enqueue(20);
q.Enqueue(30);
}
}
// Driver Code
// using array as queue
let q = [];
// inserting elements at rear
q.push(10);
q.push(20);
q.push(30);
Time Complexity: O(1), since inserting at the rear takes constant time.
Auxiliary Space: O(1)
Note: If the queue is implemented using a fixed-size array, inserting an element into a full stack will cause an overflow condition.
dequeue()
Dequeue operation removes element that is at the front end of the queue.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
// inserting elements at rear
q.push(1);
q.push(8);
q.push(3);
// deleting element from front
q.pop();
return 0;
}
import java.util.LinkedList;
import java.util.Queue;
public class GfG{
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
// inserting elements at rear
q.add(1);
q.add(8);
q.add(3);
// deleting element from front
q.poll();
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
# inserting elements at rear
q.append(1)
q.append(8)
q.append(3)
# deleting element from front
q.popleft()
using System;
using System.Collections.Generic;
public class GfG {
public static void Main() {
Queue<int> q = new Queue<int>();
// inserting elements at rear
q.Enqueue(1);
q.Enqueue(8);
q.Enqueue(3);
// deleting element from front
q.Dequeue();
}
}
// Driver Code
let q = [];
// inserting elements at rear
q.push(1);
q.push(8);
q.push(3);
// deleting element from front
q.shift();
Time Complexity: O(1), since deleting from the front takes constant time.
In JavaScript, there’s no built-in queue, so we use arrays. Removing elements with q.shift() takes O(n) time because all elements are re-indexed.
Auxiliary Space: O(1)
Note: If a queue is empty, deleting an element will cause an underflow condition.
getFront()
getFront will returns the element at the front end of the queue without removing it.

#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
// inserting elements at rear
q.push(1);
q.push(8);
q.push(3);
// getting the front element
cout << q.front() << endl;
return 0;
}
import java.util.LinkedList;
import java.util.Queue;
public class GfG {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
// inserting elements at rear
q.add(1);
q.add(8);
q.add(3);
// getting the front element
System.out.println(q.peek());
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
# inserting elements at rear
q.append(1)
q.append(8)
q.append(3)
# getting the front element
print(q[0])
using System;
using System.Collections.Generic;
public class GfG {
public static void Main() {
Queue<int> q = new Queue<int>();
// inserting elements at rear
q.Enqueue(1);
q.Enqueue(8);
q.Enqueue(3);
// getting the front element
Console.WriteLine(q.Peek());
}
}
// Driver Code
let q = [];
// inserting elements at rear
q.push(1);
q.push(8);
q.push(3);
// getting the front element
console.log(q[0]);
Output
1
Time Complexity: O(1), since accessing the front element takes constant time.
Auxiliary Space: O(1)
getRear()
getRear will returns the element at the rear end without removing it.

#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(1);
q.push(8);
q.push(3);
// get rear element
cout << q.back() << endl;
return 0;
}
import java.util.LinkedList;
import java.util.Queue;
class GfG {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(8);
q.add(3);
// get rear element (last enqueued)
System.out.println(((LinkedList<Integer>) q).getLast());
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
q.append(1)
q.append(8)
q.append(3)
# get rear element
print(q[-1])
using System;
using System.Collections.Generic;
using System.Linq;
class GfG {
public static void Main() {
Queue<int> q = new Queue<int>();
q.Enqueue(1);
q.Enqueue(8);
q.Enqueue(3);
// get rear element
Console.WriteLine(q.Last());
}
}
// Driver Code
let q = [];
q.push(1);
q.push(8);
q.push(3);
// get rear element
console.log(q[q.length - 1]);
Output
3
Time Complexity: O(1)
Auxiliary Space: O(1)
isEmpty
isEmpty returns a boolean value that indicates whether the queue is empty or not.

#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(5);
q.push(9);
// check if queue is empty
if (q.empty())
cout << "Queue is empty" << endl;
else
cout << "Queue is not empty" << endl;
return 0;
}
import java.util.LinkedList;
import java.util.Queue;
public class GfG {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(5);
q.add(9);
// check if queue is empty
if (q.isEmpty())
System.out.println("Queue is empty");
else
System.out.println("Queue is not empty");
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
q.append(5)
q.append(9)
# check if queue is empty
if not q:
print("Queue is empty")
else:
print("Queue is not empty")
using System;
using System.Collections.Generic;
public class GfG {
public static void Main() {
Queue<int> q = new Queue<int>();
q.Enqueue(5);
q.Enqueue(9);
// check if queue is empty
if (q.Count == 0)
Console.WriteLine("Queue is empty");
else
Console.WriteLine("Queue is not empty");
}
}
// Driver Code
let q = [];
q.push(5);
q.push(9);
// check if queue is empty
if (q.length === 0)
console.log("Queue is empty");
else
console.log("Queue is not empty");
Output
Queue is not empty
Time Complexity: O(1)
Auxiliary Space: O(1)
Size()
Size returns the size of the queue i.e. the total number of elements it contains.

#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(2);
q.push(7);
q.push(4);
// get size of queue
cout << q.size() << endl;
return 0;
}
import java.util.Queue;
import java.util.LinkedList;
public class GfG {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(2);
q.add(7);
q.add(4);
// get size of queue
System.out.println(q.size());
}
}
from collections import deque
if __name__ == "__main__":
q = deque()
q.append(2)
q.append(7)
q.append(4)
# get size of queue
print(len(q))
using System;
using System.Collections.Generic;
public class GfG {
public static void Main() {
Queue<int> q = new Queue<int>();
q.Enqueue(2);
q.Enqueue(7);
q.Enqueue(4);
// get size of queue
Console.WriteLine(q.Count);
}
}
// Driver Code
let q = [];
q.push(2);
q.push(7);
q.push(4);
// get size of queue
console.log(q.length);
Time Complexity: O(1)
Auxiliary Space: O(1)