diff --git a/com/xgiovio/NodeSequenceTest.java b/com/xgiovio/NodeSequenceTest.java new file mode 100644 index 0000000..633c064 --- /dev/null +++ b/com/xgiovio/NodeSequenceTest.java @@ -0,0 +1,66 @@ +package com.xgiovio; + +import arraylist.ArrayIndexList; +import general_utility.test_object; +import sequence.NodeSequence; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 23/03/14 + * Time: 20:37 + */ +public class NodeSequenceTest { + + public static void main(String[] args) { + NodeSequence a = new NodeSequence(); + + System.out.print(a); + + a.add(0,new test_object(1)); + + System.out.print(a); + + a.add(1,new test_object(2)); + + + System.out.print(a); + + a.add(2,new test_object(3)); + System.out.print(a); + a.add(3,new test_object(4)); + + System.out.print(a); + a.remove(2); + System.out.print(a); + + a.add(0,new test_object(4)); + + System.out.print(a); + a.add(3,new test_object(10)); + + System.out.print(a); + a.add(4,new test_object(11)); + + System.out.print(a); + a.add(6,new test_object(100)); + + System.out.print(a); + + a.set(0,new test_object(100)); + + System.out.print(a); + a.set(6,new test_object(200)); + + System.out.print(a); + + + + + + + + + } + +} diff --git a/exceptions/EmptyDequeException.java b/exceptions/EmptyDequeException.java index e8f2afa..c786f29 100644 --- a/exceptions/EmptyDequeException.java +++ b/exceptions/EmptyDequeException.java @@ -11,4 +11,7 @@ public class EmptyDequeException extends RuntimeException { public EmptyDequeException(){ super("Deque Empty"); } + public EmptyDequeException(String msg){ + super(msg); + } } diff --git a/exceptions/EmptyQueueException.java b/exceptions/EmptyQueueException.java index 0a7c7e1..2a5acc1 100644 --- a/exceptions/EmptyQueueException.java +++ b/exceptions/EmptyQueueException.java @@ -11,4 +11,7 @@ public class EmptyQueueException extends RuntimeException { public EmptyQueueException(){ super("Queue Empty"); } + public EmptyQueueException(String msg){ + super(msg); + } } diff --git a/exceptions/EmptySequenceException.java b/exceptions/EmptySequenceException.java new file mode 100644 index 0000000..648f761 --- /dev/null +++ b/exceptions/EmptySequenceException.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 EmptySequenceException extends RuntimeException { + public EmptySequenceException(String message) { + super (message); + } + public EmptySequenceException() { + super ("EmptySequenceException"); + } +} diff --git a/exceptions/EmptyStackException.java b/exceptions/EmptyStackException.java index 7813f7b..2f3d29f 100644 --- a/exceptions/EmptyStackException.java +++ b/exceptions/EmptyStackException.java @@ -11,4 +11,7 @@ public class EmptyStackException extends RuntimeException { public EmptyStackException(){ super("Stack Empty"); } + public EmptyStackException(String msg){ + super(msg); + } } diff --git a/position/PositionList.java b/position/PositionList.java index 0d534d1..fd16f47 100644 --- a/position/PositionList.java +++ b/position/PositionList.java @@ -15,9 +15,9 @@ public interface PositionList { public boolean isEmpty(); - public Position first(); + public Position first() throws EmptyListException; - public Position last(); + public Position last() throws EmptyListException; public Position next(Position p) throws InvalidPositionException, BoundaryViolationException; diff --git a/sequence/NodeSequence.java b/sequence/NodeSequence.java new file mode 100644 index 0000000..7da205b --- /dev/null +++ b/sequence/NodeSequence.java @@ -0,0 +1,175 @@ +package sequence; + +import exceptions.BoundaryViolationException; +import exceptions.EmptyListException; +import exceptions.EmptySequenceException; +import exceptions.InvalidPositionException; +import position.NodePositionList; +import position.Position; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 30/03/2014 + * Time: 14:35 + */ +public class NodeSequence extends NodePositionList implements Sequence { + + @Override + public E getFirst() throws EmptySequenceException { + try { + Position t = first(); + return t.element(); + } + catch (EmptyListException e){ + throw new EmptySequenceException(); + } + } + + @Override + public E getLast() throws EmptySequenceException { + try { + Position t = last(); + return t.element(); + } + catch (EmptyListException e){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeFirst() throws EmptySequenceException { + try { + Position t = first(); + E ele = remove(t); + return ele; + } + catch (EmptyListException e){ + throw new EmptySequenceException(); + } + } + + @Override + public E removeLast() throws EmptySequenceException { + try { + Position t = last(); + E ele = remove(t); + return ele; + } + catch (EmptyListException e){ + throw new EmptySequenceException(); + } + } + + @Override // si potrebbe anche andare indietro a partire dall'ultimo + public Position atIndex(int index) throws BoundaryViolationException { + try { + checkIndex(index, size()); + Position t = first(); + for (int i = 0; i < index; i++) { + t = next(t); + } + return t; + } + catch (IndexOutOfBoundsException e){ + throw new BoundaryViolationException(); + } + + } + + @Override + public int indexOf(Position position) throws InvalidPositionException { + checkPosition(position); + Position t = first(); + int i ; + for (i = 0 ;t != position;i++){ + t= next(t); + } + return i; + + } + + @Override + public E remove(int i) throws IndexOutOfBoundsException { + checkIndex(i,size()); + E ele = remove(atIndex(i)); + return ele; + + } + + @Override + public void add(int i, E e) throws IndexOutOfBoundsException { + try { + checkIndex(i,size()+1); + if (i == 0){ + addFirst(e); + } else { + if ( i== size()){ + addLast(e); + }else { + Position t = atIndex(i); + addBefore(t, e); + } + } + } + catch (BoundaryViolationException err){ + throw new IndexOutOfBoundsException (); + } + + } + + @Override + public E set(int i, E e) throws IndexOutOfBoundsException { + try { + Position t = atIndex(i); + E ele = set(t,e); + return ele; + } + catch (BoundaryViolationException err){ + throw new IndexOutOfBoundsException (); + } + } + + @Override + public E get(int i) throws IndexOutOfBoundsException { + try { + Position t = atIndex(i); + return t.element(); + } + catch (BoundaryViolationException err){ + throw new IndexOutOfBoundsException (); + } + } + + + protected void checkIndex(int r, int n) throws IndexOutOfBoundsException { + if (r < 0 || r >= n) + throw new IndexOutOfBoundsException("Illegal index: " + r); + } + + @Override + public String toString() { + String to_return = ""; + to_return = to_return + "["; + if (size() > 0) { + int i; + Position temp; + for (temp = first(), i = 0;true;) { + + if ( i == size() - 1) { + to_return += (temp.element().toString()); + } else { + to_return += (temp.element().toString() + ","); + } + if (i == size() - 1){ + break; + }else{ + i++; + temp = next(temp); + } + } + } + to_return = to_return + "]"; + return to_return; + } +} diff --git a/sequence/Sequence.java b/sequence/Sequence.java new file mode 100644 index 0000000..013eedc --- /dev/null +++ b/sequence/Sequence.java @@ -0,0 +1,24 @@ +package sequence; + +import arraylist.IndexList; +import position.Position; +import position.PositionList; +import exceptions.*; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 30/03/2014 + * Time: 14:31 + */ +public interface Sequence extends PositionList, IndexList { + public E getFirst() throws EmptySequenceException; + public E getLast() throws EmptySequenceException; + public E removeFirst() throws EmptySequenceException; + public E removeLast() throws EmptySequenceException; + + public Position atIndex( int index) throws BoundaryViolationException; + public int indexOf(Position position) throws InvalidPositionException; + + +}