mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Update Queues.java
This commit is contained in:
@ -7,7 +7,6 @@ package DataStructures.Queues;
|
|||||||
* The elements that are added first are the first to be removed.
|
* The elements that are added first are the first to be removed.
|
||||||
* New elements are added to the back/rear of the queue.
|
* New elements are added to the back/rear of the queue.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
|
||||||
*/
|
*/
|
||||||
class Queue {
|
class Queue {
|
||||||
/**
|
/**
|
||||||
@ -53,7 +52,8 @@ class Queue {
|
|||||||
public boolean insert(int x) {
|
public boolean insert(int x) {
|
||||||
if (isFull())
|
if (isFull())
|
||||||
return false;
|
return false;
|
||||||
rear = (rear + 1) % maxSize; // If the back of the queue is the end of the array wrap around to the front
|
// If the back of the queue is the end of the array wrap around to the front
|
||||||
|
rear = (rear + 1) % maxSize;
|
||||||
queueArray[rear] = x;
|
queueArray[rear] = x;
|
||||||
nItems++;
|
nItems++;
|
||||||
return true;
|
return true;
|
||||||
@ -64,9 +64,8 @@ class Queue {
|
|||||||
*
|
*
|
||||||
* @return the new front of the queue
|
* @return the new front of the queue
|
||||||
*/
|
*/
|
||||||
public int remove() { // Remove an element from the front of the queue
|
public int remove() {
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
System.out.println("Queue is empty");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int temp = queueArray[front];
|
int temp = queueArray[front];
|
||||||
@ -99,7 +98,7 @@ class Queue {
|
|||||||
* @return true if the queue is empty
|
* @return true if the queue is empty
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return (nItems == 0);
|
return nItems == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,7 +107,7 @@ class Queue {
|
|||||||
* @return true if the queue is full
|
* @return true if the queue is full
|
||||||
*/
|
*/
|
||||||
public boolean isFull() {
|
public boolean isFull() {
|
||||||
return (nItems == maxSize);
|
return nItems == maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user