Implementato NodeSequence e aggiunto alcuni costruttori alle eccezioni

This commit is contained in:
2014-03-30 16:56:16 +02:00
parent 148933ac58
commit c25afb02a2
8 changed files with 294 additions and 2 deletions

View File

@@ -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<test_object> a = new NodeSequence<test_object>();
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);
}
}

View File

@@ -11,4 +11,7 @@ public class EmptyDequeException extends RuntimeException {
public EmptyDequeException(){ public EmptyDequeException(){
super("Deque Empty"); super("Deque Empty");
} }
public EmptyDequeException(String msg){
super(msg);
}
} }

View File

@@ -11,4 +11,7 @@ public class EmptyQueueException extends RuntimeException {
public EmptyQueueException(){ public EmptyQueueException(){
super("Queue Empty"); super("Queue Empty");
} }
public EmptyQueueException(String msg){
super(msg);
}
} }

View File

@@ -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");
}
}

View File

@@ -11,4 +11,7 @@ public class EmptyStackException extends RuntimeException {
public EmptyStackException(){ public EmptyStackException(){
super("Stack Empty"); super("Stack Empty");
} }
public EmptyStackException(String msg){
super(msg);
}
} }

View File

@@ -15,9 +15,9 @@ public interface PositionList<E> {
public boolean isEmpty(); public boolean isEmpty();
public Position<E> first(); public Position<E> first() throws EmptyListException;
public Position<E> last(); public Position<E> last() throws EmptyListException;
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException; public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException;

175
sequence/NodeSequence.java Normal file
View File

@@ -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 <E> extends NodePositionList<E> implements Sequence<E> {
@Override
public E getFirst() throws EmptySequenceException {
try {
Position<E> t = first();
return t.element();
}
catch (EmptyListException e){
throw new EmptySequenceException();
}
}
@Override
public E getLast() throws EmptySequenceException {
try {
Position<E> t = last();
return t.element();
}
catch (EmptyListException e){
throw new EmptySequenceException();
}
}
@Override
public E removeFirst() throws EmptySequenceException {
try {
Position<E> t = first();
E ele = remove(t);
return ele;
}
catch (EmptyListException e){
throw new EmptySequenceException();
}
}
@Override
public E removeLast() throws EmptySequenceException {
try {
Position<E> 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<E> atIndex(int index) throws BoundaryViolationException {
try {
checkIndex(index, size());
Position<E> 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<E> position) throws InvalidPositionException {
checkPosition(position);
Position<E> 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<E> t = atIndex(i);
addBefore(t, e);
}
}
}
catch (BoundaryViolationException err){
throw new IndexOutOfBoundsException ();
}
}
@Override
public E set(int i, E e) throws IndexOutOfBoundsException {
try {
Position<E> 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<E> 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<E> 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;
}
}

24
sequence/Sequence.java Normal file
View File

@@ -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<E> extends PositionList<E>, IndexList<E> {
public E getFirst() throws EmptySequenceException;
public E getLast() throws EmptySequenceException;
public E removeFirst() throws EmptySequenceException;
public E removeLast() throws EmptySequenceException;
public Position<E> atIndex( int index) throws BoundaryViolationException;
public int indexOf(Position <E> position) throws InvalidPositionException;
}