From cafb12ea9f94516a2b6ddb433f15978706832e83 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Thu, 10 Apr 2014 00:21:49 +0200 Subject: [PATCH] Migliroato positions e agiunto come metodo alla interfaccia position. Fixato alcuni bug. Aggiunto test della prova intercorso del 10/4/14. Bisogna implementare tree --- com/xgiovio/ArraySequenceTest.java | 9 ++ com/xgiovio/NodePositionListTest.java | 34 ++++- iterator/IterablePosition.java | 94 +++++++++++++ iterator/IterablePositionIndexList.java | 73 ++++++++++ position/NodePositionList.java | 85 +----------- position/PositionList.java | 2 + sequence/ArraySequence.java | 12 ++ .../EmptyMyADT_Exception.java | 7 + test_prova_intercorso/ExStack_17_4_13.java | 126 ++++++++++++++++++ test_prova_intercorso/MyADT.java | 27 ++++ test_prova_intercorso/QueueMyADT.java | 87 ++++++++++++ test_prova_intercorso/test.java | 30 +++++ tree/LinkedTree.java | 3 + 13 files changed, 499 insertions(+), 90 deletions(-) create mode 100644 iterator/IterablePosition.java create mode 100644 iterator/IterablePositionIndexList.java create mode 100644 test_prova_intercorso/EmptyMyADT_Exception.java create mode 100644 test_prova_intercorso/ExStack_17_4_13.java create mode 100644 test_prova_intercorso/MyADT.java create mode 100644 test_prova_intercorso/QueueMyADT.java create mode 100644 test_prova_intercorso/test.java diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java index b187b53..a3a11d6 100644 --- a/com/xgiovio/ArraySequenceTest.java +++ b/com/xgiovio/ArraySequenceTest.java @@ -31,6 +31,15 @@ public class ArraySequenceTest { System.out.print(it.next()); System.out.print(it.hasNext()); + System.out.print("\n"); + Iterator> 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()); + + } diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index 7c5c140..e1f38e5 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -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)(it.next().); - - - - + System.out.print("\n"); + Iterator> 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()); } diff --git a/iterator/IterablePosition.java b/iterator/IterablePosition.java new file mode 100644 index 0000000..a518673 --- /dev/null +++ b/iterator/IterablePosition.java @@ -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 implements Iterable> { + + PositionList new_structure = null; + + public IterablePosition(PositionList structure){ + + new_structure = structure; + + } + + + @Override + public Iterator> iterator() { + return new getpositionsiterator(new_structure); + } + + + class getpositionsiterator implements Iterator> { + + PositionList new_structure = null; + Position pos = null; + + public getpositionsiterator (PositionList 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 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 (); + } + + + + } + + +} \ No newline at end of file diff --git a/iterator/IterablePositionIndexList.java b/iterator/IterablePositionIndexList.java new file mode 100644 index 0000000..bddbd75 --- /dev/null +++ b/iterator/IterablePositionIndexList.java @@ -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 + +public class IterablePositionIndexList implements Iterable> { + + Position[] new_structure = null; + + public IterablePositionIndexList (Position[] in){ + new_structure = in; + } + + @Override + public Iterator> iterator() { + return new getpositionsiterator(new_structure); + } + + + class getpositionsiterator implements Iterator>{ + + public getpositionsiterator(Position[] 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 next() throws NoSuchElementException { + if (hasNext()){ + pos++; + return data.get(pos); + }else { + throw new NoSuchElementException(); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException (); + } + + private ArrayIndexList> data = new ArrayIndexList>(); + private int pos = -1; + + + } + + +} diff --git a/position/NodePositionList.java b/position/NodePositionList.java index 826c58c..f988a24 100644 --- a/position/NodePositionList.java +++ b/position/NodePositionList.java @@ -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 implements PositionList { } - // inception style. a dream in a dream in a dream - public Iterable> positions() { - return new MyPositionsIterable(this); + return new IterablePosition(this); } - class MyPositionsIterable implements Iterable>{ - private NodePositionList base = null; - - public MyPositionsIterable(NodePositionList in){ - base = in; - } - - @Override - public Iterator> iterator() { - return new MyPositionsIterator(base); - } - - class MyPositionsIterator implements Iterator> { - public MyPositionsIterator (NodePositionList structure){ - - new_structure = new NodePositionList>(); - if (structure.size() != 0){ - Position 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 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> new_structure; - Position> pos = null; - } - - - - } - - // end inception style } diff --git a/position/PositionList.java b/position/PositionList.java index c9ccb65..6c408d4 100644 --- a/position/PositionList.java +++ b/position/PositionList.java @@ -36,6 +36,8 @@ public interface PositionList extends Iterable { public E set(Position p, E e) throws InvalidPositionException; + public Iterable> positions(); + } diff --git a/sequence/ArraySequence.java b/sequence/ArraySequence.java index f390a10..8b89ba9 100644 --- a/sequence/ArraySequence.java +++ b/sequence/ArraySequence.java @@ -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 implements Sequence { return new IndexListIterator(temp); } + + @Override + public Iterable> positions() { + + Position[] temp = new Position [this.size()]; + for (int i = 0 ;i < this.size(); i++){ + temp[i] = this.atIndex(i); + } + return new IterablePositionIndexList(temp); + + } } diff --git a/test_prova_intercorso/EmptyMyADT_Exception.java b/test_prova_intercorso/EmptyMyADT_Exception.java new file mode 100644 index 0000000..bdaaf7d --- /dev/null +++ b/test_prova_intercorso/EmptyMyADT_Exception.java @@ -0,0 +1,7 @@ +package test_prova_intercorso; + +public class EmptyMyADT_Exception extends RuntimeException { + + public EmptyMyADT_Exception(String error){super (error);} + +} diff --git a/test_prova_intercorso/ExStack_17_4_13.java b/test_prova_intercorso/ExStack_17_4_13.java new file mode 100644 index 0000000..60335fa --- /dev/null +++ b/test_prova_intercorso/ExStack_17_4_13.java @@ -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 data = new ArrayStack(); + 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; + } + + } + + } + + diff --git a/test_prova_intercorso/MyADT.java b/test_prova_intercorso/MyADT.java new file mode 100644 index 0000000..fed04a2 --- /dev/null +++ b/test_prova_intercorso/MyADT.java @@ -0,0 +1,27 @@ +package test_prova_intercorso; + +public interface MyADT{ + + + + 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); + + +} diff --git a/test_prova_intercorso/QueueMyADT.java b/test_prova_intercorso/QueueMyADT.java new file mode 100644 index 0000000..3dc2af5 --- /dev/null +++ b/test_prova_intercorso/QueueMyADT.java @@ -0,0 +1,87 @@ +package test_prova_intercorso; + +import queue.NodeQueue; + +public class QueueMyADT implements MyADT { + + + public QueueMyADT(){ + data = new NodeQueue (); + } + + NodeQueue 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(); + } +} diff --git a/test_prova_intercorso/test.java b/test_prova_intercorso/test.java new file mode 100644 index 0000000..f3bc382 --- /dev/null +++ b/test_prova_intercorso/test.java @@ -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 a = new QueueMyADT(); + 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); + + + + + + + } + +} diff --git a/tree/LinkedTree.java b/tree/LinkedTree.java index 8163323..0028969 100644 --- a/tree/LinkedTree.java +++ b/tree/LinkedTree.java @@ -70,4 +70,7 @@ public class LinkedTree implements Tree { public Iterator iterator() { return null; } + + + }