Queue Introduction
In this page:
What is a Queue?
A queue is a linear data structure where elements are processed in the exact order they arrive, a rule called FIFO, First In First Out, unlike a stack's LIFO order. Real-world examples include a printer queue or a line at a ticket counter, where the first person to arrive is the first one served.
Example: What is a Queue?
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> q;
q.push("Person A"); q.push("Person B"); q.push("Person C");
cout << "Served first: " << q.front();
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<String> q = new LinkedList<>();
q.add("Person A"); q.add("Person B"); q.add("Person C");
System.out.println("Served first: " + q.peek());
}
}
from collections import deque
q = deque()
q.append("Person A")
q.append("Person B")
q.append("Person C")
print("Served first:", q[0])
#include <stdio.h>
int main() {
char *q[3] = {"Person A", "Person B", "Person C"};
printf("Served first: %s", q[0]);
return 0;
}
Login to try C/C++/Java code in the editor
Enqueue Operation
Enqueue is the operation that adds a new element, and it always happens at the rear (back) of the queue, joining behind whatever elements are already waiting. In an array-based implementation, this typically means incrementing a rear pointer and placing the new element at that index.
Example: Enqueue Operation
#include <iostream>
using namespace std;
int main() {
int q[5], rear = -1;
q[++rear] = 10;
q[++rear] = 20;
q[++rear] = 30;
for (int i = 0; i <= rear; i++) cout << q[i] << " ";
return 0;
}
public class Main {
public static void main(String[] args) {
int[] q = new int[5];
int rear = -1;
q[++rear] = 10;
q[++rear] = 20;
q[++rear] = 30;
for (int i = 0; i <= rear; i++) System.out.print(q[i] + " ");
}
}
q = [None] * 5
rear = -1
for val in (10, 20, 30):
rear += 1
q[rear] = val
print(q[:rear + 1])
#include <stdio.h>
int main() {
int q[5], rear = -1;
q[++rear] = 10;
q[++rear] = 20;
q[++rear] = 30;
for (int i = 0; i <= rear; i++) printf("%d ", q[i]);
return 0;
}
Login to try C/C++/Java code in the editor
Dequeue Operation
Dequeue is the operation that removes an element, and it always happens at the front of the queue, taking whichever element has been waiting the longest. In an array-based implementation, this means reading the element at the front pointer and then incrementing that pointer forward.
Example: Dequeue Operation
#include <iostream>
using namespace std;
int main() {
int q[5] = {10, 20, 30};
int front = 0, rear = 2;
cout << "Removed: " << q[front] << endl;
front++;
cout << "New front: " << q[front];
return 0;
}
public class Main {
public static void main(String[] args) {
int[] q = {10, 20, 30};
int front = 0;
System.out.println("Removed: " + q[front]);
front++;
System.out.println("New front: " + q[front]);
}
}
q = [10, 20, 30]
front = 0
print("Removed:", q[front])
front += 1
print("New front:", q[front])
#include <stdio.h>
int main() {
int q[3] = {10, 20, 30};
int front = 0;
printf("Removed: %d\n", q[front]);
front++;
printf("New front: %d", q[front]);
return 0;
}
Login to try C/C++/Java code in the editor
Queue Front and Rear
The front points to the next element that will be removed, and the rear points to where the next new element will be added, and keeping these two positions straight is the core bookkeeping a queue implementation needs.
Example: Queue Front and Rear
#include <iostream>
using namespace std;
int main() {
int q[5], front = 0, rear = -1;
q[++rear] = 5; q[++rear] = 15;
cout << "Front index: " << front << ", Rear index: " << rear << endl;
front++;
cout << "After dequeue, front index: " << front;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] q = new int[5];
int front = 0, rear = -1;
q[++rear] = 5; q[++rear] = 15;
System.out.println("Front index: " + front + ", Rear index: " + rear);
front++;
System.out.println("After dequeue, front index: " + front);
}
}
q = [None] * 5
front, rear = 0, -1
for val in (5, 15):
rear += 1
q[rear] = val
print("Front index:", front, ", Rear index:", rear)
front += 1
print("After dequeue, front index:", front)
#include <stdio.h>
int main() {
int q[5], front = 0, rear = -1;
q[++rear] = 5; q[++rear] = 15;
printf("Front index: %d, Rear index: %d\n", front, rear);
front++;
printf("After dequeue, front index: %d", front);
return 0;
}
Login to try C/C++/Java code in the editor
Queue Applications
Queues model real waiting lines directly: task scheduling, buffering data as it streams in, print job queues, and breadth-first search, where nodes are explored in the same order they're discovered.
Example: Queue Applications
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> printJobs;
printJobs.push("doc1.pdf"); printJobs.push("doc2.pdf"); printJobs.push("doc3.pdf");
while (!printJobs.empty()) {
cout << "Printing: " << printJobs.front() << endl;
printJobs.pop();
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<String> printJobs = new LinkedList<>();
printJobs.add("doc1.pdf"); printJobs.add("doc2.pdf"); printJobs.add("doc3.pdf");
while (!printJobs.isEmpty()) {
System.out.println("Printing: " + printJobs.poll());
}
}
}
from collections import deque
print_jobs = deque(["doc1.pdf", "doc2.pdf", "doc3.pdf"])
while print_jobs:
print("Printing:", print_jobs.popleft())
#include <stdio.h>
int main() {
char *printJobs[3] = {"doc1.pdf", "doc2.pdf", "doc3.pdf"};
for (int i = 0; i < 3; i++) printf("Printing: %s\n", printJobs[i]);
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: