Data Structures — Queue
3 min readOct 22, 2025
What is a Queue?
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle — the first element inserted is the first one removed.
Get Mandar Deepak Bagwe’s stories in your inbox
Join Medium for free to get updates from this writer.
Think of it like a line of people at a ticket counter — the person who comes first gets served first.
Front → [A][B][C][D] ← Rear
Dequeue from Front | Enqueue at RearBasic Queue Operations
| ---------------- | ------------------------------------------------- | --------------- |
| Operation | Description | Time Complexity |
| ---------------- | ------------------------------------------------- | --------------- |
| enqueue(x) | Insert element at the rear | O(1) |
| dequeue() | Remove element from the front | O(1) |
| peek() / front() | View the front element | O(1) |
| isEmpty() | Check if queue is empty | O(1) |
| isFull() | Check if queue is full (for static array version) | O(1) |
| traverse() | Print all elements | O(n) |
| ---------------- | ------------------------------------------------- | --------------- |Java Implementation (Array-Based Queue)
public class Queue {
private int[] queue;
private int front;
private int rear;
private int size;
private int capacity;
public Queue(int capacity){
this.capacity = capacity;
queue = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int value){
if(size == capacity){
System.out.println("Queue is full");
return;
}
queue[++rear] = value;
size++;
}
public int dequeue(){
if(size == 0){
System.out.println("Queue is empty");
return -1;
}
int value = queue[front++];
size--;
return value;
}
public int peek(){
if(size == 0){
System.out.println("Queue is empty");
return -1;
}
return queue[front];
}
public boolean isEmpty(){
return size == 0;
}
public boolean isFull(){
return size == capacity;
}
public void traverse(){
if(size == 0){
System.out.println("Queue is empty");
return;
}
for(int i = front; i <= rear; i++){
System.out.print(queue[i] + " ");
}
}
}Example Run
public class Main {
public static void main(String[] args) {
Queue q = new Queue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.traverse(); // 10 20 30
System.out.println("Dequeued: " + q.dequeue()); // 10
q.traverse(); // 20 30
System.out.println("Front: " + q.peek()); // 20
}
}Output:
10 20 30
Dequeued: 10
20 30
Front: 20Drawbacks of Simple Array Queue
- When elements are dequeued, the front stays fixed, and you end up wasting space at the beginning.
- Solution: Circular Queue.
Circular Queue Implementation — Java
public class CircularQueue {
private int[] queue;
private int front;
private int rear;
private int size;
private int capacity;
public CircularQueue(int capacity){
this.capacity = capacity;
queue = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int data){
if(size == capacity){
System.out.println("Queue is full");
return;
}
rear = (rear + 1) % capacity;
queue[rear] = data;
size++;
}
public int dequeue(){
if(size == 0){
System.out.println("Queue is empty");
return -1;
}
front = (front + 1) % capacity;
size--;
return queue[front];
}
public int peek(){
if(size == 0){
System.out.println("Queue is empty");
return -1;
}
return queue[front];
}
public boolean isEmpty(){
return size == 0;
}
public boolean isFull(){
return size == capacity;
}
public void traverse(){
if(size == 0){
System.out.println("Queue is empty");
return;
}
for(int i = 0; i < size; i++){
System.out.print(queue[(front + i) % capacity] + " ");
}
}
}Example Run
public class Main {
public static void main(String[] args) {
Queue q = new Queue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.traverse(); // 10 20 30
System.out.println("Dequeued: " + q.dequeue()); // 10
q.traverse(); // 20 30
System.out.println("Front: " + q.peek()); // 20
}
}Output:
10 20 30
Dequeued: 20
20 30
Front: 20Real-Life Applications of Queue
- CPU Scheduling: OS manages process execution order using queues
- Printer Spooling: Print jobs queued until printer is free
- Customer Service Systems: Calls/messages handled in order received
- Network Buffers: Packets queued for transmission
- Breadth-First Search (BFS): Graph traversal uses queue
- Order Management Systems: Orders processed FIFO
- Messaging Systems: Message queues (Kafka, RabbitMQ, etc.)







