implementato nodeposition con to string e reverse. aggiunto copyright
This commit is contained in:
@@ -6,6 +6,8 @@ package arraylist;
|
|||||||
* Date: 24/03/14
|
* Date: 24/03/14
|
||||||
* Time: 14:31
|
* Time: 14:31
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
public class ArrayIndexList<E> implements IndexList<E> {
|
public class ArrayIndexList<E> implements IndexList<E> {
|
||||||
private E[] array;
|
private E[] array;
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ package arraylist;
|
|||||||
* Date: 24/03/14
|
* Date: 24/03/14
|
||||||
* Time: 14:29
|
* Time: 14:29
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public interface IndexList<E> {
|
public interface IndexList<E> {
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
35
com/xgiovio/NodePositionListTest.java
Normal file
35
com/xgiovio/NodePositionListTest.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package com.xgiovio;
|
||||||
|
|
||||||
|
import arraylist.ArrayIndexList;
|
||||||
|
import general_utility.test_object;
|
||||||
|
import position.NodePositionList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with xgiovio.macbookair.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 23/03/14
|
||||||
|
* Time: 20:37
|
||||||
|
*/
|
||||||
|
public class NodePositionListTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NodePositionList<test_object> a = new NodePositionList<test_object>();
|
||||||
|
|
||||||
|
a.addLast(new test_object(1));
|
||||||
|
a.addLast(new test_object(2));
|
||||||
|
a.addLast(new test_object(3));
|
||||||
|
a.addLast(new test_object(4));
|
||||||
|
|
||||||
|
|
||||||
|
System.out.print(a);
|
||||||
|
a.reverse();
|
||||||
|
System.out.print(a);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ import exceptions.EmptyDequeException;
|
|||||||
* Date: 17/03/14
|
* Date: 17/03/14
|
||||||
* Time: 16:21
|
* Time: 16:21
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public interface Deque<E> {
|
public interface Deque<E> {
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import exceptions.EmptyDequeException;
|
|||||||
* Date: 17/03/14
|
* Date: 17/03/14
|
||||||
* Time: 16:26
|
* Time: 16:26
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public class NodeDeque<E> implements Deque <E> {
|
public class NodeDeque<E> implements Deque <E> {
|
||||||
|
|
||||||
public NodeDeque(){
|
public NodeDeque(){
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ package deque.utility;
|
|||||||
* Date: 17/03/14
|
* Date: 17/03/14
|
||||||
* Time: 16:32
|
* Time: 16:32
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public class DLNode <E> {
|
public class DLNode <E> {
|
||||||
|
|
||||||
public DLNode (){
|
public DLNode (){
|
||||||
|
|||||||
18
exceptions/BoundaryViolationException.java
Normal file
18
exceptions/BoundaryViolationException.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signals that the boundaries of a data structure have been illegally
|
||||||
|
* traversed (e.g. past the end of a list).
|
||||||
|
* @author Roberto Tamassia
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
public class BoundaryViolationException extends RuntimeException {
|
||||||
|
public BoundaryViolationException(String message) {
|
||||||
|
super (message);
|
||||||
|
}
|
||||||
|
public BoundaryViolationException() {
|
||||||
|
super ("BoundaryViolationException");
|
||||||
|
}
|
||||||
|
}
|
||||||
18
exceptions/EmptyListException.java
Normal file
18
exceptions/EmptyListException.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package exceptions;
|
||||||
|
/**
|
||||||
|
* Thrown when a list cannot fulfill the requested operation because
|
||||||
|
* it is empty.
|
||||||
|
* @author Roberto Tamassia
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
|
||||||
|
public class EmptyListException extends RuntimeException {
|
||||||
|
public EmptyListException(String message) {
|
||||||
|
super (message);
|
||||||
|
}
|
||||||
|
public EmptyListException() {
|
||||||
|
super ("EmptyListException");
|
||||||
|
}
|
||||||
|
}
|
||||||
21
exceptions/InvalidPositionException.java
Normal file
21
exceptions/InvalidPositionException.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package exceptions;
|
||||||
|
/**
|
||||||
|
* Thrown when a position is determined to be invalid.
|
||||||
|
* @author Roberto Tamassia, Michael Goodrich
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
|
||||||
|
public class InvalidPositionException extends RuntimeException {
|
||||||
|
public InvalidPositionException(String err) {
|
||||||
|
super(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidPositionException() {
|
||||||
|
super("Invalid Position");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
36
position/DNode.java
Normal file
36
position/DNode.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package position;
|
||||||
|
import exceptions.*;
|
||||||
|
/**
|
||||||
|
* A simple node class for a doubly-linked list. Each DNode has a
|
||||||
|
* reference to a stored element, a previous node, and a next node.
|
||||||
|
*
|
||||||
|
* @author Roberto Tamassia
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
public class DNode<E> implements Position<E> {
|
||||||
|
private DNode<E> prev = null;
|
||||||
|
private DNode<E> next = null;
|
||||||
|
private E element = null;
|
||||||
|
|
||||||
|
public DNode(DNode<E> newPrev, DNode<E> newNext, E elem) {
|
||||||
|
prev = newPrev;
|
||||||
|
next = newNext;
|
||||||
|
element = elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E element() throws InvalidPositionException {
|
||||||
|
if ((prev == null) && (next == null))
|
||||||
|
throw new InvalidPositionException("Position is not in a list!");
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DNode<E> getNext() { return next; }
|
||||||
|
public DNode<E> getPrev() { return prev; }
|
||||||
|
|
||||||
|
public void setNext(DNode<E> newNext) { next = newNext; }
|
||||||
|
public void setPrev(DNode<E> newPrev) { prev = newPrev; }
|
||||||
|
public void setElement(E newElement) { element = newElement; }
|
||||||
|
}
|
||||||
|
|
||||||
190
position/NodePositionList.java
Normal file
190
position/NodePositionList.java
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
package position;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import exceptions.*;
|
||||||
|
import stack.utility.Node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realization of a PositionList using a doubly-linked list of nodes.
|
||||||
|
*
|
||||||
|
* @author Michael Goodrich, Natasha Gelfand, Roberto Tamassia, Eric Zamore
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
public class NodePositionList<E> implements PositionList<E> {
|
||||||
|
|
||||||
|
protected int numElts; // Number of elements in the list
|
||||||
|
protected DNode<E> header, trailer; // Special sentinels
|
||||||
|
|
||||||
|
public NodePositionList() {
|
||||||
|
numElts = 0;
|
||||||
|
header = new DNode<E>(null, null, null); // create header
|
||||||
|
trailer = new DNode<E>(header, null, null); // create trailer
|
||||||
|
header.setNext(trailer); // make header and trailer point to each other
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DNode<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||||
|
if (p == null)
|
||||||
|
throw new InvalidPositionException
|
||||||
|
("Null position passed to NodeList");
|
||||||
|
if (p == header)
|
||||||
|
throw new InvalidPositionException
|
||||||
|
("The header node is not a valid position");
|
||||||
|
if (p == trailer)
|
||||||
|
throw new InvalidPositionException
|
||||||
|
("The trailer node is not a valid position");
|
||||||
|
try {
|
||||||
|
DNode<E> temp = (DNode<E>) p;
|
||||||
|
if ((temp.getPrev() == null) || (temp.getNext() == null))
|
||||||
|
throw new InvalidPositionException
|
||||||
|
("Position does not belong to a valid NodeList");
|
||||||
|
return temp;
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new InvalidPositionException
|
||||||
|
("Position is of wrong type for this list");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() { return numElts; }
|
||||||
|
|
||||||
|
public boolean isEmpty() { return (numElts == 0); }
|
||||||
|
|
||||||
|
public Position<E> first() throws EmptyListException {
|
||||||
|
if (isEmpty())
|
||||||
|
throw new EmptyListException("List is empty");
|
||||||
|
return header.getNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position<E> last()
|
||||||
|
throws EmptyListException {
|
||||||
|
if (isEmpty())
|
||||||
|
throw new EmptyListException("List is empty");
|
||||||
|
return trailer.getPrev();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
DNode<E> prev = v.getPrev();
|
||||||
|
if (prev == header)
|
||||||
|
throw new BoundaryViolationException
|
||||||
|
("Cannot advance past the beginning of the list");
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
DNode<E> next = v.getNext();
|
||||||
|
if (next == trailer)
|
||||||
|
throw new BoundaryViolationException
|
||||||
|
("Cannot advance past the end of the list");
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addBefore(Position<E> p, E element) throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
numElts++;
|
||||||
|
DNode<E> newNode = new DNode<E>(v.getPrev(), v, element);
|
||||||
|
v.getPrev().setNext(newNode);
|
||||||
|
v.setPrev(newNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAfter(Position<E> p, E element)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
numElts++;
|
||||||
|
DNode<E> newNode = new DNode<E>(v, v.getNext(), element);
|
||||||
|
v.getNext().setPrev(newNode);
|
||||||
|
v.setNext(newNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFirst(E element) {
|
||||||
|
numElts++;
|
||||||
|
DNode<E> newNode = new DNode<E>(header, header.getNext(), element);
|
||||||
|
header.getNext().setPrev(newNode);
|
||||||
|
header.setNext(newNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLast(E element) {
|
||||||
|
numElts++;
|
||||||
|
DNode<E> oldLast = trailer.getPrev();
|
||||||
|
DNode<E> newNode = new DNode<E>(oldLast, trailer, element);
|
||||||
|
oldLast.setNext(newNode);
|
||||||
|
trailer.setPrev(newNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public E remove(Position<E> p)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
numElts--;
|
||||||
|
DNode<E> vPrev = v.getPrev();
|
||||||
|
DNode<E> vNext = v.getNext();
|
||||||
|
vPrev.setNext(vNext);
|
||||||
|
vNext.setPrev(vPrev);
|
||||||
|
E vElem = v.element();
|
||||||
|
|
||||||
|
v.setNext(null);
|
||||||
|
v.setPrev(null);
|
||||||
|
return vElem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E set(Position<E> p, E element)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
E oldElt = v.element();
|
||||||
|
v.setElement(element);
|
||||||
|
return oldElt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isFirst(Position<E> p)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
return v.getPrev() == header;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLast(Position<E> p)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> v = checkPosition(p);
|
||||||
|
return v.getNext() == trailer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swapElements(Position<E> a, Position<E> b)
|
||||||
|
throws InvalidPositionException {
|
||||||
|
DNode<E> pA = checkPosition(a);
|
||||||
|
DNode<E> pB = checkPosition(b);
|
||||||
|
E temp = pA.element();
|
||||||
|
pA.setElement(pB.element());
|
||||||
|
pB.setElement(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[";
|
||||||
|
|
||||||
|
for ( DNode<E> temp = header.getNext(); temp != trailer ; temp = temp.getNext()){
|
||||||
|
|
||||||
|
if ( temp.getNext() == trailer){
|
||||||
|
to_return+=(temp.element().toString());
|
||||||
|
}else{
|
||||||
|
to_return+=(temp.element().toString() + ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
to_return = to_return + "]";
|
||||||
|
|
||||||
|
return to_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void reverse(){
|
||||||
|
if (size() > 1){
|
||||||
|
E temp = remove(last());
|
||||||
|
reverse();
|
||||||
|
addFirst(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
14
position/Position.java
Normal file
14
position/Position.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package position;
|
||||||
|
/**
|
||||||
|
* An interface for a position, which is a holder object storing a
|
||||||
|
* single element.
|
||||||
|
* @author Roberto Tamassia, Michael Goodrich
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
public interface Position<E> {
|
||||||
|
|
||||||
|
E element();
|
||||||
|
}
|
||||||
|
|
||||||
40
position/PositionList.java
Normal file
40
position/PositionList.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package position;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import exceptions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface for positional lists.
|
||||||
|
* @author Roberto Tamassia, Michael Goodrich
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
public interface PositionList<E> {
|
||||||
|
|
||||||
|
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
public boolean isEmpty();
|
||||||
|
|
||||||
|
public Position<E> first();
|
||||||
|
|
||||||
|
public Position<E> last();
|
||||||
|
|
||||||
|
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException;
|
||||||
|
|
||||||
|
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException;
|
||||||
|
|
||||||
|
public void addFirst(E e);
|
||||||
|
|
||||||
|
public void addLast(E e);
|
||||||
|
|
||||||
|
public void addAfter(Position<E> p, E e) throws InvalidPositionException;
|
||||||
|
|
||||||
|
public void addBefore(Position<E> p, E e) throws InvalidPositionException;
|
||||||
|
|
||||||
|
public E remove(Position<E> p) throws InvalidPositionException;
|
||||||
|
|
||||||
|
public E set(Position<E> p, E e) throws InvalidPositionException;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,6 +8,8 @@ import exceptions.EmptyQueueException;
|
|||||||
* Date: 10/03/14
|
* Date: 10/03/14
|
||||||
* Time: 15:49
|
* Time: 15:49
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public class ArrayQueue<E> implements Queue<E> {
|
public class ArrayQueue<E> implements Queue<E> {
|
||||||
|
|
||||||
public ArrayQueue(){
|
public ArrayQueue(){
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import java.awt.image.AreaAveragingScaleFilter;
|
|||||||
* Date: 17/03/14
|
* Date: 17/03/14
|
||||||
* Time: 14:08
|
* Time: 14:08
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public class NodeQueue<E> implements Queue<E> {
|
public class NodeQueue<E> implements Queue<E> {
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import exceptions.EmptyQueueException;
|
|||||||
* Date: 10/03/14
|
* Date: 10/03/14
|
||||||
* Time: 15:45
|
* Time: 15:45
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
public interface Queue<E> {
|
public interface Queue<E> {
|
||||||
|
|
||||||
public void enqueue (E element);
|
public void enqueue (E element);
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import exceptions.EmptyStackException;
|
|||||||
* Date: 05/03/14
|
* Date: 05/03/14
|
||||||
* Time: 0.12
|
* Time: 0.12
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
public class ArrayStack<E> implements Stack<E> {
|
public class ArrayStack<E> implements Stack<E> {
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import stack.utility.Node;
|
|||||||
* Date: 05/03/14
|
* Date: 05/03/14
|
||||||
* Time: 0.12
|
* Time: 0.12
|
||||||
*/
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
public class NodeStack<E> implements Stack<E> {
|
public class NodeStack<E> implements Stack<E> {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user