Implementato interamente un tda sequenze usando array (ArrayIndexList). Aggiunto reverse di sequenze in global.

This commit is contained in:
2014-03-30 21:44:37 +02:00
parent 5cc6be1a96
commit 483863d410
6 changed files with 179 additions and 33 deletions

View File

@@ -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 {
}
}

View File

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

View File

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

22
com/xgiovio/global.java Normal file
View File

@@ -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 <E > void reverse (Sequence<E> in){
if (in.size()> 1){
E t = in.removeFirst();
global.reverse(in);
in.addLast(t);
}
}
}

View File

@@ -16,6 +16,11 @@ import sequence.utility.DNodeIndex;
*/
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
public ArraySequence (int in){
super(in);
}
@Override
public E getFirst() throws EmptySequenceException {
try {
@@ -64,76 +69,131 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
try {
checkIndex(in_index, size());
/////////////
return new DNodeIndex<E>(get(in_index),in_index);
}
catch (IndexOutOfBoundsException err){
throw new BoundaryViolationException();
}
}
@Override
public int indexOf(Position<E> position) throws InvalidPositionException {
return 0;
checkPosition(position);
return ((DNodeIndex<E>) position).getIndex();
}
@Override
public Position<E> first() throws EmptyListException {
return null;
try{
return new DNodeIndex<E>(get(0),0);
}
catch (IndexOutOfBoundsException err){
throw new EmptyListException("Empty Array");
}
}
@Override
public Position<E> last() throws EmptyListException {
return null;
try{
return new DNodeIndex<E>(get(size() - 1),size() - 1);
}
catch (IndexOutOfBoundsException err){
throw new EmptyListException("Empty Array");
}
}
@Override
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
return null;
DNodeIndex<E> t = checkPosition(p);
if (t.getIndex()< size() -1){
return new DNodeIndex<E>(get(t.getIndex() + 1),t.getIndex() + 1);
}else{
throw new BoundaryViolationException();
}
}
@Override
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
return null;
DNodeIndex<E> t = checkPosition(p);
if (t.getIndex()> 0){
return new DNodeIndex<E>(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<E> p, E e) throws InvalidPositionException {
try {
DNodeIndex<E> t = checkPosition(p);
add(t.getIndex() + 1, e);
}
catch (IndexOutOfBoundsException err){
throw new InvalidPositionException();
}
}
@Override
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
try {
DNodeIndex<E> t = checkPosition(p);
add(t.getIndex() - 1,e);
}
catch (IndexOutOfBoundsException err){
throw new InvalidPositionException();
}
}
@Override
public E remove(Position<E> p) throws InvalidPositionException {
return null;
try {
DNodeIndex<E> t = checkPosition(p);
return remove(t.getIndex());
}
catch (IndexOutOfBoundsException err){
throw new InvalidPositionException();
}
}
@Override
public E set(Position<E> p, E e) throws InvalidPositionException {
return null;
try {
DNodeIndex<E> 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<E> checkPosition(Position<E> p) throws InvalidPositionException {
@@ -143,7 +203,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
try {
DNodeIndex<E> temp = (DNodeIndex<E>) 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<E> extends ArrayIndexList<E> implements Sequence<E> {
("Position is of wrong type for this array");
}
}
}

View File

@@ -13,35 +13,27 @@ import position.Position;
//Additional modifications and methods by xgiovio
public class DNodeIndex<E> implements Position<E> {
private DNodeIndex<E> prev = null;
private DNodeIndex<E> next = null;
private E element = null;
private int index = -1;
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> 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<E> newPrev, DNodeIndex<E> 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<E> getNext() { return next; }
public DNodeIndex<E> getPrev() { return prev; }
public void setNext(DNodeIndex<E> newNext) { next = newNext; }
public void setPrev(DNodeIndex<E> newPrev) { prev = newPrev; }
public void setElement(E newElement) { element = newElement; }
public int getIndex() {