implementato nodeposition con to string e reverse. aggiunto copyright
This commit is contained in:
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user