parent
4a8357651d
commit
9b38ecdfa6
@ -5,17 +5,17 @@ import java.util.ArrayList;
|
|||||||
/**
|
/**
|
||||||
* This class implements a GenericArrayListQueue.
|
* This class implements a GenericArrayListQueue.
|
||||||
*
|
*
|
||||||
* <p>A GenericArrayListQueue data structure functions the same as any specific-typed queue. The
|
* A GenericArrayListQueue data structure functions the same as any specific-typed queue. The
|
||||||
* GenericArrayListQueue holds elemets of types to-be-specified at runtime. The elements that are
|
* 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
|
* added first are the first to be removed (FIFO). New elements are added to the back/rear of the
|
||||||
* queue.
|
* queue.
|
||||||
*/
|
*/
|
||||||
public class GenericArrayListQueue<T> {
|
public class GenericArrayListQueue<T> {
|
||||||
/** The generic ArrayList for the queue T is the generic element */
|
/** The generic ArrayList for the queue T is the generic element */
|
||||||
ArrayList<T> _queue = new ArrayList<T>();
|
ArrayList<T> _queue = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the queue has elements (not empty)
|
* Checks if the queue has elements (not empty).
|
||||||
*
|
*
|
||||||
* @return True if the queue has elements. False otherwise.
|
* @return True if the queue has elements. False otherwise.
|
||||||
*/
|
*/
|
||||||
@ -24,7 +24,7 @@ public class GenericArrayListQueue<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks what's at the front of the queue
|
* Checks what's at the front of the queue.
|
||||||
*
|
*
|
||||||
* @return If queue is not empty, element at the front of the queue. Otherwise, null
|
* @return If queue is not empty, element at the front of the queue. Otherwise, null
|
||||||
*/
|
*/
|
||||||
@ -51,7 +51,7 @@ public class GenericArrayListQueue<T> {
|
|||||||
*
|
*
|
||||||
* @return If queue is not empty, element retrieved. Otherwise, null
|
* @return If queue is not empty, element retrieved. Otherwise, null
|
||||||
*/
|
*/
|
||||||
public T poll() {
|
public T pull() {
|
||||||
T result = null;
|
T result = null;
|
||||||
if (this.hasElements()) {
|
if (this.hasElements()) {
|
||||||
result = _queue.remove(0);
|
result = _queue.remove(0);
|
||||||
@ -65,19 +65,19 @@ public class GenericArrayListQueue<T> {
|
|||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<Integer>();
|
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
|
||||||
System.out.println("Running...");
|
System.out.println("Running...");
|
||||||
assert queue.peek() == null;
|
assert queue.peek() == null;
|
||||||
assert queue.poll() == null;
|
assert queue.pull() == null;
|
||||||
assert queue.add(1) == true;
|
assert queue.add(1);
|
||||||
assert queue.peek() == 1;
|
assert queue.peek() == 1;
|
||||||
assert queue.add(2) == true;
|
assert queue.add(2);
|
||||||
assert queue.peek() == 1;
|
assert queue.peek() == 1;
|
||||||
assert queue.poll() == 1;
|
assert queue.pull() == 1;
|
||||||
assert queue.peek() == 2;
|
assert queue.peek() == 2;
|
||||||
assert queue.poll() == 2;
|
assert queue.pull() == 2;
|
||||||
assert queue.peek() == null;
|
assert queue.peek() == null;
|
||||||
assert queue.poll() == null;
|
assert queue.pull() == null;
|
||||||
System.out.println("Finished.");
|
System.out.println("Finished.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class LinkedQueue {
|
|||||||
/**
|
/**
|
||||||
* Check if queue is empty
|
* Check if queue is empty
|
||||||
*
|
*
|
||||||
* @return <tt>true</tt> if queue is empty, otherwise <tt>false</tt>
|
* @return true if queue is empty, otherwise false
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return size == 0;
|
return size == 0;
|
||||||
@ -48,7 +48,7 @@ public class LinkedQueue {
|
|||||||
* Add element to rear of queue
|
* Add element to rear of queue
|
||||||
*
|
*
|
||||||
* @param data insert value
|
* @param data insert value
|
||||||
* @return <tt>true</tt> if add successfully
|
* @return true if add successfully
|
||||||
*/
|
*/
|
||||||
public boolean enqueue(int data) {
|
public boolean enqueue(int data) {
|
||||||
Node newNode = new Node(data);
|
Node newNode = new Node(data);
|
||||||
|
@ -3,7 +3,7 @@ package DataStructures.Queues;
|
|||||||
/**
|
/**
|
||||||
* This implements Queues by using the class Queue.
|
* This implements Queues by using the class Queue.
|
||||||
*
|
*
|
||||||
* <p>A queue data structure functions the same as a real world queue. The elements that are added
|
* 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.
|
* first are the first to be removed. New elements are added to the back/rear of the queue.
|
||||||
*/
|
*/
|
||||||
class Queue {
|
class Queue {
|
||||||
@ -153,7 +153,7 @@ public class Queues {
|
|||||||
myQueue.remove(); // Will make 2 the new front, making 10 no longer part of the queue
|
myQueue.remove(); // Will make 2 the new front, making 10 no longer part of the queue
|
||||||
// [10, 2(front), 5, 3(rear)]
|
// [10, 2(front), 5, 3(rear)]
|
||||||
|
|
||||||
myQueue.insert(7); // Insert 7 at the rear which will be index 0 because of wrap around
|
myQueue.insert(7); // Insert 7 at the rear which will get 0 index because of wrap around
|
||||||
// [7(rear), 2(front), 5, 3]
|
// [7(rear), 2(front), 5, 3]
|
||||||
|
|
||||||
System.out.println(myQueue.peekFront()); // Will print 2
|
System.out.println(myQueue.peekFront()); // Will print 2
|
||||||
|
Loading…
Reference in New Issue
Block a user