From 483863d410d1ba95d5909c6451193bad93f4e837 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 30 Mar 2014 21:44:37 +0200 Subject: [PATCH] Implementato interamente un tda sequenze usando array (ArrayIndexList). Aggiunto reverse di sequenze in global. --- com/xgiovio/ArrayIndexListTest.java | 5 ++ com/xgiovio/ArraySequenceTest.java | 61 +++++++++++++++++ com/xgiovio/NodeSequenceTest.java | 3 + com/xgiovio/global.java | 22 ++++++ sequence/ArraySequence.java | 101 ++++++++++++++++++++++------ sequence/utility/DNodeIndex.java | 20 ++---- 6 files changed, 179 insertions(+), 33 deletions(-) create mode 100644 com/xgiovio/ArraySequenceTest.java create mode 100644 com/xgiovio/global.java diff --git a/com/xgiovio/ArrayIndexListTest.java b/com/xgiovio/ArrayIndexListTest.java index f6626bc..5223fb6 100644 --- a/com/xgiovio/ArrayIndexListTest.java +++ b/com/xgiovio/ArrayIndexListTest.java @@ -2,6 +2,8 @@ package com.xgiovio; import arraylist.ArrayIndexList; import general_utility.test_object; +import sequence.NodeSequence; +import sequence.Sequence; import stack.NodeStack; /** @@ -29,6 +31,9 @@ public class ArrayIndexListTest { + + + } } diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java new file mode 100644 index 0000000..54c0965 --- /dev/null +++ b/com/xgiovio/ArraySequenceTest.java @@ -0,0 +1,61 @@ +package com.xgiovio; + +import arraylist.ArrayIndexList; +import general_utility.test_object; +import position.Position; +import sequence.ArraySequence; +import sequence.NodeSequence; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 23/03/14 + * Time: 20:37 + */ +public class ArraySequenceTest { + + public static void main(String[] args) { + ArraySequence a = new ArraySequence(10); + + a.add(0,new test_object(1)); + a.add(1,new test_object(2)); + a.add(2,new test_object(3)); + a.add(3,new test_object(4)); + + System.out.print(a); + a.remove(2); + System.out.print(a); + System.out.print(a.first().element()); + System.out.print(a.last().element()); + a.addLast(new test_object(1000)); + System.out.print(a); + a.addFirst(new test_object(2000)); + System.out.print(a); + a.add(0,new test_object(1)); + System.out.print(a); + a.add(5,new test_object(5)); + System.out.print(a); + a.add(6,new test_object(125)); + System.out.print(a); + a.add(8,new test_object(211)); + System.out.print(a); + a.set(0, new test_object(211)); + System.out.print(a); + a.set(1,new test_object(211)); + System.out.print(a); + + + global.reverse(a); + + System.out.print(a); + + + + + + + + + } + +} diff --git a/com/xgiovio/NodeSequenceTest.java b/com/xgiovio/NodeSequenceTest.java index 633c064..a242e28 100644 --- a/com/xgiovio/NodeSequenceTest.java +++ b/com/xgiovio/NodeSequenceTest.java @@ -53,6 +53,9 @@ public class NodeSequenceTest { a.set(6,new test_object(200)); System.out.print(a); + global.reverse(a); + + System.out.print(a); diff --git a/com/xgiovio/global.java b/com/xgiovio/global.java new file mode 100644 index 0000000..b1f0e0c --- /dev/null +++ b/com/xgiovio/global.java @@ -0,0 +1,22 @@ +package com.xgiovio; + +import sequence.Sequence; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 30/03/2014 + * Time: 21:35 + */ +class global { + public static void reverse (Sequence in){ + if (in.size()> 1){ + E t = in.removeFirst(); + global.reverse(in); + in.addLast(t); + + } + + } + +} diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java index 4541bee..e2f70cb 100644 --- a/sequence/ArraySequence.java +++ b/sequence/ArraySequence.java @@ -16,6 +16,11 @@ import sequence.utility.DNodeIndex; */ public class ArraySequence extends ArrayIndexList implements Sequence { + + public ArraySequence (int in){ + super(in); + } + @Override public E getFirst() throws EmptySequenceException { try { @@ -64,76 +69,131 @@ public class ArraySequence extends ArrayIndexList implements Sequence { public Position atIndex(int in_index) throws BoundaryViolationException { try { checkIndex(in_index, size()); - - ///////////// - - + return new DNodeIndex(get(in_index),in_index); } catch (IndexOutOfBoundsException err){ throw new BoundaryViolationException(); } - - } @Override public int indexOf(Position position) throws InvalidPositionException { - return 0; + checkPosition(position); + return ((DNodeIndex) position).getIndex(); } @Override public Position first() throws EmptyListException { - return null; + try{ + return new DNodeIndex(get(0),0); + } + catch (IndexOutOfBoundsException err){ + throw new EmptyListException("Empty Array"); + } } @Override public Position last() throws EmptyListException { - return null; + try{ + return new DNodeIndex(get(size() - 1),size() - 1); + } + catch (IndexOutOfBoundsException err){ + throw new EmptyListException("Empty Array"); + } } @Override public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { - return null; + DNodeIndex t = checkPosition(p); + if (t.getIndex()< size() -1){ + return new DNodeIndex(get(t.getIndex() + 1),t.getIndex() + 1); + }else{ + throw new BoundaryViolationException(); + } } @Override public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { - return null; + DNodeIndex t = checkPosition(p); + if (t.getIndex()> 0){ + return new DNodeIndex(get(t.getIndex() - 1),t.getIndex() - 1); + }else{ + throw new BoundaryViolationException(); + } } @Override public void addFirst(E e) { - + add (0,e); } @Override public void addLast(E e) { - + add (size(),e); } @Override public void addAfter(Position p, E e) throws InvalidPositionException { - + try { + DNodeIndex t = checkPosition(p); + add(t.getIndex() + 1, e); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } } @Override public void addBefore(Position p, E e) throws InvalidPositionException { - + try { + DNodeIndex t = checkPosition(p); + add(t.getIndex() - 1,e); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } } @Override public E remove(Position p) throws InvalidPositionException { - return null; + try { + DNodeIndex t = checkPosition(p); + return remove(t.getIndex()); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } } @Override public E set(Position p, E e) throws InvalidPositionException { - return null; + try { + DNodeIndex t = checkPosition(p); + return set (t.getIndex(),e); + } + catch (IndexOutOfBoundsException err){ + throw new InvalidPositionException(); + } + + } @Override public String toString() { - return super.toString(); + String to_return = ""; + to_return = to_return + "["; + + for ( int i = 0; i< size() ; i++){ + + if ( i== size() -1){ + to_return+=(get(i).toString()); + }else{ + to_return+=(get(i).toString() + ","); + } + } + to_return = to_return + "]"; + + return to_return; } protected DNodeIndex checkPosition(Position p) throws InvalidPositionException { @@ -143,7 +203,7 @@ public class ArraySequence extends ArrayIndexList implements Sequence { try { DNodeIndex temp = (DNodeIndex) p; - if ((temp.getPrev() == null) || (temp.getNext() == null) || (temp.getIndex() == -1)) + if ( temp.getIndex() >= size() || temp.getIndex() < 0 ) throw new InvalidPositionException ("Position does not belong to a valid value"); return temp; @@ -152,4 +212,7 @@ public class ArraySequence extends ArrayIndexList implements Sequence { ("Position is of wrong type for this array"); } } + + + } diff --git a/sequence/utility/DNodeIndex.java b/sequence/utility/DNodeIndex.java index 02307cb..c2e01a2 100644 --- a/sequence/utility/DNodeIndex.java +++ b/sequence/utility/DNodeIndex.java @@ -13,35 +13,27 @@ import position.Position; //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; + public DNodeIndex( E elem, int in_idex) { + element = elem; index = in_idex; } - public DNodeIndex(DNodeIndex newPrev, DNodeIndex newNext, E elem) { - prev = newPrev; - next = newNext; + public DNodeIndex( E elem) { + element = elem; - } + } public E element() throws InvalidPositionException { - if ((prev == null) && (next == null)) + if (index == -1) 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() {