mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
@ -1,7 +1,7 @@
|
||||
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)
|
||||
// This program implements the concept of CircularQueue in Java
|
||||
// Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
|
||||
public class CircularQueue {
|
||||
|
||||
int[] arr;
|
||||
@ -23,7 +23,8 @@ public class CircularQueue {
|
||||
public boolean isFull() {
|
||||
if (topOfQueue + 1 == beginningOfQueue) {
|
||||
return true;
|
||||
} else return topOfQueue == size - 1 && beginningOfQueue == 0;
|
||||
} else
|
||||
return topOfQueue == size - 1 && beginningOfQueue == 0;
|
||||
}
|
||||
|
||||
public void enQueue(int value) {
|
||||
|
@ -124,11 +124,9 @@ public class LinkedQueue<T> implements Iterable<T> {
|
||||
|
||||
public T peek(int pos) {
|
||||
if (pos > size)
|
||||
throw new IndexOutOfBoundsException(
|
||||
"Position %s out of range!".formatted(pos));
|
||||
throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
|
||||
Node<T> node = front;
|
||||
while (pos-- > 0)
|
||||
node = node.next;
|
||||
while (pos-- > 0) node = node.next;
|
||||
return node.data;
|
||||
}
|
||||
|
||||
@ -140,7 +138,6 @@ public class LinkedQueue<T> implements Iterable<T> {
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<>() {
|
||||
|
||||
Node<T> node = front;
|
||||
|
||||
@Override
|
||||
@ -168,16 +165,14 @@ public class LinkedQueue<T> implements Iterable<T> {
|
||||
* Clear all nodes in queue
|
||||
*/
|
||||
public void clear() {
|
||||
while (size > 0)
|
||||
dequeue();
|
||||
while (size > 0) dequeue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringJoiner join = new StringJoiner(", "); // separator of ', '
|
||||
Node<T> travel = front;
|
||||
while ((travel = travel.next) != null)
|
||||
join.add(String.valueOf(travel.data));
|
||||
while ((travel = travel.next) != null) join.add(String.valueOf(travel.data));
|
||||
return '[' + join.toString() + ']';
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,5 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This class implements a PriorityQueue.
|
||||
*
|
||||
@ -126,7 +123,8 @@ class PriorityQueue {
|
||||
if (isEmpty()) {
|
||||
throw new RuntimeException("Queue is Empty");
|
||||
} else {
|
||||
int max = queueArray[1]; // By defintion of our max-heap, value at queueArray[1] pos is the greatest
|
||||
int max = queueArray[1]; // By defintion of our max-heap, value at queueArray[1] pos is
|
||||
// the greatest
|
||||
|
||||
// Swap max and last element
|
||||
int temp = queueArray[1];
|
||||
@ -175,4 +173,3 @@ class PriorityQueue {
|
||||
return nItems;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user