update
This commit is contained in:
parent
9cc7e1d0df
commit
9ef4210608
@ -0,0 +1,238 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>CircularList.java</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST1 {color: #9876aa}
|
||||
.ST2 {color: #ffc66d}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.whitespace {color: #505050}
|
||||
.comment {color: #808080}
|
||||
.ST4 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST5 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST3 {color: #8a653b}
|
||||
.literal {color: #cc7832}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP6_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp6_calebfontenot/CircularList.java</td></tr></table>
|
||||
<pre>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.mp6_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">Markou</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> CircularList<T <span class="literal">extends</span> Comparable<T>> {
|
||||
|
||||
Node<T> <span class="ST1">head</span>;
|
||||
|
||||
<span class="literal">public</span> <span class="literal">class</span> Node< T <span class="literal">extends</span> Comparable<T>>
|
||||
<span class="literal">implements</span> Comparable<Node<T>> {
|
||||
|
||||
<span class="literal">private</span> T <span class="ST1">t</span>;
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">set</span>(T t)
|
||||
{
|
||||
<span class="literal">this</span>.<span class="ST1">t</span> = t;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> T <span class="ST2">get</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="ST1">t</span>;
|
||||
}
|
||||
Node<T> <span class="ST1">next</span>;
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compareTo</span>(Node<T> o)
|
||||
{
|
||||
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST1">t</span>.compareTo(o.get());
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@param</span> <span class="ST3">t</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">add</span>(Object t)
|
||||
{
|
||||
Node<T> temp = <span class="literal">new</span> Node();
|
||||
temp.set((T) t);
|
||||
|
||||
<span class="literal">if</span> (<span class="ST1">head</span> == <span class="literal">null</span>) {
|
||||
<span class="ST1">head</span> = temp;
|
||||
<span class="ST1">head</span>.<span class="ST1">next</span> = <span class="ST1">head</span>;
|
||||
} <span class="literal">else</span> {
|
||||
Node<T> temp2 = <span class="ST1">head</span>;
|
||||
<span class="literal">do</span> {
|
||||
temp2 = temp2.<span class="ST1">next</span>;
|
||||
} <span class="literal">while</span> (temp2.<span class="ST1">next</span> != <span class="ST1">head</span>);
|
||||
temp.<span class="ST1">next</span> = <span class="ST1">head</span>;
|
||||
temp2.<span class="ST1">next</span> = temp;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@return</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">getSize</span>()
|
||||
{
|
||||
<span class="literal">if</span> (<span class="ST1">head</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
Node<T> temp = <span class="ST1">head</span>;
|
||||
<span class="literal">int</span> count = <span class="number">0</span>;
|
||||
<span class="literal">do</span> {
|
||||
temp = temp.<span class="ST1">next</span>;
|
||||
count++;
|
||||
} <span class="literal">while</span> (temp != <span class="ST1">head</span>);
|
||||
|
||||
<span class="literal">return</span> count;
|
||||
}
|
||||
<span class="comment">////////////////////////////////////////////////////////</span>
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">addAt</span>(Object t, <span class="literal">int</span> pos)
|
||||
{
|
||||
<span class="literal">if</span> (pos < <span class="number">0</span> || pos > getSize()) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
Node<T> temp = <span class="literal">new</span> Node();
|
||||
temp.set((T) t);
|
||||
|
||||
<span class="literal">if</span> (<span class="ST1">head</span> == <span class="literal">null</span>) {
|
||||
add(t);
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (pos == <span class="number">0</span>) {
|
||||
<span class="literal">int</span> oldSize = getSize();
|
||||
Node<T> <span class="comment">currentHead</span> = <span class="ST1">head</span>;
|
||||
temp.<span class="ST1">next</span> = <span class="ST1">head</span>;
|
||||
<span class="ST1">head</span> = temp;
|
||||
<span class="comment">// Set the node at the end of the list to point back to the new head.</span>
|
||||
Node<T> pointer = <span class="ST1">head</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < oldSize; ++i) {
|
||||
pointer = pointer.<span class="ST1">next</span>;
|
||||
}
|
||||
pointer.<span class="ST1">next</span> = temp;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="comment">// Iterate to the desired position</span>
|
||||
Node<T> pointer = <span class="ST1">head</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < pos -<span class="number">1</span>; ++i) {
|
||||
pointer = pointer.<span class="ST1">next</span>;
|
||||
}
|
||||
temp.<span class="ST1">next</span> = pointer.<span class="ST1">next</span>;
|
||||
pointer.<span class="ST1">next</span> = temp;
|
||||
}
|
||||
}
|
||||
<span class="comment">//////////////////////////////////////////////////////////////</span>
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">removes</span> <span class="comment">the</span> <span class="comment">last</span> <span class="comment">node</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">remove</span>()
|
||||
{
|
||||
Node<T> pointer = <span class="ST1">head</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="literal">this</span>.getSize() - <span class="number">2</span>; ++i) {
|
||||
pointer = pointer.<span class="ST1">next</span>;
|
||||
}
|
||||
pointer.<span class="ST1">next</span> = <span class="ST1">head</span>;
|
||||
}
|
||||
<span class="comment">//////////////////////////////////////////////////////////////</span>
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">removes</span> <span class="comment">a</span> <span class="comment">specific</span> <span class="comment">object</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">list</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">remove</span>(Object t)
|
||||
{
|
||||
Node<T> pointer = <span class="ST1">head</span>;
|
||||
<span class="literal">boolean</span> isObjectRemoved = <span class="literal">false</span>;
|
||||
<span class="literal">do</span> {
|
||||
<span class="literal">boolean</span> eval = pointer.<span class="ST1">next</span>.<span class="ST1">t</span>.equals(t);
|
||||
<span class="literal">if</span> (eval) {
|
||||
isObjectRemoved = <span class="literal">true</span>;
|
||||
pointer.<span class="ST1">next</span> = pointer.<span class="ST1">next</span>.<span class="ST1">next</span>;
|
||||
<span class="literal">break</span>;
|
||||
} <span class="literal">else</span> {
|
||||
pointer = pointer.<span class="ST1">next</span>;
|
||||
}
|
||||
} <span class="literal">while</span> (pointer != <span class="ST1">head</span>);
|
||||
<span class="literal">return</span> isObjectRemoved;
|
||||
}
|
||||
<span class="comment">//////////////////////////////////////////////////////////////</span>
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">removeAt</span>(<span class="literal">int</span> pos)
|
||||
{
|
||||
Node<T> pointer = <span class="ST1">head</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < pos; ++i) {
|
||||
<span class="literal">boolean</span> eval = i == pos - <span class="number">1</span>;
|
||||
<span class="literal">if</span> (eval) {
|
||||
pointer.<span class="ST1">next</span> = pointer.<span class="ST1">next</span>.<span class="ST1">next</span>;
|
||||
<span class="literal">break</span>;
|
||||
} <span class="literal">else</span> {
|
||||
pointer = pointer.<span class="ST1">next</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">print</span>()
|
||||
{
|
||||
|
||||
<span class="literal">if</span> (<span class="ST1">head</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
Node<T> temp = <span class="ST1">head</span>;
|
||||
<span class="literal">int</span> elementNum = <span class="number">0</span>;
|
||||
<span class="literal">do</span> {
|
||||
System.<span class="ST4">out</span>.print(++elementNum + <span class="string">"</span><span class="string">: </span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(temp.get().toString());
|
||||
temp = temp.<span class="ST1">next</span>;
|
||||
} <span class="literal">while</span> (temp != <span class="ST1">head</span>);
|
||||
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String... ar)
|
||||
{
|
||||
CircularList<String> list = <span class="literal">new</span> CircularList();
|
||||
list.add(<span class="string">"</span><span class="string">Hello</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">World</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">one</span><span class="string">"</span>);
|
||||
String removeMe = <span class="string">"</span><span class="string">two</span><span class="string">"</span>;
|
||||
list.add(removeMe);
|
||||
list.add(<span class="string">"</span><span class="string">three</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">four</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">five</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">six</span><span class="string">"</span>);
|
||||
list.add(<span class="string">"</span><span class="string">seven</span><span class="string">"</span>);
|
||||
list.print();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">-------------</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Remove the last object:</span><span class="string">"</span>);
|
||||
list.remove();
|
||||
list.print();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">-------------</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Remove </span><span class="literal">\"</span><span class="string">two</span><span class="literal">\"</span><span class="string">"</span>);
|
||||
list.remove(removeMe);
|
||||
list.print();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">-------------</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Remove the 3rd element:</span><span class="string">"</span>);
|
||||
list.removeAt(<span class="number">2</span>);
|
||||
list.print();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">-------------</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Add an element to the beginning</span><span class="string">"</span>);
|
||||
list.addAt(<span class="string">"</span><span class="string">Earth</span><span class="string">"</span>, <span class="number">0</span>);
|
||||
list.print();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">-------------</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Add an element named </span><span class="literal">\"</span><span class="string">potato</span><span class="literal">\"</span><span class="string"> in the 4th position:</span><span class="string">"</span>);
|
||||
list.addAt(<span class="string">"</span><span class="string">potato</span><span class="string">"</span>, <span class="number">3</span>);
|
||||
list.print();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -85,18 +85,37 @@ public class CircularList<T extends Comparable<T>> {
|
||||
if (head == null) {
|
||||
add(t);
|
||||
} else if (pos == 0) {
|
||||
|
||||
int oldSize = getSize();
|
||||
Node<T> currentHead = head;
|
||||
temp.next = head;
|
||||
head = temp;
|
||||
// Set the node at the end of the list to point back to the new head.
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < oldSize; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
pointer.next = temp;
|
||||
} else {
|
||||
|
||||
// Iterate to the desired position
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < pos -1; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
temp.next = pointer.next;
|
||||
pointer.next = temp;
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* removes the
|
||||
* removes the last node
|
||||
*/
|
||||
public void remove()
|
||||
{
|
||||
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < this.getSize() - 2; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
pointer.next = head;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
/**
|
||||
@ -104,13 +123,34 @@ public class CircularList<T extends Comparable<T>> {
|
||||
*/
|
||||
public boolean remove(Object t)
|
||||
{
|
||||
return true;
|
||||
Node<T> pointer = head;
|
||||
boolean isObjectRemoved = false;
|
||||
do {
|
||||
boolean eval = pointer.next.t.equals(t);
|
||||
if (eval) {
|
||||
isObjectRemoved = true;
|
||||
pointer.next = pointer.next.next;
|
||||
break;
|
||||
} else {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
} while (pointer != head);
|
||||
return isObjectRemoved;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
public void removeAt(int pos)
|
||||
{
|
||||
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < pos; ++i) {
|
||||
boolean eval = i == pos - 1;
|
||||
if (eval) {
|
||||
pointer.next = pointer.next.next;
|
||||
break;
|
||||
} else {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void print()
|
||||
@ -120,7 +160,9 @@ public class CircularList<T extends Comparable<T>> {
|
||||
return;
|
||||
}
|
||||
Node<T> temp = head;
|
||||
int elementNum = 0;
|
||||
do {
|
||||
System.out.print(++elementNum + ": ");
|
||||
System.out.println(temp.get().toString());
|
||||
temp = temp.next;
|
||||
} while (temp != head);
|
||||
@ -129,11 +171,38 @@ public class CircularList<T extends Comparable<T>> {
|
||||
|
||||
public static void main(String... ar)
|
||||
{
|
||||
CircularList<Integer> list = new CircularList();
|
||||
CircularList<String> list = new CircularList();
|
||||
list.add("Hello");
|
||||
list.add("World");
|
||||
list.add("one");
|
||||
String removeMe = "two";
|
||||
list.add(removeMe);
|
||||
list.add("three");
|
||||
list.add("four");
|
||||
list.add("five");
|
||||
list.add("six");
|
||||
list.add("seven");
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove the last object:");
|
||||
list.remove();
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove \"two\"");
|
||||
list.remove(removeMe);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove the 3rd element:");
|
||||
list.removeAt(2);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Add an element to the beginning");
|
||||
list.addAt("Earth", 0);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Add an element named \"potato\" in the 4th position:");
|
||||
list.addAt("potato", 3);
|
||||
list.print();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,799 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.mp6_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
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;//head
|
||||
private Node<E> tail;//tail
|
||||
|
||||
class Node<E>
|
||||
{
|
||||
|
||||
E e;
|
||||
Node<E> l;
|
||||
Node<E> r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so
|
||||
* immediately without violating capacity restrictions, returning true upon
|
||||
* success and throwing an IllegalStateException if no space is currently
|
||||
* available.
|
||||
*
|
||||
* Specified by: add in interface Collection<E>
|
||||
* Parameters: e - the element to add Returns: true (as specified by
|
||||
* Collection.add(E)) Throws: IllegalStateException - if the element cannot
|
||||
* be added at this time due to capacity restrictions ClassCastException -
|
||||
* if the class of the specified element prevents it from being added to
|
||||
* this queue NullPointerException - if the specified element is null and
|
||||
* this queue does not permit null elements IllegalArgumentException - if
|
||||
* some property of this element prevents it from being added to this queue
|
||||
*
|
||||
* @param e - the element to add
|
||||
* @return true if this collection changed as a result of the call
|
||||
* @throws IllegalStateException - if the element cannot be added at this
|
||||
* time due to capacity restrictions
|
||||
* @throws ClassCastException - if the class of the specified element
|
||||
* @throws NullPointerException - if the specified element is null and this
|
||||
* queue does not permit null elements
|
||||
* @throws IllegalArgumentException - if some property of this element
|
||||
* prevents it from being added to this queue
|
||||
*/
|
||||
@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;
|
||||
|
||||
//1. empty queue
|
||||
if (this.head == null && this.tail == null)
|
||||
{
|
||||
this.head = this.tail = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
int index = findCorrectPositionToInsertElement(e);
|
||||
//int index = findCorrectPositionToInsertElementHashCode(e);
|
||||
|
||||
//2. we add at size ( last node)
|
||||
if (index == size())
|
||||
{
|
||||
tail.r = newNode;//1
|
||||
newNode.l = tail;//2
|
||||
tail = newNode;//3
|
||||
}
|
||||
//3. we add at 0 in the front
|
||||
else if (index == 0)
|
||||
{
|
||||
newNode.r = head;
|
||||
this.head.l = newNode;
|
||||
this.head = newNode;
|
||||
if (size() == 1)
|
||||
{
|
||||
tail = head;
|
||||
}
|
||||
}
|
||||
//4. we add in the middle
|
||||
else
|
||||
{
|
||||
Node<E> p = head;
|
||||
|
||||
for (int i = 0; i < index - 1; ++i)
|
||||
{
|
||||
p = p.r;
|
||||
}
|
||||
|
||||
//after for loop p point one position before insertion
|
||||
newNode.l = p;//we connect the left of the new node
|
||||
//to the node that is BEFORE
|
||||
//the node to be inserted
|
||||
|
||||
newNode.r = p.r;//we connect the right of the new node
|
||||
// to the node thta is AFTER
|
||||
//the node to be inserted
|
||||
|
||||
p.r = newNode; //we connect the right the node BEFORE the node
|
||||
//to be inserted to the new node
|
||||
|
||||
p.r.r.l = newNode;//we connect the left of the node AFTER the node
|
||||
//to be iserted to the new node
|
||||
}
|
||||
|
||||
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 findCorrectPositionToInsertElementHashCode(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so
|
||||
* immediately without violating capacity restrictions. When using a
|
||||
* capacity-restricted queue, this method is generally preferable to add(E),
|
||||
* which can fail to insert an element only by throwing an exception.
|
||||
*
|
||||
* @param e - the element to add
|
||||
* @throws IllegalStateException - if the element cannot be added at this
|
||||
* time due to capacity restrictions
|
||||
* @throws ClassCastException - if the class of the specified element
|
||||
* @throws NullPointerException - if the specified element is null and this
|
||||
* queue does not permit null elements
|
||||
* @throws IllegalArgumentException - if some property of this element
|
||||
* prevents it from being added to this queue
|
||||
* @return true if the element was added
|
||||
*/
|
||||
@Override
|
||||
public boolean offer(E e)
|
||||
{
|
||||
return add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue, or returns null if this
|
||||
* queue is empty.
|
||||
*
|
||||
* Returns: the head of this queue, or null if this queue is empty
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@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 //size 1
|
||||
{
|
||||
E e = head.e;
|
||||
head = tail = null;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E element()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peek()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return head == null && tail == null ? true : false;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o)
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@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()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a)
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified element from this collection,
|
||||
* if it is present (optional operation). More formally, removes an element
|
||||
* e such that (o==null ? e==null : o.equals(e)), if this collection
|
||||
* contains one or more such elements. Returns true if this collection
|
||||
* contained the specified element (or equivalently, if this collection
|
||||
* changed as a result of the call).
|
||||
*
|
||||
* @param o - element to be removed from this collection, if present
|
||||
* @throws ClassCastException - if the type of the specified element is
|
||||
* incompatible with this collection
|
||||
* @throws NullPointerException - if the specified element is null and this
|
||||
* collection does not permit null elements
|
||||
* @return true if an element was removed as a result of this call
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pos
|
||||
* @param e
|
||||
* @throws IndexOutOfBoundsException - if pos less 0 OR pos greater-equal
|
||||
* size()
|
||||
* @return
|
||||
*/
|
||||
private boolean removeAt(int pos, E e)
|
||||
{
|
||||
if (pos < 0 || pos >= size())
|
||||
{
|
||||
throw new IndexOutOfBoundsException(pos + " is out of bounds");
|
||||
}
|
||||
//1.list is empty
|
||||
if (isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//2. one node exists
|
||||
else if (size() == 1)
|
||||
{
|
||||
this.head = this.tail = null;
|
||||
}
|
||||
//3. remove in the front( head)
|
||||
else if (pos == 0)
|
||||
{
|
||||
this.head = this.head.r;
|
||||
head.l = null;
|
||||
}
|
||||
//4. remove in the end ( tail)
|
||||
else if (pos == size() - 1)
|
||||
{
|
||||
this.tail = this.tail.l;
|
||||
this.tail.r = null;
|
||||
}
|
||||
|
||||
//5. remove in the middle ( at least 3 nodes are in the queue)
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c)
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all of the elements in the specified collection to this collection
|
||||
* (optional operation). The behavior of this operation is undefined if the
|
||||
* specified collection is modified while the operation is in progress.
|
||||
* (This implies that the behavior of this call is undefined if the
|
||||
* specified collection is this collection, and this collection is
|
||||
* nonempty.)
|
||||
*
|
||||
* @param c - collection containing elements to be added to this collection
|
||||
* @throws ClassCastException - if the class of an element of the specified
|
||||
* collection prevents it from being added to this collection.
|
||||
* @throws NullPointerException - if the specified collection contains a
|
||||
* null element and this collection does not permit null elements, or if the
|
||||
* specified collection is null
|
||||
* @throws IllegalArgumentException - if some property of an element of the
|
||||
* specified collection prevents it from being added to this collection
|
||||
* @throws IllegalArgumentException - if some property of an element of the
|
||||
* specified collection prevents it from being added to this collection
|
||||
* @return true if this collection changed as a result of the call
|
||||
*/
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c)
|
||||
{
|
||||
int sizeBefore = size();
|
||||
for (E e : c)
|
||||
{
|
||||
add(e);
|
||||
}
|
||||
int sizeAfter = size();
|
||||
return sizeAfter > sizeBefore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c)
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c)
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
// head = tail = null;
|
||||
|
||||
//extra, no necessary to set the link of every node
|
||||
Node<E> p = head ;
|
||||
while ( p != tail )
|
||||
{
|
||||
p = p.r;
|
||||
p.l = null;
|
||||
}
|
||||
head = tail = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super E> action)
|
||||
{
|
||||
//1. use a pointer that points to the head
|
||||
//2. while the pointer has not reached the end of the queue
|
||||
//consume it
|
||||
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);
|
||||
|
||||
//TEST IT FULLY HERE. FOR ALL METHODS AND ALL CASES.
|
||||
//Have the Jzva PriorityQueue below
|
||||
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);}
|
||||
|
||||
|
||||
}
|
||||
}
|
BIN
Semester 3/ZIPs/MP6_CalebFontenot.zip
Normal file
BIN
Semester 3/ZIPs/MP6_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user