Implementato parzialmente l'arraysequence
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package position;
|
||||
import java.util.Iterator;
|
||||
import exceptions.*;
|
||||
import stack.utility.Node;
|
||||
import position.utility.DNode;
|
||||
|
||||
/**
|
||||
* Realization of a PositionList using a doubly-linked list of nodes.
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package position;
|
||||
package position.utility;
|
||||
import exceptions.*;
|
||||
import position.Position;
|
||||
|
||||
/**
|
||||
* A simple node class for a doubly-linked list. Each DNode has a
|
||||
* reference to a stored element, a previous node, and a next node.
|
||||
155
sequence/ArraySequence.java
Normal file
155
sequence/ArraySequence.java
Normal file
@@ -0,0 +1,155 @@
|
||||
package sequence;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import exceptions.BoundaryViolationException;
|
||||
import exceptions.EmptyListException;
|
||||
import exceptions.EmptySequenceException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.Position;
|
||||
import sequence.utility.DNodeIndex;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 30/03/2014
|
||||
* Time: 17:19
|
||||
*/
|
||||
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
@Override
|
||||
public E getFirst() throws EmptySequenceException {
|
||||
try {
|
||||
E ele = get(0);
|
||||
return ele;
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getLast() throws EmptySequenceException {
|
||||
try {
|
||||
E ele = get(size()-1);
|
||||
return ele;
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeFirst() throws EmptySequenceException {
|
||||
try {
|
||||
E ele = remove(0);
|
||||
return ele;
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeLast() throws EmptySequenceException {
|
||||
try {
|
||||
E ele = remove(size() - 1);
|
||||
return ele;
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
|
||||
try {
|
||||
checkIndex(in_index, size());
|
||||
|
||||
/////////////
|
||||
|
||||
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Position<E> position) throws InvalidPositionException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> first() throws EmptyListException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> last() throws EmptyListException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(E e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(E e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAfter(Position<E> p, E e) throws InvalidPositionException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(Position<E> p) throws InvalidPositionException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(Position<E> p, E e) throws InvalidPositionException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
protected DNodeIndex<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||
if (p == null)
|
||||
throw new InvalidPositionException
|
||||
("Null position passed");
|
||||
|
||||
try {
|
||||
DNodeIndex<E> temp = (DNodeIndex<E>) p;
|
||||
if ((temp.getPrev() == null) || (temp.getNext() == null) || (temp.getIndex() == -1))
|
||||
throw new InvalidPositionException
|
||||
("Position does not belong to a valid value");
|
||||
return temp;
|
||||
} catch (ClassCastException e) {
|
||||
throw new InvalidPositionException
|
||||
("Position is of wrong type for this array");
|
||||
}
|
||||
}
|
||||
}
|
||||
55
sequence/utility/DNodeIndex.java
Normal file
55
sequence/utility/DNodeIndex.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package sequence.utility;
|
||||
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.Position;
|
||||
|
||||
/**
|
||||
* A simple node class for a doubly-linked list. Each DNode has a
|
||||
* reference to a stored element, a previous node, and a next node.
|
||||
*
|
||||
* @author Roberto Tamassia
|
||||
*/
|
||||
//Copyright (c) 2003 Brown University, Providence, RI
|
||||
//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;
|
||||
element = elem;
|
||||
index = in_idex;
|
||||
}
|
||||
public DNodeIndex(DNodeIndex<E> newPrev, DNodeIndex<E> newNext, E elem) {
|
||||
prev = newPrev;
|
||||
next = newNext;
|
||||
element = elem;
|
||||
}
|
||||
|
||||
public E element() throws InvalidPositionException {
|
||||
if ((prev == null) && (next == null))
|
||||
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() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user