From: Jérôme Benoit Date: Sun, 4 Mar 2018 16:38:43 +0000 (+0100) Subject: Add the Compactable interface. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5731ae5f40887e5a299eeca6d9047c06cd17ab41;p=TP_POO.git Add the Compactable interface. Need testing. Signed-off-by: Jérôme Benoit --- diff --git a/Entiers/Entiers.java b/Entiers/Entiers.java index 4f1168e..c878f63 100644 --- a/Entiers/Entiers.java +++ b/Entiers/Entiers.java @@ -60,7 +60,7 @@ class Entiers { private int binarySearch(int first, int last, int value) { if (last < first) - //FIXME: should not return an integer + //FIXME: should not return an integer return -1; int middle = (first + last) / 2; if (value == int_array[middle]) @@ -98,7 +98,8 @@ class Entiers { } public void afficher() { - System.out.println("----"); + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); for (int i = 0; i < getCurrentSize(); i++) { System.out.println("element " + i + " " + int_array[i]); } diff --git a/Listes/Liste.java b/Listes/Liste.java index 167a79d..ca2d42d 100644 --- a/Listes/Liste.java +++ b/Listes/Liste.java @@ -116,20 +116,22 @@ public class Liste { } public void afficher() { - System.out.println("----"); + String className = this.getClass().getSimpleName(); + int i = 0; + System.out.println("---- " + className + " ----"); if (isEmpty()) { - System.out.println("Liste vide"); + return; } else if (headNode.getNext() == null) { - System.out.println("Valeur du noeud 0 : " + headNode.getData()); + System.out.println("element " + i + " : " + headNode.getData()); } else { - IntNode nodeCursor = headNode; - int i = 0; - while (nodeCursor.getNext() != null) { - System.out.println("Valeur du noeud " + i + " : " + nodeCursor.getData()); - nodeCursor = nodeCursor.getNext(); + IntNode nodeCursorNext = headNode.getNext(); + System.out.println("element " + i + " : " + headNode.getData()); + i++; + while (nodeCursorNext != null) { + System.out.println("element " + i + " : " + nodeCursorNext.getData()); + nodeCursorNext = nodeCursorNext.getNext(); i++; } - System.out.println("Valeur du noeud " + i++ + " : " + nodeCursor.getData()); } } diff --git a/Structure/Entiers.java b/Structure/Entiers.java index b4cffb1..80fcc90 100644 --- a/Structure/Entiers.java +++ b/Structure/Entiers.java @@ -60,7 +60,7 @@ class Entiers extends Structure { private int binarySearch(int first, int last, int value) { if (last < first) - //FIXME: should not return an integer + //FIXME: should not return an integer return -1; int middle = (first + last) / 2; if (value == int_array[middle]) @@ -98,7 +98,8 @@ class Entiers extends Structure { } public void afficher() { - System.out.println("---- entiers ----"); + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); for (int i = 0; i < getCurrentSize(); i++) { System.out.println("element " + i + " : " + int_array[i]); } diff --git a/Structure/Liste.java b/Structure/Liste.java index da22d8a..448878f 100644 --- a/Structure/Liste.java +++ b/Structure/Liste.java @@ -118,20 +118,22 @@ public class Liste extends Structure { } public void afficher() { - System.out.println("---- liste ----"); + String className = this.getClass().getSimpleName(); + int i = 0; + System.out.println("---- " + className + " ----"); if (isEmpty()) { return; } else if (headNode.getNext() == null) { - System.out.println("element 0 : " + headNode.getData()); + System.out.println("element " + i + " : " + headNode.getData()); } else { - IntNode nodeCursor = headNode; - int i = 0; - while (nodeCursor.getNext() != null) { - System.out.println("element " + i + " : " + nodeCursor.getData()); - nodeCursor = nodeCursor.getNext(); + IntNode nodeCursorNext = headNode.getNext(); + System.out.println("element " + i + " : " + headNode.getData()); + i++; + while (nodeCursorNext != null) { + System.out.println("element " + i + " : " + nodeCursorNext.getData()); + nodeCursorNext = nodeCursorNext.getNext(); i++; } - System.out.println("element " + i++ + " : " + nodeCursor.getData()); } } diff --git a/TP2/Entiers.java b/TP2/Entiers.java index a1e18b6..7d55c42 100644 --- a/TP2/Entiers.java +++ b/TP2/Entiers.java @@ -75,7 +75,6 @@ class Entiers extends Structure { System.out.println("Aucune valeur à supprimer"); return false; } - for (int i = 0; i < getCurrentSize(); i++) { if (int_array[i] == value) { // Deleting the element in the tab @@ -106,7 +105,8 @@ class Entiers extends Structure { } public void compacter(int nElements) { - + // Remove the last nElements + current_size -= nElements; } } diff --git a/TP2/Image.java b/TP2/Image.java index 5385626..62a710c 100644 --- a/TP2/Image.java +++ b/TP2/Image.java @@ -46,6 +46,18 @@ class Image extends Forme implements Compactable { return rtVal; } + public boolean supprimer(int index) { + boolean rtVal = false; + if (!isEmpty()) { + for (int j = index; (j < formeNumber - 1 && index < formeNumber); j++) { + formeCollection[j] = formeCollection[j + 1]; + } + rtVal = true; + formeNumber--; + } + return rtVal; + } + public void afficher() { for (int i = 0; i < formeNumber; i++) { formeCollection[i].afficher(); @@ -53,7 +65,9 @@ class Image extends Forme implements Compactable { } public void compacter(int nElements) { - + for (int i = formeNumber - 1; (i > formeNumber - nElements && !isEmpty()); i--) { + supprimer(i); + } } } diff --git a/TP2/Liste.java b/TP2/Liste.java index 4ead214..201d875 100644 --- a/TP2/Liste.java +++ b/TP2/Liste.java @@ -119,25 +119,44 @@ public class Liste extends Structure { public void afficher() { String className = this.getClass().getSimpleName(); + int i = 0; System.out.println("---- " + className + " ----"); if (isEmpty()) { return; } else if (headNode.getNext() == null) { - System.out.println("element 0 : " + headNode.getData()); + System.out.println("element " + i + " : " + headNode.getData()); } else { - IntNode nodeCursor = headNode; - int i = 0; - while (nodeCursor.getNext() != null) { - System.out.println("element " + i + " : " + nodeCursor.getData()); - nodeCursor = nodeCursor.getNext(); + IntNode nodeCursorNext = headNode.getNext(); + System.out.println("element " + i + " : " + headNode.getData()); + i++; + while (nodeCursorNext != null) { + System.out.println("element " + i + " : " + nodeCursorNext.getData()); + nodeCursorNext = nodeCursorNext.getNext(); i++; } - System.out.println("element " + i++ + " : " + nodeCursor.getData()); } } public void compacter(int nElements) { - + // Remove the first nElements + if (isEmpty()) { + return; + } else if (nElements == 0) { + return; + } else if (headNode.getNext() == null) { + headNode = null; + } else { + // We have at least 2 nodes in the linked list + IntNode nodeCursor = headNode.getNext(); + int i = 0; + // Go to the node at the nElements + 1 place + while (i < nElements - 1 && nodeCursor.getNext() != null) { + nodeCursor = nodeCursor.getNext(); + i++; + } + // Set the nElements + 1 node as the root node + setHeadNode(nodeCursor); + } } }