public class CustomLinkedListQueue<E> {
private Node<E> front, rear;
private int size;
public CustomLinkedListQueue() {
this.front = this.rear = null;
this.size = 0;
}
}
public void enqueue(E data) {
Node<E> newNode = new Node<>(data);
if (rear == null) { // Empty queue case
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
size++;
}
public E dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
E data = front.data;
front = front.next;
if (front == null) rear = null; // Queue is now empty
size--;
return data;
}
public E peek() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return front.data;
}
public boolean isEmpty() {
return front == null;
}
public int size() {
return size;
}
public void printQueue() {
Node<E> current = front;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
public class CustomLinkedListQueue<E> {
private Node<E> front, rear;
private int size;
public CustomLinkedListQueue() {
this.front = this.rear = null;
this.size = 0;
}
public void enqueue(E data) {
Node<E> newNode = new Node<>(data);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
size++;
}
public E dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
E data = front.data;
front = front.next;
if (front == null) rear = null;
size--;
return data;
}
public E peek() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return front.data;
}
public boolean isEmpty() {
return front == null;
}
public int size() {
return size;
}
public void printQueue() {
Node<E> current = front;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
CustomLinkedListQueue<Integer> queue = new CustomLinkedListQueue<>();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.printQueue(); // Output: 10 -> 20 -> 30 -> null
System.out.println("Front element: " + queue.peek()); // Output: 10
System.out.println("Dequeued: " + queue.dequeue()); // Output: 10
queue.printQueue(); // Output: 20 -> 30 -> null
System.out.println("Queue size: " + queue.size()); // Output: 2
}
}
public class CustomArrayQueue<E> {
private E[] queue;
private int front, rear, size, capacity;
@SuppressWarnings("unchecked")
public CustomArrayQueue(int capacity) {
this.capacity = capacity;
this.queue = (E[]) new Object[capacity];
this.front = this.size = 0;
this.rear = -1;
}
}
public void enqueue(E data) {
if (size == capacity) throw new RuntimeException("Queue is full");
rear = (rear + 1) % capacity; // Circular increment
queue[rear] = data;
size++;
}
public E dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
E data = queue[front];
queue[front] = null; // Help GC
front = (front + 1) % capacity; // Circular increment
size--;
return data;
}
public E peek() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return queue[front];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
public int size() {
return size;
}
public void printQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
System.out.print("Queue: ");
for (int i = 0; i < size; i++) {
System.out.print(queue[(front + i) % capacity] + " ");
}
System.out.println();
}
public class CustomArrayQueue<E> {
private E[] queue;
private int front, rear, size, capacity;
@SuppressWarnings("unchecked")
public CustomArrayQueue(int capacity) {
this.capacity = capacity;
this.queue = (E[]) new Object[capacity];
this.front = this.size = 0;
this.rear = -1;
}
public void enqueue(E data) {
if (size == capacity) throw new RuntimeException("Queue is full");
rear = (rear + 1) % capacity; // Circular increment
queue[rear] = data;
size++;
}
public E dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
E data = queue[front];
queue[front] = null; // Help GC
front = (front + 1) % capacity; // Circular increment
size--;
return data;
}
public E peek() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return queue[front];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
public int size() {
return size;
}
public void printQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
System.out.print("Queue: ");
for (int i = 0; i < size; i++) {
System.out.print(queue[(front + i) % capacity] + " ");
}
System.out.println();
}
public static void main(String[] args) {
CustomArrayQueue<Integer> queue = new CustomArrayQueue<>(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.printQueue(); // Output: Queue: 10 20 30
System.out.println("Front element: " + queue.peek()); // Output: 10
System.out.println("Dequeued: " + queue.dequeue()); // Output: 10
queue.printQueue(); // Output: Queue: 20 30
System.out.println("Queue size: " + queue.size()); // Output: 2
}
}