mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
@ -0,0 +1,118 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
//This program implements the concept of CircularQueue in Java
|
||||
//Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
|
||||
public class CircularQueue {
|
||||
|
||||
public static void main(String[] args) {
|
||||
circularQueue cq = new circularQueue(5);
|
||||
System.out.println(cq.isEmpty());
|
||||
System.out.println(cq.isFull());
|
||||
cq.enQueue(1);
|
||||
cq.enQueue(2);
|
||||
cq.enQueue(3);
|
||||
cq.enQueue(4);
|
||||
cq.enQueue(5);
|
||||
|
||||
System.out.println(cq.deQueue());
|
||||
System.out.println(cq.deQueue());
|
||||
System.out.println(cq.deQueue());
|
||||
System.out.println(cq.deQueue());
|
||||
System.out.println(cq.deQueue());
|
||||
System.out.println(cq.isFull());
|
||||
System.out.println(cq.isEmpty());
|
||||
cq.enQueue(6);
|
||||
cq.enQueue(7);
|
||||
cq.enQueue(8);
|
||||
System.out.println(cq.peek());
|
||||
System.out.println(cq.peek());
|
||||
cq.deleteQueue();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class circularQueue {
|
||||
|
||||
int[] arr;
|
||||
int topOfQueue;
|
||||
int beginningOfQueue;
|
||||
int size;
|
||||
|
||||
public circularQueue(int size) {
|
||||
arr = new int[size];
|
||||
topOfQueue = -1;
|
||||
beginningOfQueue = -1;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
if (beginningOfQueue == -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
if (topOfQueue + 1 == beginningOfQueue) {
|
||||
return true;
|
||||
} else if (topOfQueue == size - 1 && beginningOfQueue == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void enQueue(int value) {
|
||||
if (isFull()) {
|
||||
System.out.println("The Queue is full!");
|
||||
} else if (isEmpty()) {
|
||||
beginningOfQueue = 0;
|
||||
topOfQueue++;
|
||||
arr[topOfQueue] = value;
|
||||
System.out.println(value + " has been successfully inserted!");
|
||||
} else {
|
||||
if (topOfQueue + 1 == size) {
|
||||
topOfQueue = 0;
|
||||
} else {
|
||||
topOfQueue++;
|
||||
}
|
||||
arr[topOfQueue] = value;
|
||||
System.out.println(value + " has been successfully inserted!");
|
||||
}
|
||||
}
|
||||
|
||||
public int deQueue() {
|
||||
if (isEmpty()) {
|
||||
System.out.println("The Queue is Empty!");
|
||||
return -1;
|
||||
} else {
|
||||
int res = arr[beginningOfQueue];
|
||||
arr[beginningOfQueue] = Integer.MIN_VALUE;
|
||||
if (beginningOfQueue == topOfQueue) {
|
||||
beginningOfQueue = topOfQueue = -1;
|
||||
} else if (beginningOfQueue + 1 == size) {
|
||||
beginningOfQueue = 0;
|
||||
} else {
|
||||
beginningOfQueue++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int peek() {
|
||||
if (isEmpty()) {
|
||||
System.out.println("The Queue is Empty!");
|
||||
return -1;
|
||||
} else {
|
||||
return arr[beginningOfQueue];
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteQueue() {
|
||||
arr = null;
|
||||
System.out.println("The Queue is deleted!");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,273 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
/**
|
||||
* A [deque](https://en.wikipedia.org/wiki/Double-ended_queue) is short for a
|
||||
* double ended queue pronounced "deck" and sometimes referred to as a head-tail
|
||||
* linked list. A deque is a data structure based on a doubly linked list, but
|
||||
* only supports adding and removal of nodes from the beginning and the end of
|
||||
* the list.
|
||||
*
|
||||
* @author [Ian Cowan](https://github.com/iccowan)
|
||||
*/
|
||||
public class Deques<T> {
|
||||
|
||||
/**
|
||||
* Node for the deque
|
||||
*/
|
||||
class DequeNode<S> {
|
||||
|
||||
/**
|
||||
* Value of the node
|
||||
*/
|
||||
S val;
|
||||
|
||||
/**
|
||||
* Next node in the deque from this node
|
||||
*/
|
||||
DequeNode<S> next = null;
|
||||
|
||||
/**
|
||||
* Previous node in the deque from this node
|
||||
*/
|
||||
DequeNode<S> prev = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
DequeNode(S val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Head of the deque
|
||||
*/
|
||||
DequeNode<T> head = null;
|
||||
|
||||
/**
|
||||
* Tail of the deque
|
||||
*/
|
||||
DequeNode<T> tail = null;
|
||||
|
||||
/**
|
||||
* Size of the deque
|
||||
*/
|
||||
int size = 0;
|
||||
|
||||
/**
|
||||
* Adds the specified value to the head of the deque
|
||||
*
|
||||
* @param val Value to add to the deque
|
||||
*/
|
||||
public void addFirst(T val) {
|
||||
// Create a new node with the given value
|
||||
DequeNode<T> newNode = new DequeNode<T>(val);
|
||||
|
||||
// Add the node
|
||||
if (head == null) {
|
||||
// If the deque is empty, add the node as the head and tail
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
// If the deque is not empty, insert the node as the new head
|
||||
newNode.next = head;
|
||||
head.prev = newNode;
|
||||
head = newNode;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified value to the tail of the deque
|
||||
*
|
||||
* @param val Value to add to the deque
|
||||
*/
|
||||
public void addLast(T val) {
|
||||
// Create a new node with the given value
|
||||
DequeNode<T> newNode = new DequeNode<T>(val);
|
||||
|
||||
// Add the node
|
||||
if (tail == null) {
|
||||
// If the deque is empty, add the node as the head and tail
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
// If the deque is not empty, insert the node as the new tail
|
||||
newNode.prev = tail;
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns the first (head) value in the deque
|
||||
*
|
||||
* @return the value of the head of the deque
|
||||
*/
|
||||
public T pollFirst() {
|
||||
// If the head is null, return null
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// First, let's get the value of the old head
|
||||
T oldHeadVal = head.val;
|
||||
|
||||
// Now, let's remove the head
|
||||
if (head == tail) {
|
||||
// If there is only one node, remove it
|
||||
head = null;
|
||||
tail = null;
|
||||
} else {
|
||||
// If there is more than one node, fix the references
|
||||
head.next.prev = null;
|
||||
DequeNode<T> oldHead = head;
|
||||
head = head.next;
|
||||
|
||||
// Can be considered unnecessary...
|
||||
// Unlinking the old head to make sure there are no random
|
||||
// references possibly affecting garbage collection
|
||||
oldHead.next = null;
|
||||
}
|
||||
|
||||
size--;
|
||||
return oldHeadVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns the last (tail) value in the deque
|
||||
*
|
||||
* @return the value of the tail of the deque
|
||||
*/
|
||||
public T pollLast() {
|
||||
// If the tail is null, return null
|
||||
if (tail == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Let's get the value of the old tail
|
||||
T oldTailVal = tail.val;
|
||||
|
||||
// Now, remove the tail
|
||||
if (head == tail) {
|
||||
// If there is only one node, remove it
|
||||
head = null;
|
||||
tail = null;
|
||||
} else {
|
||||
// If there is more than one node, fix the references
|
||||
tail.prev.next = null;
|
||||
DequeNode<T> oldTail = tail;
|
||||
tail = tail.prev;
|
||||
|
||||
// Similarly to above, can be considered unnecessary
|
||||
// See `pollFirst()` for explanation
|
||||
oldTail.prev = null;
|
||||
}
|
||||
|
||||
size--;
|
||||
return oldTailVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first (head) value of the deque WITHOUT removing
|
||||
*
|
||||
* @return the value of the head of the deque
|
||||
*/
|
||||
public T peekFirst() {
|
||||
return head.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last (tail) value of the deque WITHOUT removing
|
||||
*
|
||||
* @return the value of the tail of the deque
|
||||
*/
|
||||
public T peekLast() {
|
||||
return tail.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the deque
|
||||
*
|
||||
* @return the size of the deque
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the deque is empty
|
||||
*
|
||||
* @return whether or not the deque is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return head == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stringified deque in a pretty form:
|
||||
*
|
||||
* <p>
|
||||
* Head -> 1 <-> 2 <-> 3 <- Tail
|
||||
*
|
||||
* @return the stringified deque
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
String dequeString = "Head -> ";
|
||||
DequeNode<T> currNode = head;
|
||||
while (currNode != null) {
|
||||
dequeString += currNode.val;
|
||||
|
||||
if (currNode.next != null) {
|
||||
dequeString += " <-> ";
|
||||
}
|
||||
|
||||
currNode = currNode.next;
|
||||
}
|
||||
|
||||
dequeString += " <- Tail";
|
||||
|
||||
return dequeString;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Deques<Integer> myDeque = new Deques<Integer>();
|
||||
for (int i = 0; i < 42; i++) {
|
||||
if (i / 42.0 < 0.5) {
|
||||
myDeque.addFirst(i);
|
||||
} else {
|
||||
myDeque.addLast(i);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(myDeque);
|
||||
System.out.println("Size: " + myDeque.size());
|
||||
System.out.println();
|
||||
|
||||
myDeque.pollFirst();
|
||||
myDeque.pollFirst();
|
||||
myDeque.pollLast();
|
||||
System.out.println(myDeque);
|
||||
System.out.println("Size: " + myDeque.size());
|
||||
System.out.println();
|
||||
|
||||
int dequeSize = myDeque.size();
|
||||
for (int i = 0; i < dequeSize; i++) {
|
||||
int removing = -1;
|
||||
if (i / 39.0 < 0.5) {
|
||||
removing = myDeque.pollFirst();
|
||||
} else {
|
||||
removing = myDeque.pollLast();
|
||||
}
|
||||
|
||||
System.out.println("Removing: " + removing);
|
||||
}
|
||||
|
||||
System.out.println(myDeque);
|
||||
System.out.println(myDeque.size());
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class implements a GenericArrayListQueue.
|
||||
*
|
||||
* A GenericArrayListQueue data structure functions the same as any
|
||||
* specific-typed queue. The GenericArrayListQueue holds elements of types
|
||||
* to-be-specified at runtime. The elements that are added first are the first
|
||||
* to be removed (FIFO). New elements are added to the back/rear of the queue.
|
||||
*/
|
||||
public class GenericArrayListQueue<T> {
|
||||
|
||||
/**
|
||||
* The generic ArrayList for the queue T is the generic element
|
||||
*/
|
||||
ArrayList<T> _queue = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Checks if the queue has elements (not empty).
|
||||
*
|
||||
* @return True if the queue has elements. False otherwise.
|
||||
*/
|
||||
private boolean hasElements() {
|
||||
return !_queue.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks what's at the front of the queue.
|
||||
*
|
||||
* @return If queue is not empty, element at the front of the queue.
|
||||
* Otherwise, null
|
||||
*/
|
||||
public T peek() {
|
||||
T result = null;
|
||||
if (this.hasElements()) {
|
||||
result = _queue.get(0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts an element of type T to the queue.
|
||||
*
|
||||
* @param element of type T to be added
|
||||
* @return True if the element was added successfully
|
||||
*/
|
||||
public boolean add(T element) {
|
||||
return _queue.add(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve what's at the front of the queue
|
||||
*
|
||||
* @return If queue is not empty, element retrieved. Otherwise, null
|
||||
*/
|
||||
public T pull() {
|
||||
T result = null;
|
||||
if (this.hasElements()) {
|
||||
result = _queue.remove(0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||
System.out.println("Running...");
|
||||
assert queue.peek() == null;
|
||||
assert queue.pull() == null;
|
||||
assert queue.add(1);
|
||||
assert queue.peek() == 1;
|
||||
assert queue.add(2);
|
||||
assert queue.peek() == 1;
|
||||
assert queue.pull() == 1;
|
||||
assert queue.peek() == 2;
|
||||
assert queue.pull() == 2;
|
||||
assert queue.peek() == null;
|
||||
assert queue.pull() == null;
|
||||
System.out.println("Finished.");
|
||||
}
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class LinkedQueue {
|
||||
|
||||
class Node {
|
||||
|
||||
int data;
|
||||
Node next;
|
||||
|
||||
public Node() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public Node(int data) {
|
||||
this(data, null);
|
||||
}
|
||||
|
||||
public Node(int data, Node next) {
|
||||
this.data = data;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Front of Queue
|
||||
*/
|
||||
private Node front;
|
||||
|
||||
/**
|
||||
* Rear of Queue
|
||||
*/
|
||||
private Node rear;
|
||||
|
||||
/**
|
||||
* Size of Queue
|
||||
*/
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Init LinkedQueue
|
||||
*/
|
||||
public LinkedQueue() {
|
||||
front = rear = new Node();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if queue is empty
|
||||
*
|
||||
* @return true if queue is empty, otherwise false
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add element to rear of queue
|
||||
*
|
||||
* @param data insert value
|
||||
* @return true if add successfully
|
||||
*/
|
||||
public boolean enqueue(int data) {
|
||||
Node newNode = new Node(data);
|
||||
rear.next = newNode;
|
||||
rear = newNode;
|
||||
/* make rear point at last node */
|
||||
size++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove element at the front of queue
|
||||
*
|
||||
* @return element at the front of queue
|
||||
*/
|
||||
public int dequeue() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException("queue is empty");
|
||||
}
|
||||
Node destroy = front.next;
|
||||
int retValue = destroy.data;
|
||||
front.next = front.next.next;
|
||||
destroy = null;
|
||||
/* clear let GC do it's work */
|
||||
size--;
|
||||
|
||||
if (isEmpty()) {
|
||||
front = rear;
|
||||
}
|
||||
|
||||
return retValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek element at the front of queue without removing
|
||||
*
|
||||
* @return element at the front
|
||||
*/
|
||||
public int peekFront() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException("queue is empty");
|
||||
}
|
||||
return front.next.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek element at the rear of queue without removing
|
||||
*
|
||||
* @return element at the front
|
||||
*/
|
||||
public int peekRear() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException("queue is empty");
|
||||
}
|
||||
return rear.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return size of queue
|
||||
*
|
||||
* @return size of queue
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all nodes in queue
|
||||
*/
|
||||
public void clear() {
|
||||
while (!isEmpty()) {
|
||||
dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (isEmpty()) {
|
||||
return "[]";
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Node cur = front.next;
|
||||
builder.append("[");
|
||||
while (cur != null) {
|
||||
builder.append(cur.data).append(", ");
|
||||
cur = cur.next;
|
||||
}
|
||||
builder.replace(builder.length() - 2, builder.length(), "]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
LinkedQueue queue = new LinkedQueue();
|
||||
assert queue.isEmpty();
|
||||
|
||||
queue.enqueue(1);
|
||||
/* 1 */
|
||||
queue.enqueue(2);
|
||||
/* 1 2 */
|
||||
queue.enqueue(3);
|
||||
/* 1 2 3 */
|
||||
System.out.println(queue);
|
||||
/* [1, 2, 3] */
|
||||
|
||||
assert queue.size() == 3;
|
||||
assert queue.dequeue() == 1;
|
||||
assert queue.peekFront() == 2;
|
||||
assert queue.peekRear() == 3;
|
||||
|
||||
queue.clear();
|
||||
assert queue.isEmpty();
|
||||
|
||||
System.out.println(queue);
|
||||
/* [] */
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
/**
|
||||
* This class implements a PriorityQueue.
|
||||
*
|
||||
* <p>
|
||||
* A priority queue adds elements into positions based on their priority. So the
|
||||
* most important elements are placed at the front/on the top. In this example I
|
||||
* give numbers that are bigger, a higher priority. Queues in theory have no
|
||||
* fixed size but when using an array implementation it does.
|
||||
*/
|
||||
class PriorityQueue {
|
||||
|
||||
/**
|
||||
* The max size of the queue
|
||||
*/
|
||||
private int maxSize;
|
||||
/**
|
||||
* The array for the queue
|
||||
*/
|
||||
private int[] queueArray;
|
||||
/**
|
||||
* How many items are in the queue
|
||||
*/
|
||||
private int nItems;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param size Size of the queue
|
||||
*/
|
||||
public PriorityQueue(int size) {
|
||||
maxSize = size;
|
||||
queueArray = new int[size];
|
||||
nItems = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts an element in it's appropriate place
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void insert(int value) {
|
||||
if (isFull()) {
|
||||
throw new RuntimeException("Queue is full");
|
||||
} else {
|
||||
int j = nItems - 1; // index of last element
|
||||
while (j >= 0 && queueArray[j] > value) {
|
||||
queueArray[j + 1] = queueArray[j]; // Shifts every element up to make room for insertion
|
||||
j--;
|
||||
}
|
||||
queueArray[j + 1] = value; // Once the correct position is found the value is inserted
|
||||
nItems++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the element from the front of the queue
|
||||
*
|
||||
* @return The element removed
|
||||
*/
|
||||
public int remove() {
|
||||
return queueArray[--nItems];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks what's at the front of the queue
|
||||
*
|
||||
* @return element at the front of the queue
|
||||
*/
|
||||
public int peek() {
|
||||
return queueArray[nItems - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the queue is empty
|
||||
*
|
||||
* @return true if the queue is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return (nItems == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the queue is full
|
||||
*
|
||||
* @return true if the queue is full
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return (nItems == maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in the queue
|
||||
*
|
||||
* @return number of elements in the queue
|
||||
*/
|
||||
public int getSize() {
|
||||
return nItems;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class implements the PriorityQueue class above.
|
||||
*
|
||||
* @author Unknown
|
||||
*/
|
||||
public class PriorityQueues {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
PriorityQueue myQueue = new PriorityQueue(4);
|
||||
myQueue.insert(10);
|
||||
myQueue.insert(2);
|
||||
myQueue.insert(5);
|
||||
myQueue.insert(3);
|
||||
// [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
|
||||
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
System.out.print(
|
||||
myQueue.remove() + " "); // will print the queue in reverse order [10, 5, 3, 2]
|
||||
}
|
||||
// As you can see, a Priority Queue can be used as a sorting algotithm
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
/**
|
||||
* This implements Queues by using the class Queue.
|
||||
*
|
||||
* A queue data structure functions the same as a real world queue. The elements
|
||||
* that are added first are the first to be removed. New elements are added to
|
||||
* the back/rear of the queue.
|
||||
*/
|
||||
class Queue {
|
||||
|
||||
/**
|
||||
* Default initial capacity.
|
||||
*/
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
/**
|
||||
* Max size of the queue
|
||||
*/
|
||||
private int maxSize;
|
||||
/**
|
||||
* The array representing the queue
|
||||
*/
|
||||
private int[] queueArray;
|
||||
/**
|
||||
* Front of the queue
|
||||
*/
|
||||
private int front;
|
||||
/**
|
||||
* Rear of the queue
|
||||
*/
|
||||
private int rear;
|
||||
/**
|
||||
* How many items are in the queue
|
||||
*/
|
||||
private int nItems;
|
||||
|
||||
/**
|
||||
* init with DEFAULT_CAPACITY
|
||||
*/
|
||||
public Queue() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param size Size of the new queue
|
||||
*/
|
||||
public Queue(int size) {
|
||||
maxSize = size;
|
||||
queueArray = new int[size];
|
||||
front = 0;
|
||||
rear = -1;
|
||||
nItems = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts an element at the rear of the queue
|
||||
*
|
||||
* @param x element to be added
|
||||
* @return True if the element was added successfully
|
||||
*/
|
||||
public boolean insert(int x) {
|
||||
if (isFull()) {
|
||||
return false;
|
||||
}
|
||||
// If the back of the queue is the end of the array wrap around to the front
|
||||
rear = (rear + 1) % maxSize;
|
||||
queueArray[rear] = x;
|
||||
nItems++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element from the front of the queue
|
||||
*
|
||||
* @return the new front of the queue
|
||||
*/
|
||||
public int remove() {
|
||||
if (isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
int temp = queueArray[front];
|
||||
front = (front + 1) % maxSize;
|
||||
nItems--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks what's at the front of the queue
|
||||
*
|
||||
* @return element at the front of the queue
|
||||
*/
|
||||
public int peekFront() {
|
||||
return queueArray[front];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks what's at the rear of the queue
|
||||
*
|
||||
* @return element at the rear of the queue
|
||||
*/
|
||||
public int peekRear() {
|
||||
return queueArray[rear];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the queue is empty
|
||||
*
|
||||
* @return true if the queue is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return nItems == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the queue is full
|
||||
*
|
||||
* @return true if the queue is full
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return nItems == maxSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in the queue
|
||||
*
|
||||
* @return number of elements in the queue
|
||||
*/
|
||||
public int getSize() {
|
||||
return nItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[");
|
||||
for (int i = front;; i = ++i % maxSize) {
|
||||
sb.append(queueArray[i]).append(", ");
|
||||
if (i == rear) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sb.replace(sb.length() - 2, sb.length(), "]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is the example for the Queue class
|
||||
*
|
||||
* @author Unknown
|
||||
*/
|
||||
public class Queues {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Queue myQueue = new Queue(4);
|
||||
myQueue.insert(10);
|
||||
myQueue.insert(2);
|
||||
myQueue.insert(5);
|
||||
myQueue.insert(3);
|
||||
// [10(front), 2, 5, 3(rear)]
|
||||
|
||||
System.out.println(myQueue.isFull()); // Will print true
|
||||
|
||||
myQueue.remove(); // Will make 2 the new front, making 10 no longer part of the queue
|
||||
// [10, 2(front), 5, 3(rear)]
|
||||
|
||||
myQueue.insert(7); // Insert 7 at the rear which will get 0 index because of wrap around
|
||||
// [7(rear), 2(front), 5, 3]
|
||||
|
||||
System.out.println(myQueue.peekFront()); // Will print 2
|
||||
System.out.println(myQueue.peekRear()); // Will print 7
|
||||
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
# Queue
|
||||
- The Queue interface is present in the `java.util` package.
|
||||
- It is an ordered list of objects that follows the **FIFO** (First-In-First-Out) principle.
|
||||
|
||||
## Characteristics of a Queue
|
||||
- The Queue is used to insert elements at the end of the queue and removes elements from the beginning of the queue.
|
||||
- It supports all methods of Collection interface including insertion, deletion etc.
|
||||
- LinkedList, ArrayBlockingQueue and PriorityQueue are the most commonly used implementations.
|
||||
|
||||
## Declaration
|
||||
|
||||
`Queue<Obj> queue = new PriorityQueue<Obj> ();`
|
||||
|
||||
## Important operations
|
||||
|
||||
| Operations | Description |
|
||||
| ----------- | ----------- |
|
||||
|Enqueue|Adds an item to the queue|
|
||||
|Dequeue|Removes an item from the queue|
|
||||
|Front|Gets the front item from the queue|
|
||||
|Rear|Gets the last item from the queue|
|
||||
|
||||
|
Reference in New Issue
Block a user