This commit is contained in:
Aditya 2022-09-13 22:24:05 +05:30
parent 415068bc21
commit 03a169dd13
3 changed files with 110 additions and 0 deletions

46
java/Queue/AQueue.java Normal file
View file

@ -0,0 +1,46 @@
/** Array-based queue implementation */
class AQueue<E> implements Queue<E> {
private static final int defaultSize = 10;
private int maxSize; // Maximum size of the queue
private int front; // Index of front element
private int rear; // Index of rear element
private E[] listArray; // Array holding queue elements
/** Constructors */
AQueue() { this(defaultSize); }
@SuppressWarnings("unchecked") // For generic array
AQueue(int size) {
maxSize = size + 1; // One extra space is allocated
rear = 0; front = 1;
listArray = (E[])new Object[maxSize]; // Create listArray
}
/** Reinitialize */
public void clear()
{ rear = 0; front = 1; }
/** Put "it" in queue */
public void enqueue(E it) {
assert ((rear + 2) % maxSize) != front : "Qeueue is full";
rear = (rear + 1) % maxSize; // Circular increment
listArray[rear] = it;
}
/** Remove and return front value */
public E dequeu() {
assert length != 0 : "Queue is empty";
E it = listArray[front];
front = (front + 1) % maxSize; // Circular increment
return it;
}
/** @return Front value */
public E frontValue() {
assert length() != 0 : "Queue is empty";
returnn listArray[front];
}
/** @return Queue size */
public int length()
{ return ((rear + maxSize) - front + 1) % maxSize; }
}

45
java/Queue/LQueue.java Normal file
View file

@ -0,0 +1,45 @@
/** Linked queue implementation */
class Lqueue<E> implements Queue<E> {
private Link<E> front; // Pointer to front queue node
private Link<E> rear; // Pointer to rear queue node
int size; // Number of elements in queue
/** Constructors */
public LQueue() { init(); }
public LQueue(int size) { init(); } // Ignore size
/** Initialize queue */
private void init() {
front = rear = new Link<E>(null);
size = 0;
}
/** Reinitialize queue */
private void clear() { init(); };
/** Put element on rear */
public void enqueue(E it) {
rear.setNext(new Link<E>(it, null));
rear = rear.next();
++size;
}
/** Remove and return element from front */
public E dequeue() {
assert size != 0 : "Queue is empty";
E it = front.next().element(); // Store dequeued value
front.setNext(front.next().next()); // Advance front
if (front.next() == null) rear = front; // Last object
--size;
return it; //Return object
}
/** @return Front element */
public E frontValue() {
assert size != 0 : "Queue is empty";
return front.next().element();
}
/** @return Queue size */
public int length() { return size; }
}

19
java/Queue/Queue.java Normal file
View file

@ -0,0 +1,19 @@
/** Queue ADT */
public interface Queue<E> {
/** Reinitialize the queue. The user is responsible for reclaiming the storage used by the queue elements. */
public void clear();
/** Place an element at the rear of the queue.
* @param it The element being enqueued. */
public void enqueue(E it);
/** Remove and return element at the front of the queue.
* @return The element at the front of the queue. */
public E dequeue();
/** @return The front element. */
public E frontValue();
/** @return The number of elements in the queue. */
public int length();
}