diff --git a/arraylist/ArrayIndexList.java b/arraylist/ArrayIndexList.java index fa55fff..0b83aa8 100644 --- a/arraylist/ArrayIndexList.java +++ b/arraylist/ArrayIndexList.java @@ -6,6 +6,8 @@ package arraylist; * Date: 24/03/14 * Time: 14:31 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class ArrayIndexList implements IndexList { private E[] array; diff --git a/arraylist/IndexList.java b/arraylist/IndexList.java index 247c9ec..bf08f34 100644 --- a/arraylist/IndexList.java +++ b/arraylist/IndexList.java @@ -6,6 +6,8 @@ package arraylist; * Date: 24/03/14 * Time: 14:29 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public interface IndexList { diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java new file mode 100644 index 0000000..8069bcb --- /dev/null +++ b/com/xgiovio/NodePositionListTest.java @@ -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 a = new NodePositionList(); + + 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); + + + + + + + } + +} diff --git a/deque/Deque.java b/deque/Deque.java index 931a499..7978192 100644 --- a/deque/Deque.java +++ b/deque/Deque.java @@ -8,6 +8,8 @@ import exceptions.EmptyDequeException; * Date: 17/03/14 * Time: 16:21 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public interface Deque { int size(); diff --git a/deque/NodeDeque.java b/deque/NodeDeque.java index 8715f04..28a01e3 100644 --- a/deque/NodeDeque.java +++ b/deque/NodeDeque.java @@ -9,6 +9,8 @@ import exceptions.EmptyDequeException; * Date: 17/03/14 * Time: 16:26 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class NodeDeque implements Deque { public NodeDeque(){ diff --git a/deque/utility/DLNode.java b/deque/utility/DLNode.java index 34ad38a..94cb2b7 100644 --- a/deque/utility/DLNode.java +++ b/deque/utility/DLNode.java @@ -6,6 +6,9 @@ package deque.utility; * Date: 17/03/14 * Time: 16:32 */ + +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class DLNode { public DLNode (){ diff --git a/exceptions/BoundaryViolationException.java b/exceptions/BoundaryViolationException.java new file mode 100644 index 0000000..ab93c0a --- /dev/null +++ b/exceptions/BoundaryViolationException.java @@ -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"); + } +} diff --git a/exceptions/EmptyListException.java b/exceptions/EmptyListException.java new file mode 100644 index 0000000..43a16ea --- /dev/null +++ b/exceptions/EmptyListException.java @@ -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"); + } +} diff --git a/exceptions/InvalidPositionException.java b/exceptions/InvalidPositionException.java new file mode 100644 index 0000000..2cebd38 --- /dev/null +++ b/exceptions/InvalidPositionException.java @@ -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"); + + } + +} + diff --git a/position/DNode.java b/position/DNode.java new file mode 100644 index 0000000..5f8695e --- /dev/null +++ b/position/DNode.java @@ -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 implements Position { + private DNode prev = null; + private DNode next = null; + private E element = null; + + public DNode(DNode newPrev, DNode 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 getNext() { return next; } + public DNode getPrev() { return prev; } + + public void setNext(DNode newNext) { next = newNext; } + public void setPrev(DNode newPrev) { prev = newPrev; } + public void setElement(E newElement) { element = newElement; } +} + diff --git a/position/NodePositionList.java b/position/NodePositionList.java new file mode 100644 index 0000000..fce1dcb --- /dev/null +++ b/position/NodePositionList.java @@ -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 implements PositionList { + + protected int numElts; // Number of elements in the list + protected DNode header, trailer; // Special sentinels + + public NodePositionList() { + numElts = 0; + header = new DNode(null, null, null); // create header + trailer = new DNode(header, null, null); // create trailer + header.setNext(trailer); // make header and trailer point to each other + } + + protected DNode checkPosition(Position 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 temp = (DNode) 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 first() throws EmptyListException { + if (isEmpty()) + throw new EmptyListException("List is empty"); + return header.getNext(); + } + + public Position last() + throws EmptyListException { + if (isEmpty()) + throw new EmptyListException("List is empty"); + return trailer.getPrev(); + } + + public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { + DNode v = checkPosition(p); + DNode prev = v.getPrev(); + if (prev == header) + throw new BoundaryViolationException + ("Cannot advance past the beginning of the list"); + return prev; + } + + public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { + DNode v = checkPosition(p); + DNode next = v.getNext(); + if (next == trailer) + throw new BoundaryViolationException + ("Cannot advance past the end of the list"); + return next; + } + + public void addBefore(Position p, E element) throws InvalidPositionException { + DNode v = checkPosition(p); + numElts++; + DNode newNode = new DNode(v.getPrev(), v, element); + v.getPrev().setNext(newNode); + v.setPrev(newNode); + } + + public void addAfter(Position p, E element) + throws InvalidPositionException { + DNode v = checkPosition(p); + numElts++; + DNode newNode = new DNode(v, v.getNext(), element); + v.getNext().setPrev(newNode); + v.setNext(newNode); + } + + public void addFirst(E element) { + numElts++; + DNode newNode = new DNode(header, header.getNext(), element); + header.getNext().setPrev(newNode); + header.setNext(newNode); + } + + public void addLast(E element) { + numElts++; + DNode oldLast = trailer.getPrev(); + DNode newNode = new DNode(oldLast, trailer, element); + oldLast.setNext(newNode); + trailer.setPrev(newNode); + } + + public E remove(Position p) + throws InvalidPositionException { + DNode v = checkPosition(p); + numElts--; + DNode vPrev = v.getPrev(); + DNode 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 p, E element) + throws InvalidPositionException { + DNode v = checkPosition(p); + E oldElt = v.element(); + v.setElement(element); + return oldElt; + } + + + + public boolean isFirst(Position p) + throws InvalidPositionException { + DNode v = checkPosition(p); + return v.getPrev() == header; + } + + public boolean isLast(Position p) + throws InvalidPositionException { + DNode v = checkPosition(p); + return v.getNext() == trailer; + } + + public void swapElements(Position a, Position b) + throws InvalidPositionException { + DNode pA = checkPosition(a); + DNode 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 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); + } + } + + +} diff --git a/position/Position.java b/position/Position.java new file mode 100644 index 0000000..8c41538 --- /dev/null +++ b/position/Position.java @@ -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 element(); +} + diff --git a/position/PositionList.java b/position/PositionList.java new file mode 100644 index 0000000..0d534d1 --- /dev/null +++ b/position/PositionList.java @@ -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 { + + + public int size(); + + public boolean isEmpty(); + + public Position first(); + + public Position last(); + + public Position next(Position p) throws InvalidPositionException, BoundaryViolationException; + + public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException; + + public void addFirst(E e); + + public void addLast(E e); + + public void addAfter(Position p, E e) throws InvalidPositionException; + + public void addBefore(Position p, E e) throws InvalidPositionException; + + public E remove(Position p) throws InvalidPositionException; + + public E set(Position p, E e) throws InvalidPositionException; + + +} + diff --git a/queue/ArrayQueue.java b/queue/ArrayQueue.java index c12a84f..a45c4c3 100644 --- a/queue/ArrayQueue.java +++ b/queue/ArrayQueue.java @@ -8,6 +8,8 @@ import exceptions.EmptyQueueException; * Date: 10/03/14 * Time: 15:49 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class ArrayQueue implements Queue { public ArrayQueue(){ diff --git a/queue/NodeQueue.java b/queue/NodeQueue.java index 993f7f6..db5daed 100644 --- a/queue/NodeQueue.java +++ b/queue/NodeQueue.java @@ -11,6 +11,8 @@ import java.awt.image.AreaAveragingScaleFilter; * Date: 17/03/14 * Time: 14:08 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class NodeQueue implements Queue { diff --git a/queue/Queue.java b/queue/Queue.java index 916f2f0..6cae156 100644 --- a/queue/Queue.java +++ b/queue/Queue.java @@ -8,6 +8,8 @@ import exceptions.EmptyQueueException; * Date: 10/03/14 * Time: 15:45 */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public interface Queue { public void enqueue (E element); diff --git a/stack/ArrayStack.java b/stack/ArrayStack.java index 8d30ad0..a7ca043 100644 --- a/stack/ArrayStack.java +++ b/stack/ArrayStack.java @@ -9,7 +9,8 @@ import exceptions.EmptyStackException; * Date: 05/03/14 * Time: 0.12 */ - +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class ArrayStack implements Stack { diff --git a/stack/NodeStack.java b/stack/NodeStack.java index 16655b8..81a8c3c 100644 --- a/stack/NodeStack.java +++ b/stack/NodeStack.java @@ -9,7 +9,8 @@ import stack.utility.Node; * Date: 05/03/14 * Time: 0.12 */ - +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio public class NodeStack implements Stack {