Migliroato positions e agiunto come metodo alla interfaccia position. Fixato alcuni bug. Aggiunto test della prova intercorso del 10/4/14. Bisogna implementare tree
This commit is contained in:
@@ -31,6 +31,15 @@ public class ArraySequenceTest {
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
|
||||
System.out.print("\n");
|
||||
Iterator<Position<test_object>> itp = a.positions().iterator();
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -63,14 +63,34 @@ public class NodePositionListTest {
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
|
||||
System.out.print(a.positions().iterator());
|
||||
System.out.print(it.hasNext());
|
||||
System.out.print(((Position<Integer>)(it.next().);
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.print("\n");
|
||||
Iterator<Position<Integer>> itp = a.positions().iterator();
|
||||
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
|
||||
|
||||
}
|
||||
|
||||
94
iterator/IterablePosition.java
Normal file
94
iterator/IterablePosition.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import javafx.geometry.Pos;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:06
|
||||
*/
|
||||
|
||||
//iterable of positions implementation with cursor for PositionList data structures.
|
||||
|
||||
public class IterablePosition<E> implements Iterable<Position<E>> {
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
|
||||
public IterablePosition(PositionList<E> structure){
|
||||
|
||||
new_structure = structure;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> iterator() {
|
||||
return new getpositionsiterator(new_structure);
|
||||
}
|
||||
|
||||
|
||||
class getpositionsiterator implements Iterator<Position<E>> {
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
public getpositionsiterator (PositionList<E> in){
|
||||
new_structure = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure. size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos;
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
73
iterator/IterablePositionIndexList.java
Normal file
73
iterator/IterablePositionIndexList.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package iterator;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import position.Position;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:13
|
||||
*/
|
||||
|
||||
// generic iterable list creation using an arraylist. the class calling this metod should pass an array of Position<E>
|
||||
|
||||
public class IterablePositionIndexList<E> implements Iterable<Position<E>> {
|
||||
|
||||
Position<E>[] new_structure = null;
|
||||
|
||||
public IterablePositionIndexList (Position<E>[] in){
|
||||
new_structure = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> iterator() {
|
||||
return new getpositionsiterator(new_structure);
|
||||
}
|
||||
|
||||
|
||||
class getpositionsiterator implements Iterator<Position<E>>{
|
||||
|
||||
public getpositionsiterator(Position<E>[] in_elements){
|
||||
for (int i = 0;i< in_elements.length;i++){
|
||||
data.add(i,in_elements[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
|
||||
if (pos <= data.size() -2){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
pos++;
|
||||
return data.get(pos);
|
||||
}else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
private ArrayIndexList<Position<E>> data = new ArrayIndexList<Position<E>>();
|
||||
private int pos = -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package position;
|
||||
import exceptions.*;
|
||||
import iterator.ElementIterator;
|
||||
import iterator.IterablePosition;
|
||||
import position.utility.DNode;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Realization of a PositionList using a doubly-linked list of nodes.
|
||||
@@ -198,91 +198,10 @@ public class NodePositionList<E> implements PositionList<E> {
|
||||
}
|
||||
|
||||
|
||||
// inception style. a dream in a dream in a dream
|
||||
|
||||
public Iterable<Position<E>> positions() {
|
||||
return new MyPositionsIterable(this);
|
||||
}
|
||||
|
||||
class MyPositionsIterable implements Iterable<Position<E>>{
|
||||
|
||||
private NodePositionList<E> base = null;
|
||||
|
||||
public MyPositionsIterable(NodePositionList<E> in){
|
||||
base = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> iterator() {
|
||||
return new MyPositionsIterator(base);
|
||||
}
|
||||
|
||||
class MyPositionsIterator <E> implements Iterator<Position<E>> {
|
||||
public MyPositionsIterator (NodePositionList<E> structure){
|
||||
|
||||
new_structure = new NodePositionList<Position<E>>();
|
||||
if (structure.size() != 0){
|
||||
Position<E> temp;
|
||||
for (temp = structure.first() ; temp!= structure.last() ; temp = structure.next(temp)){
|
||||
new_structure.addLast(temp);
|
||||
}
|
||||
new_structure.addLast(temp);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure.size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos.element();
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
|
||||
}
|
||||
|
||||
NodePositionList<Position<E>> new_structure;
|
||||
Position<Position<E>> pos = null;
|
||||
return new IterablePosition(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// end inception style
|
||||
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ public interface PositionList<E> extends Iterable<E> {
|
||||
|
||||
public E set(Position<E> p, E e) throws InvalidPositionException;
|
||||
|
||||
public Iterable<Position<E>> positions();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import exceptions.EmptyListException;
|
||||
import exceptions.EmptySequenceException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import iterator.IndexListIterator;
|
||||
import iterator.IterablePositionIndexList;
|
||||
import position.Position;
|
||||
import sequence.utility.ArrayPosition;
|
||||
|
||||
@@ -298,4 +299,15 @@ public class ArraySequence<E> implements Sequence<E> {
|
||||
return new IndexListIterator<E>(temp);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Position<E>> positions() {
|
||||
|
||||
Position<E>[] temp = new Position [this.size()];
|
||||
for (int i = 0 ;i < this.size(); i++){
|
||||
temp[i] = this.atIndex(i);
|
||||
}
|
||||
return new IterablePositionIndexList<E>(temp);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
7
test_prova_intercorso/EmptyMyADT_Exception.java
Normal file
7
test_prova_intercorso/EmptyMyADT_Exception.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package test_prova_intercorso;
|
||||
|
||||
public class EmptyMyADT_Exception extends RuntimeException {
|
||||
|
||||
public EmptyMyADT_Exception(String error){super (error);}
|
||||
|
||||
}
|
||||
126
test_prova_intercorso/ExStack_17_4_13.java
Normal file
126
test_prova_intercorso/ExStack_17_4_13.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package test_prova_intercorso;
|
||||
|
||||
import stack.ArrayStack;
|
||||
|
||||
//importare le classi necessarie
|
||||
|
||||
/* Il programma deve stampare:
|
||||
|
||||
I test
|
||||
Il carattere 'a' ha livello di annidamento 2
|
||||
Il carattere 'b' ha livello di annidamento 2
|
||||
Il carattere 'c' ha livello di annidamento 2
|
||||
Il carattere 'd' ha livello di annidamento 3
|
||||
Il carattere 'e' ha livello di annidamento 4
|
||||
Il carattere 'f' ha livello di annidamento 3
|
||||
Il carattere 'g' ha livello di annidamento 2
|
||||
Il carattere 'h' ha livello di annidamento 2
|
||||
Il carattere 'i' ha livello di annidamento 2
|
||||
Il carattere 'l' ha livello di annidamento 2
|
||||
Il carattere 'm' ha livello di annidamento 4
|
||||
Il carattere 'n' ha livello di annidamento 3
|
||||
Il carattere 'o' ha livello di annidamento 2
|
||||
Il carattere 'p' ha livello di annidamento 1
|
||||
Il carattere 'q' ha livello di annidamento 0
|
||||
|
||||
II test
|
||||
L'espressione non e` ben parentesizzata
|
||||
|
||||
III test
|
||||
L'espressione non e` ben parentesizzata
|
||||
|
||||
*/
|
||||
|
||||
public class ExStack_17_4_13 {
|
||||
|
||||
|
||||
|
||||
public static void main(String [] args){
|
||||
System.out.println("I test");
|
||||
String s="((abc(d(e)f)g)(hi)(l((m)n)o)p)q";
|
||||
for(int i=0;i<s.length();i++)
|
||||
for(int j=0;j<5;j++){
|
||||
char c= s.charAt(i);
|
||||
if(c!='(' && c!=')'&& checkLevel(s,s.charAt(i),j))
|
||||
System.out.println("Il carattere '" + s.charAt(i) + "' ha livello di annidamento "+ j);
|
||||
}
|
||||
|
||||
System.out.println("\nII test");
|
||||
s="((abc(d(e)f)g)(hi))(l((m)n)o)p)q";
|
||||
boolean f= false;
|
||||
for(int i=0;i<s.length();i++)
|
||||
for(int j=0;j<5;j++){
|
||||
char c= s.charAt(i);
|
||||
if(c!='(' && c!=')'&& checkLevel(s,s.charAt(i),j))
|
||||
{System.out.println("Il carattere '" + s.charAt(i) + "' ha livello di annidamento "+ j);
|
||||
f= true;
|
||||
}
|
||||
}
|
||||
if(f==false)System.out.println("L'espressione non e` ben parentesizzata");
|
||||
|
||||
|
||||
System.out.println("\nIII test");
|
||||
s="((abcd(e)(f)(g)(hi)(l((m)(n)o)p)q";
|
||||
f=false;
|
||||
for(int i=0;i<s.length();i++)
|
||||
for(int j=0;j<6;j++){
|
||||
char c= s.charAt(i);
|
||||
if(c!='(' && c!=')'&& checkLevel(s,s.charAt(i),j))
|
||||
{System.out.println("Il carattere '" + s.charAt(i) + "' ha livello di annidamento "+ j);
|
||||
f= true;
|
||||
}
|
||||
}
|
||||
if(f==false)System.out.println("L'espressione non e` ben parentesizzata");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//scrivere qui la funzione
|
||||
public static boolean checkLevel(String s, char x, int i){
|
||||
|
||||
ArrayStack<Character> data = new ArrayStack<Character>();
|
||||
int annidamento = 0;
|
||||
int found = 0;
|
||||
for (int n = 0 ; n< s.length() ;n++){
|
||||
if (s.charAt(n) == '('){
|
||||
data.push(s.charAt(n));
|
||||
annidamento++;
|
||||
if (annidamento > i){
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (s.charAt(n) == ')'){
|
||||
if (annidamento <= 0){
|
||||
return false;
|
||||
}else {
|
||||
for (int k = 0; k< data.size();k++){
|
||||
data.pop();
|
||||
}
|
||||
annidamento--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (s.charAt(n) == x && annidamento == i && found == 0){
|
||||
found = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data.size() == 0 && found == 1){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
27
test_prova_intercorso/MyADT.java
Normal file
27
test_prova_intercorso/MyADT.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package test_prova_intercorso;
|
||||
|
||||
public interface MyADT<E>{
|
||||
|
||||
|
||||
|
||||
public int size();
|
||||
|
||||
public boolean isEmpty();
|
||||
|
||||
/* se il numero di elementi contenuti nella struttura dati e` dispari,
|
||||
il metodo mid restituisce l<>elemento centrale; se il numero di elementi
|
||||
e` pari, il metodo restituisce null.
|
||||
*/
|
||||
public E mid() throws EmptyMyADT_Exception;
|
||||
|
||||
|
||||
|
||||
public E removeMid()throws EmptyMyADT_Exception;
|
||||
|
||||
/*
|
||||
Il metodo insert inserisce l<>elemento e alla fine della collezione
|
||||
*/
|
||||
public void insert(E e);
|
||||
|
||||
|
||||
}
|
||||
87
test_prova_intercorso/QueueMyADT.java
Normal file
87
test_prova_intercorso/QueueMyADT.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package test_prova_intercorso;
|
||||
|
||||
import queue.NodeQueue;
|
||||
|
||||
public class QueueMyADT<E> implements MyADT<E> {
|
||||
|
||||
|
||||
public QueueMyADT(){
|
||||
data = new NodeQueue <E> ();
|
||||
}
|
||||
|
||||
NodeQueue <E> data;
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
|
||||
return data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
|
||||
return data.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E mid() throws EmptyMyADT_Exception {
|
||||
|
||||
|
||||
if (data.size() < 1){
|
||||
throw new EmptyMyADT_Exception("Lista vuota");
|
||||
}
|
||||
|
||||
if (data.size() % 2 == 1){
|
||||
|
||||
E to_return = null;
|
||||
for (int i = 0 ; i< data.size() ;i++){
|
||||
if (i == data.size() / 2){
|
||||
to_return = data.front();
|
||||
}
|
||||
data.enqueue(data.dequeue());
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public E removeMid() throws EmptyMyADT_Exception {
|
||||
|
||||
if (data.size() < 1){
|
||||
throw new EmptyMyADT_Exception("Lista vuota");
|
||||
}
|
||||
|
||||
if (data.size() % 2 == 1){
|
||||
|
||||
E to_return = null;
|
||||
int original_size = data.size();
|
||||
for (int i = 0 ; i< original_size ;i++){
|
||||
if (i == original_size / 2){
|
||||
to_return = data.dequeue();
|
||||
}else{
|
||||
data.enqueue(data.dequeue());
|
||||
}
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(E e) {
|
||||
data.enqueue(e);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return data.toString();
|
||||
}
|
||||
}
|
||||
30
test_prova_intercorso/test.java
Normal file
30
test_prova_intercorso/test.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package test_prova_intercorso;
|
||||
|
||||
public class test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.print(ExStack_17_4_13.checkLevel("((aad)())", 'd', 2));
|
||||
|
||||
QueueMyADT<Integer> a = new QueueMyADT<Integer>();
|
||||
a.insert(10);
|
||||
a.insert(20);
|
||||
a.insert(30);
|
||||
a.insert(40);
|
||||
a.insert(50);
|
||||
|
||||
System.out.print (a);
|
||||
|
||||
|
||||
System.out.print (a.mid());
|
||||
|
||||
System.out.print (a);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,4 +70,7 @@ public class LinkedTree <E> implements Tree<E> {
|
||||
public Iterator<E> iterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user