user defined queue using arraylist nd lnked list
Fri Jun 07 2024 12:38:52 GMT+0000 (Coordinated Universal Time)
Saved by @dbms
class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
class GenericQueueArray<T> {
private T[] queueArray;
private int front, rear, size, capacity;
@SuppressWarnings("unchecked")
public GenericQueueArray(int capacity) {
this.capacity = capacity;
this.queueArray = (T[]) new Object[capacity];
this.front = 0;
this.rear = 0; // Initialize rear to be the same as front
this.size = 0;
}
public void enqueue(T item) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
queueArray[rear] = item;
rear = (rear + 1) % capacity;
size++;
}
public T dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return null;
}
T item = queueArray[front];
front = (front + 1) % capacity;
size--;
return item;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
}
class GenericQueueLinkedList<T> {
private Node<T> front, rear;
public GenericQueueLinkedList() {
this.front = this.rear = null;
}
public void enqueue(T item) {
Node<T> newNode = new Node<>(item);
if (isEmpty()) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public T dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return null;
}
T item = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return item;
}
public boolean isEmpty() {
return front == null;
}
}
public class GenericQueueExample {
public static void main(String[] args) {
// Static input for demonstration
GenericQueueArray<Integer> intQueueArray = new GenericQueueArray<>(5);
intQueueArray.enqueue(1);
intQueueArray.enqueue(2);
intQueueArray.enqueue(3);
System.out.println("Array Queue Dequeue: " + intQueueArray.dequeue());
System.out.println("Array Queue Dequeue: " + intQueueArray.dequeue());
GenericQueueLinkedList<Integer> intQueueLinkedList = new GenericQueueLinkedList<>();
intQueueLinkedList.enqueue(1);
intQueueLinkedList.enqueue(2);
intQueueLinkedList.enqueue(3);
System.out.println("Linked List Queue Dequeue: " + intQueueLinkedList.dequeue());
System.out.println("Linked List Queue Dequeue: " + intQueueLinkedList.dequeue());
GenericQueueArray<Double> doubleQueueArray = new GenericQueueArray<>(5);
doubleQueueArray.enqueue(1.1);
doubleQueueArray.enqueue(2.2);
doubleQueueArray.enqueue(3.3);
System.out.println("Array Queue Dequeue: " + doubleQueueArray.dequeue());
System.out.println("Array Queue Dequeue: " + doubleQueueArray.dequeue());
GenericQueueLinkedList<Double> doubleQueueLinkedList = new GenericQueueLinkedList<>();
doubleQueueLinkedList.enqueue(1.1);
doubleQueueLinkedList.enqueue(2.2);
doubleQueueLinkedList.enqueue(3.3);
System.out.println("Linked List Queue Dequeue: " + doubleQueueLinkedList.dequeue());
System.out.println("Linked List Queue Dequeue: " + doubleQueueLinkedList.dequeue());
GenericQueueArray<String> stringQueueArray = new GenericQueueArray<>(5);
stringQueueArray.enqueue("one");
stringQueueArray.enqueue("two");
stringQueueArray.enqueue("three");
System.out.println("Array Queue Dequeue: " + stringQueueArray.dequeue());
System.out.println("Array Queue Dequeue: " + stringQueueArray.dequeue());
GenericQueueLinkedList<String> stringQueueLinkedList = new GenericQueueLinkedList<>();
stringQueueLinkedList.enqueue("one");
stringQueueLinkedList.enqueue("two");
stringQueueLinkedList.enqueue("three");
System.out.println("Linked List Queue Dequeue: " + stringQueueLinkedList.dequeue());
System.out.println("Linked List Queue Dequeue: " + stringQueueLinkedList.dequeue());
}
}



Comments