Basic implementation of a queue in Java
A queue is a fundamental data structure in computer science that follows the First-In-First-Out (FIFO) principle. In a queue, the first element added is the first one to be removed. Think of it as a collection of elements with two main operations: enqueue, which adds an element to the rear of the queue, and dequeue, which removes the element from the front.
Key Characteristics of a Queue:
- FIFO Order:
- The first element added is the first one to be removed.
2. Two Main Operations:
- Enqueue: Adds an element to the rear of the queue.
- Dequeue: Removes the element from the front of the queue.
3. Front and Rear:
- The front of the queue represents the least recently added element.
- The rear of the queue represents the most recently added element.
4. No Random Access:
- Elements can only be added to the rear and removed from the front.
5. Common Use Cases:
- Print queue in a printer.
- Task scheduling in operating systems.
- Breadth-First Search (BFS) algorithm.
- Order processing systems.
public class QueueArray {
private int maxSize;
private int[] queArray;
private int front;
private int rear;
private int nItems;
public QueueArray(int maxSize) {
this.queArray = new int[maxSize];
this.maxSize = maxSize;
this.front = 0;
this.rear = -1;
this.nItems = 0;
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (this.nItems == this.maxSize);
}
public int size() {
return this.nItems;
}
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
if (rear == maxSize - 1) {
rear = -1;
}
queArray[++rear] = item;
nItems++;
System.out.println("Enqueued " + item);
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
int temp = this.queArray[front];
if (this.front == maxSize - 1) {
this.front = 0;
} else {
this.front++;
}
nItems--;
System.out.println("Dequeued " + temp);
return temp;
}
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return queArray[front];
}
public void display() {
int i = this.front;
while (i != this.rear) {
System.out.print(this.queArray[i] + " ");
if (i == maxSize - 1) {
i = 0;
} else {
i++;
}
}
System.out.println(this.queArray[this.rear]);
System.out.println();
}
}
public class Main {
public static void main(String[] args) throws Exception {
QueueArray queue = new QueueArray(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
queue.display();
queue.dequeue();
queue.dequeue();
queue.display();
System.out.println("Peek: " + queue.peek());
}
}
Output:
Enqueued 10
Enqueued 20
Enqueued 30
Enqueued 40
Enqueued 50
10 20 30 40 50
Dequeued 10
Dequeued 20
30 40 50
Peek: 30
This QueueArray
class provides a basic implementation of a queue using an array with common queue operations. As with any data structure, it's important to handle edge cases to ensure proper behavior, such as attempting to enqueue to a full queue or dequeue from an empty queue.