From 5cc6be1a962ab05138434a79e195dd79ce1c7dc1 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 30 Mar 2014 17:43:15 +0200 Subject: [PATCH] Implementato parzialmente l'arraysequence --- position/NodePositionList.java | 3 +- position/{ => utility}/DNode.java | 4 +- sequence/ArraySequence.java | 155 ++++++++++++++++++++++++++++++ sequence/utility/DNodeIndex.java | 55 +++++++++++ 4 files changed, 214 insertions(+), 3 deletions(-) rename position/{ => utility}/DNode.java (95%) create mode 100644 sequence/ArraySequence.java create mode 100644 sequence/utility/DNodeIndex.java diff --git a/position/NodePositionList.java b/position/NodePositionList.java index fce1dcb..54b8eaa 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -1,7 +1,6 @@ package position; -import java.util.Iterator; import exceptions.*; -import stack.utility.Node; +import position.utility.DNode; /** * Realization of a PositionList using a doubly-linked list of nodes. diff --git a/position/DNode.java b/position/utility/DNode.java similarity index 95% rename from position/DNode.java rename to position/utility/DNode.java index 5f8695e..73bfaf1 100644 --- a/position/DNode.java +++ b/position/utility/DNode.java @@ -1,5 +1,7 @@ -package position; +package position.utility; import exceptions.*; +import position.Position; + /** * 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. diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java new file mode 100644 index 0000000..4541bee --- /dev/null +++ b/sequence/ArraySequence.java @@ -0,0 +1,155 @@ +package sequence; + +import arraylist.ArrayIndexList; +import exceptions.BoundaryViolationException; +import exceptions.EmptyListException; +import exceptions.EmptySequenceException; +import exceptions.InvalidPositionException; +import position.Position; +import sequence.utility.DNodeIndex; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 30/03/2014 + * Time: 17:19 + */ +public class ArraySequence extends ArrayIndexList implements Sequence { + + @Override + public E getFirst() throws EmptySequenceException { + try { + E ele = get(0); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E getLast() throws EmptySequenceException { + try { + E ele = get(size()-1); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeFirst() throws EmptySequenceException { + try { + E ele = remove(0); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeLast() throws EmptySequenceException { + try { + E ele = remove(size() - 1); + return ele; + } + catch (IndexOutOfBoundsException err){ + throw new EmptySequenceException(); + } + } + + @Override + public Position atIndex(int in_index) throws BoundaryViolationException { + try { + checkIndex(in_index, size()); + + ///////////// + + + } + catch (IndexOutOfBoundsException err){ + throw new BoundaryViolationException(); + } + + + } + + @Override + public int indexOf(Position position) throws InvalidPositionException { + return 0; + } + + @Override + public Position first() throws EmptyListException { + return null; + } + + @Override + public Position last() throws EmptyListException { + return null; + } + + @Override + public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { + return null; + } + + @Override + public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { + return null; + } + + @Override + public void addFirst(E e) { + + } + + @Override + public void addLast(E e) { + + } + + @Override + public void addAfter(Position p, E e) throws InvalidPositionException { + + } + + @Override + public void addBefore(Position p, E e) throws InvalidPositionException { + + } + + @Override + public E remove(Position p) throws InvalidPositionException { + return null; + } + + @Override + public E set(Position p, E e) throws InvalidPositionException { + return null; + } + + @Override + public String toString() { + return super.toString(); + } + + protected DNodeIndex checkPosition(Position p) throws InvalidPositionException { + if (p == null) + throw new InvalidPositionException + ("Null position passed"); + + try { + DNodeIndex temp = (DNodeIndex) p; + if ((temp.getPrev() == null) || (temp.getNext() == null) || (temp.getIndex() == -1)) + throw new InvalidPositionException + ("Position does not belong to a valid value"); + return temp; + } catch (ClassCastException e) { + throw new InvalidPositionException + ("Position is of wrong type for this array"); + } + } +} diff --git a/sequence/utility/DNodeIndex.java b/sequence/utility/DNodeIndex.java new file mode 100644 index 0000000..02307cb --- /dev/null +++ b/sequence/utility/DNodeIndex.java @@ -0,0 +1,55 @@ +package sequence.utility; + +import exceptions.InvalidPositionException; +import position.Position; + +/** + * 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 DNodeIndex implements Position { + private DNodeIndex prev = null; + private DNodeIndex next = null; + private E element = null; + private int index = -1; + + + public DNodeIndex(DNodeIndex newPrev, DNodeIndex newNext, E elem, int in_idex) { + prev = newPrev; + next = newNext; + element = elem; + index = in_idex; + } + public DNodeIndex(DNodeIndex newPrev, DNodeIndex 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 DNodeIndex getNext() { return next; } + public DNodeIndex getPrev() { return prev; } + + public void setNext(DNodeIndex newNext) { next = newNext; } + public void setPrev(DNodeIndex newPrev) { prev = newPrev; } + public void setElement(E newElement) { element = newElement; } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} +