/home/caleb/ASDV-Java/Semester 3/Assignments/MP6_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp6_calebfontenot/PriorityQueueASDV.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package edu.slcc.asdv.caleb.mp6_calebfontenot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.function.Consumer;
public class PriorityQueueASDV<E extends Comparable<E>>
implements Queue<E>, Cloneable {
private Node<E> head;
private Node<E> tail;
class Node<E> {
E e;
Node<E> l;
Node<E> r;
}
<E>
e
@Override
public boolean add(E e) {
if (e == null) {
throw new NullPointerException("NULL elements not allowed!");
}
Node<E> newNode = new Node<E>();
newNode.e = e;
if (this.head == null && this.tail == null) {
this.head = this.tail = newNode;
return true;
}
int index = findCorrectPositionToInsertElement(e);
if (index == size()) {
tail.r = newNode;
newNode.l = tail;
tail = newNode;
}
else if (index == 0) {
newNode.r = head;
this.head.l = newNode;
this.head = newNode;
if (size() == 1) {
tail = head;
}
}
else {
Node<E> p = head;
for (int i = 0; i < index - 1; ++i) {
p = p.r;
}
newNode.l = p;
newNode.r = p.r;
p.r = newNode;
p.r.r.l = newNode;
}
return true;
}
@Override
public int size() {
Node<E> p = this.head;
int count = 0;
while (p != null) {
p = p.r;
count++;
}
return count;
}
private int findCorrectPositionToInsertElement(E e) {
Node<E> p = this.head;
int pos = 0;
while (p != null) {
if (e.compareTo(p.e) > 0) {
p = p.r;
++pos;
} else {
break;
}
}
return pos;
}
private int (E e) {
Node<E> p = this.head;
int pos = 0;
while (p != null) {
if (e.hashCode() > p.e.hashCode()) {
p = p.r;
++pos;
} else {
break;
}
}
return pos;
}
e
@Override
public boolean offer(E e) {
return add(e);
}
<p>
@Override
public E remove() {
Node<E> = head.r;
E removedElement = head.e;
head = head.r;
head.l = null;
return removedElement;
}
@Override
public E poll() {
if (size() == 0) {
return null;
}
if (size() > 1) {
head = head.r;
E e = head.l.e;
head.l = null;
return e;
} else
{
E e = head.e;
head = tail = null;
return e;
}
}
@Override
public E element() {
if (head != null) {
return (E) head;
} else {
throw new NoSuchElementException("Element does not exist.");
}
}
@Override
public E peek() {
return (E) head;
}
@Override
public boolean isEmpty() {
return head == null && tail == null ? true : false;
}
o
@Override
public boolean contains(Object o) {
Node<E> pointer = head;
do {
if (pointer.equals(o)) {
return true;
} else {
pointer = pointer.r;
}
} while (pointer != null);
return false;
}
@Override
public Iterator<E> iterator() {
Iterator<E> it = new Iterator<E>() {
Node<E> p = head;
@Override
public boolean hasNext() {
return p == null ? false : true;
}
@Override
public E next() {
if (hasNext() == false) {
throw new NoSuchElementException("the is no next element");
}
E e = p.e;
p = p.r;
return e;
}
@Override
public void forEachRemaining(Consumer<? super E> action) {
while (hasNext()) {
action.accept(p.e);
p = p.r;
}
}
};
return it;
}
@Override
public Object[] toArray() {
Node<E> pointer = head;
Object[] returnArray = new Object[this.size()];
int i = 0;
while (pointer.r != null) {
returnArray[i++] = pointer.e;
pointer = pointer.r;
}
returnArray[i++] = pointer.e;
return returnArray;
}
<p>
<i></i>
<p>
<p>
<pre>
</pre>
<p>
<p>
<T>
a
@Override
public <T> T[] toArray(T[] a) {
a = Arrays.copyOf(a, this.size());
Node<E> pointer = head;
System.out.println(a.getClass());
System.out.println(pointer.getClass());
System.out.println(pointer.e.getClass());
for (int i = 0; i < this.size(); ++i) {
a[i] = (T) pointer.e;
pointer = pointer.r;
}
return a;
}
o
@Override
public boolean remove(Object o) {
if (o == null) {
throw new NullPointerException("null vales not allowed");
}
if (size() == 0) {
return false;
}
Node<E> p = this.head;
int pos = 0;
while (p != this.tail) {
if (p.e.equals(o)) {
if (size() == 1) {
this.head = this.tail = null;
return true;
}
this.removeAt(pos, (E) o);
break;
}
++pos;
p = p.r;
}
if (p == tail && p.e.equals(o)) {
this.removeAt(size() - 1, (E) o);
}
return true;
}
pos
e
private boolean removeAt(int pos, E ) {
if (pos < 0 || pos >= size()) {
throw new IndexOutOfBoundsException(pos + " is out of bounds");
}
if (isEmpty()) {
return false;
}
else if (size() == 1) {
this.head = this.tail = null;
}
else if (pos == 0) {
this.head = this.head.r;
head.l = null;
}
else if (pos == size() - 1) {
this.tail = this.tail.l;
this.tail.r = null;
}
else {
Node<E> p = head;
for (int i = 0; i < pos - 1; ++i) {
p = p.r;
}
p.r = p.r.r;
p.r.l = p;
}
return true;
}
c
@Override
public boolean containsAll(Collection<?> c) {
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to containsAll() contains a null element. Cannot continue.");
}
Object[] compareArray = c.toArray();
Node<E> pointer = null;
int matchCount = 0;
for (Object compare : compareArray) {
pointer = head;
for (int i = 0; i < size() - 1; ++i) {
if (pointer.e.equals(compare)) {
matchCount++;
}
pointer = pointer.r;
}
}
if (matchCount == compareArray.length - 1) {
return true;
}
return false;
}
c
@Override
public boolean addAll(Collection<? extends E> c) {
int sizeBefore = size();
for (E e : c) {
add(e);
}
int sizeAfter = size();
return sizeAfter > sizeBefore;
}
<p>
@Override
public boolean removeAll(Collection<?> c) {
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to removeAll() contains a null element. Cannot continue.");
}
Object[] compareArray = c.toArray();
Node<E> pointer = null;
boolean removeSuccessful = false;
for (Object compare : compareArray) {
pointer = head;
for (int i = 0; i < size() - 1; ++i) {
if (pointer.e.equals(compare)) {
remove(pointer.e);
removeSuccessful = true;
}
pointer = pointer.r;
}
}
return removeSuccessful;
}
@Override
public boolean retainAll(Collection<?> c) {
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to retainAll() contains a null element. Cannot continue.");
}
Node<E> pointer = null;
boolean removeSuccessful = false;
for (int j = 0; j < c.size() - 1; ++j) {
pointer = head;
for (int i = 0; i < size(); ++i) {
if (!c.contains(pointer.e)) {
remove(pointer.e);
removeSuccessful = true;
}
pointer = pointer.r;
}
}
return removeSuccessful;
}
@Override
public void clear() {
Node<E> p = head;
while (p != tail) {
p = p.r;
p.l = null;
}
head = tail = null;
}
@Override
public void forEach(Consumer<? super E> action) {
Node<E> p = head;
while (p != null) {
action.accept(p.e);
p = p.r;
}
}
@Override
public String toString() {
String s = "PriorityQueueASDV {";
Node<E> p = head;
while (p != null) {
s += p.e.toString();
if (p != tail) {
s += ", ";
}
p = p.r;
}
s += "}";
return s;
}
@Override
protected Object clone()
throws CloneNotSupportedException {
PriorityQueueASDV<E> c = (PriorityQueueASDV<E>) super.clone();
return c;
}
public static void main(String[] args) {
System.out.println("\n--> PriorityQueueASDV testing add");
PriorityQueueASDV<String> pq1 = new PriorityQueueASDV();
pq1.add("Paris");
pq1.add("Athens");
pq1.add("London");
pq1.add("Lafayette");
pq1.add("Berlin");
System.out.println(pq1);
System.out.println("\n--> Colllections PriorityQueue testing add");
PriorityQueue<String> pq2 = new PriorityQueue();
pq2.add("Paris");
pq2.add("Athens");
pq2.add("London");
pq2.add("Lafayette");
pq2.add("Berlin");
System.out.println(pq2);
System.out.println("\n--> PriorityQueueASDV testing remove(object o)");
System.out.println("\n\tremove from front Athens");
pq1.remove("Athens");
pq2.remove("Athens");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tremove from end Paris");
pq1.remove("Paris");
pq2.remove("Paris");
System.out.println(pq1);
System.out.println(pq1);
System.out.println("\n\tremove from the middle Lafayette");
pq1.remove("Lafayette");
pq2.remove("Lafayette");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tadd at the end Stocholm");
pq1.add("Stocholm");
pq2.add("Stocholm");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tremove from the middle London");
pq1.remove("London");
pq2.remove("London");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tremove from the front Berlin");
pq1.remove("Berlin");
pq2.remove("Berlin");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tremove from the front/end Stocholm");
pq1.remove("Stocholm");
pq2.remove("Stocholm");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\tremove from empty queue");
pq1.remove("Stocholm");
pq2.remove("Stocholm");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n--> PriorityQueueASDV recreate priority queues from empty");
pq1.add("Paris");
pq1.add("Athens");
pq1.add("London");
pq1.add("Lafayette");
pq1.add("Berlin");
pq1.add("Zurich");
pq2.add("Paris");
pq2.add("Athens");
pq2.add("London");
pq2.add("Lafayette");
pq2.add("Berlin");
pq2.add("Zurich");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\n+++HERE YOU TEST ALL YOUR METHODS FULLY, and the methods of Colleciion PriorityQueue");
System.out.println("\n\t offer New York");
pq1.offer("New York");
pq2.offer("New York");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t offer Miami");
pq1.offer("Miami");
pq2.offer("Miami");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t offer null");
try {
pq1.offer(null);
} catch (Exception e) {
System.err.println(e);
}
try {
pq2.offer(null);
} catch (Exception e) {
System.err.println(e);
}
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t offer ClassCastException with Object");
try {
pq1.offer((String) new Object());
} catch (Exception e) {
System.err.println(e);
}
try {
pq2.offer((String) new Object());
} catch (Exception e) {
System.err.println(e);
}
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t poll suposed to be Athens");
System.out.println(pq1.poll());
System.out.println(pq2.poll());
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t Iterator");
Iterator<String> it1 = pq1.iterator();
Iterator<String> it2 = pq2.iterator();
while (it1.hasNext()) {
System.out.print(it1.next() + " ");
}
System.out.println("");
while (it2.hasNext()) {
System.out.print(it2.next() + " ");
}
System.out.println("");
System.out.println("\n\t Iterator NoSuchElementException ");
try {
System.out.println(it1.next());
} catch (NoSuchElementException e) {
System.err.println(e);
}
try {
System.out.println(it2.next());
} catch (NoSuchElementException e) {
System.err.println(e);
}
System.out.println("\n\t Iterator foreach ");
it1 = pq1.iterator();
it2 = pq2.iterator();
it1.forEachRemaining(new Consumer() {
@Override
public void accept(Object t) {
System.out.print(t + "*** ");
}
});
System.out.println("");
it2.forEachRemaining(new Consumer() {
@Override
public void accept(Object t) {
System.out.print(t + "+++ ");
}
});
System.out.println("");
System.out.println("\n\t addAll Houston Chicago");
List<String> ar1 = Arrays.asList("Houston", "Chicago");
pq1.addAll(ar1);
pq2.addAll(ar1);
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t clear");
pq1.clear();
pq2.clear();
System.out.println(pq1);
System.out.println(pq2);
System.out.println("");
System.out.println("\n--> PriorityQueueASDV recreate priority queues from empty");
pq1.add("Paris");
pq1.add("Athens");
pq1.add("London");
pq1.add("Lafayette");
pq1.add("Berlin");
pq1.add("Zurich");
pq2.add("Paris");
pq2.add("Athens");
pq2.add("London");
pq2.add("Lafayette");
pq2.add("Berlin");
pq2.add("Zurich");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("\n\t forEach");
pq1.forEach(new Consumer() {
@Override
public void accept(Object t) {
System.out.print(t + "*** ");
}
});
System.out.println("");
pq2.forEach(new Consumer() {
@Override
public void accept(Object t) {
System.out.print(t + "+++ ");
}
});
System.out.println("");
System.out.println("\n\t clone");
try {
PriorityQueueASDV<String> pq1Cloned
= (PriorityQueueASDV<String>) pq1.clone();
System.out.println(pq1Cloned);
pq1Cloned.add("Las Vegas");
System.out.println(pq1Cloned);
System.out.println(pq1);
} catch (CloneNotSupportedException e) {
System.err.println(e);
}
pq1.clear();
pq2.clear();
pq1.add("Paris");
pq1.add("Athens");
pq1.add("London");
pq1.add("Lafayette");
pq1.add("Berlin");
pq1.add("Zurich");
pq2.add("Paris");
pq2.add("Athens");
pq2.add("London");
pq2.add("Lafayette");
pq2.add("Berlin");
pq2.add("Zurich");
System.out.println("----------------");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Attempt to remove an element.");
pq1.remove();
pq2.remove();
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Get array of the priority queues.");
Object pqArray1[] = pq1.toArray();
Object pqArray2[] = pq2.toArray();
printArrays(pqArray1);
printArrays(pqArray2);
System.out.println();
System.out.println("----------------");
System.out.println("Test .toArray(T[])");
String[] pqArray3 = pq1.toArray(new String[0]);
printArrays(pqArray3);
System.out.println("----------------");
System.out.println("Test containsAll()");
ArrayList<String> testArray = new ArrayList<>();
testArray.add("Lafayette");
testArray.add("Berlin");
testArray.add("Zurich");
System.out.println("Does pq1 contain Lafayette, Berlin, and Zurich? " + (pq1.containsAll(testArray) ? "yes" : "no"));
System.out.println("Does pq1 contain the contents of pq2? " + (pq1.containsAll(pq2) ? "yes" : "no"));
System.out.println("Does pq2 contain the contents of pq1? " + (pq2.containsAll(pq1) ? "yes" : "no"));
System.out.println("Adding funkytown to testArray...");
testArray.add("Funkytown");
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, and Funkytown? " + (pq1.containsAll(testArray) ? "yes" : "no"));
System.out.println("Test if containsAll() correctly throws a NullPointerException...");
try {
testArray.add(null);
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, Funkytown, and null? " + (pq1.containsAll(testArray) ? "yes" : "no"));
} catch (NullPointerException ex) {
System.out.println(ex);
}
testArray.remove(null);
System.out.println("That worked! Continuing with tests...");
System.out.println("----------------");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Testing removeAll(Collection<?>)...");
System.out.println("Removing the elements in the test array...");
pq1.removeAll(testArray);
pq2.removeAll(testArray);
System.out.println(pq1);
System.out.println(pq2);
System.out.println("----------------");
System.out.println("Testing retainAll()...");
ArrayList<String> testArray2 = new ArrayList<>();
testArray2.add("London");
testArray2.add("Paris");
pq1.retainAll(testArray2);
pq2.retainAll(testArray2);
System.out.println(pq1);
System.out.println(pq2);
}
static void printArrays(Object[] arr) {
for (Object element : arr) {
System.out.print(element + ", ");
}
}
}