Implementato interamente un tda sequenze usando array (ArrayIndexList). Aggiunto reverse di sequenze in global.
This commit is contained in:
@@ -2,6 +2,8 @@ package com.xgiovio;
|
|||||||
|
|
||||||
import arraylist.ArrayIndexList;
|
import arraylist.ArrayIndexList;
|
||||||
import general_utility.test_object;
|
import general_utility.test_object;
|
||||||
|
import sequence.NodeSequence;
|
||||||
|
import sequence.Sequence;
|
||||||
import stack.NodeStack;
|
import stack.NodeStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,6 +31,9 @@ public class ArrayIndexListTest {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
61
com/xgiovio/ArraySequenceTest.java
Normal file
61
com/xgiovio/ArraySequenceTest.java
Normal 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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -53,6 +53,9 @@ public class NodeSequenceTest {
|
|||||||
a.set(6,new test_object(200));
|
a.set(6,new test_object(200));
|
||||||
|
|
||||||
System.out.print(a);
|
System.out.print(a);
|
||||||
|
global.reverse(a);
|
||||||
|
|
||||||
|
System.out.print(a);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
22
com/xgiovio/global.java
Normal file
22
com/xgiovio/global.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,6 +16,11 @@ import sequence.utility.DNodeIndex;
|
|||||||
*/
|
*/
|
||||||
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||||
|
|
||||||
|
|
||||||
|
public ArraySequence (int in){
|
||||||
|
super(in);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getFirst() throws EmptySequenceException {
|
public E getFirst() throws EmptySequenceException {
|
||||||
try {
|
try {
|
||||||
@@ -64,76 +69,131 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
|||||||
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
|
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
|
||||||
try {
|
try {
|
||||||
checkIndex(in_index, size());
|
checkIndex(in_index, size());
|
||||||
|
return new DNodeIndex<E>(get(in_index),in_index);
|
||||||
/////////////
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException err){
|
catch (IndexOutOfBoundsException err){
|
||||||
throw new BoundaryViolationException();
|
throw new BoundaryViolationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int indexOf(Position<E> position) throws InvalidPositionException {
|
public int indexOf(Position<E> position) throws InvalidPositionException {
|
||||||
return 0;
|
checkPosition(position);
|
||||||
|
return ((DNodeIndex<E>) position).getIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Position<E> first() throws EmptyListException {
|
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
|
@Override
|
||||||
public Position<E> last() throws EmptyListException {
|
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
|
@Override
|
||||||
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
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
|
@Override
|
||||||
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
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
|
@Override
|
||||||
public void addFirst(E e) {
|
public void addFirst(E e) {
|
||||||
|
add (0,e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addLast(E e) {
|
public void addLast(E e) {
|
||||||
|
add (size(),e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addAfter(Position<E> p, E e) throws InvalidPositionException {
|
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
|
@Override
|
||||||
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
|
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
|
@Override
|
||||||
public E remove(Position<E> p) throws InvalidPositionException {
|
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
|
@Override
|
||||||
public E set(Position<E> p, E e) throws InvalidPositionException {
|
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
|
@Override
|
||||||
public String toString() {
|
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 {
|
protected DNodeIndex<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||||
@@ -143,7 +203,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
DNodeIndex<E> temp = (DNodeIndex<E>) p;
|
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
|
throw new InvalidPositionException
|
||||||
("Position does not belong to a valid value");
|
("Position does not belong to a valid value");
|
||||||
return temp;
|
return temp;
|
||||||
@@ -152,4 +212,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
|||||||
("Position is of wrong type for this array");
|
("Position is of wrong type for this array");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,35 +13,27 @@ import position.Position;
|
|||||||
//Additional modifications and methods by xgiovio
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
public class DNodeIndex<E> implements Position<E> {
|
public class DNodeIndex<E> implements Position<E> {
|
||||||
private DNodeIndex<E> prev = null;
|
|
||||||
private DNodeIndex<E> next = null;
|
|
||||||
private E element = null;
|
private E element = null;
|
||||||
private int index = -1;
|
private int index = -1;
|
||||||
|
|
||||||
|
|
||||||
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> newNext, E elem, int in_idex) {
|
public DNodeIndex( E elem, int in_idex) {
|
||||||
prev = newPrev;
|
|
||||||
next = newNext;
|
|
||||||
element = elem;
|
element = elem;
|
||||||
index = in_idex;
|
index = in_idex;
|
||||||
}
|
}
|
||||||
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> newNext, E elem) {
|
public DNodeIndex( E elem) {
|
||||||
prev = newPrev;
|
|
||||||
next = newNext;
|
|
||||||
element = elem;
|
element = elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public E element() throws InvalidPositionException {
|
public E element() throws InvalidPositionException {
|
||||||
if ((prev == null) && (next == null))
|
if (index == -1)
|
||||||
throw new InvalidPositionException("Position is not in a list!");
|
throw new InvalidPositionException("Position is not in a list!");
|
||||||
return element;
|
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 void setElement(E newElement) { element = newElement; }
|
||||||
|
|
||||||
public int getIndex() {
|
public int getIndex() {
|
||||||
|
|||||||
Reference in New Issue
Block a user