The main operations performed on a queue are:
public class ArrayQueue {
private int[] arr;
private int front;
private int rear;
private int size;
public ArrayQueue(int capacity) {
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int item) {
if (isFull()) {
throw new IllegalStateException("Queue is full");
}
rear = (rear + 1) % arr.length;
arr[rear] = item;
size++;
}
public int dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
int item = arr[front];
front = (front + 1) % arr.length;
size--;
return item;
}
public int peek() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return arr[front];
}
public boolean isFull() {
return size == arr.length;
}
public boolean isEmpty() {
return size == 0;
}
}
public class LinkedListQueue {
private class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
private Node front;
private Node rear;
public LinkedListQueue() {
front = null;
rear = null;
}
public void enqueue(int item) {
Node newNode = new Node(item);
if (isEmpty()) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
int item = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return item;
}
public int peek() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return front.data;
}
public boolean isEmpty() {
return front == null;
}
}
front
and rear
, to keep track of the front and rear of the queue.rear
pointer is incremented.front
pointer is incremented.rear
pointer reaches the end of the array, it wraps around to the beginning.public class CircularQueue {
private int[] queue;
private int front;
private int rear;
private int size;
public CircularQueue(int capacity) {
queue = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public boolean isFull() {
return size == queue.length;
}
public boolean isEmpty() {
return size == 0;
}
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full. Cannot enqueue.");
return;
}
rear = (rear + 1) % queue.length;
queue[rear] = item;
size++;
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
}
int item = queue[front];
front = (front + 1) % queue.length;
size--;
return item;
}
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return queue[front];
}
}
import java.util.Arrays;
public class PriorityQueue {
private int[] queue;
private int size;
public PriorityQueue() {
queue = new int[10]; // Initial capacity of the queue
size = 0;
}
public void enqueue(int item) {
if (size == queue.length) {
// Queue is full, resize the array
queue = Arrays.copyOf(queue, size * 2);
}
int position = findInsertionPosition(item);
shiftElementsToRight(position);
queue[position] = item;
size++;
}
public int dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
int item = queue[0];
shiftElementsToLeft();
size--;
return item;
}
public int peek() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return queue[0];
}
public boolean isEmpty() {
return size == 0;
}
private int findInsertionPosition(int item) {
int position = 0;
while (position < size && item > queue[position]) {
position++;
}
return position;
}
private void shiftElementsToRight(int position) {
for (int i = size - 1; i >= position; i--) {
queue[i + 1] = queue[i];
}
}
private void shiftElementsToLeft() {
for (int i = 1; i < size; i++) {
queue[i - 1] = queue[i];
}
}
}
import java.util.NoSuchElementException;
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
public class LinkedListDeque {
private Node head;
private Node tail;
private int size;
public LinkedListDeque() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void addFirst(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}
public void addLast(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
public int removeFirst() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is empty");
}
int removedData = head.data;
if (size == 1) {
head = null;
tail = null;
} else {
head = head.next;
head.prev = null;
}
size--;
return removedData;
}
public int removeLast() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is empty");
}
int removedData = tail.data;
if (size == 1) {
head = null;
tail = null;
} else {
tail = tail.prev;
tail.next = null;
}
size--;
return removedData;
}
public int getFirst() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is empty");
}
return head.data;
}
public int getLast() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is empty");
}
return tail.data;
}
}
Data Structure | Enqueue | Dequeue | Peek | Size | Space Complexity |
---|---|---|---|---|---|
Simple Queue | O(1) | O(1) | O(1) | O(1) | O(n) |
Circular Queue | O(1) | O(1) | O(1) | O(1) | O(n) |
Double Ended Queue | O(1) | O(1) | O(1) | O(1) | O(n) |
Priority Queue | O(log n) | O(log n) | O(1) | O(1) | O(n) |
There are various applications of queues discussed as below.
Note: also read about Stack in DSA
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…