X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=blobdiff_plain;f=Entiers%2FEntiers.java;h=adcc8221fc812bc2d1b181a92a9edaa21652434c;hp=fe92652e5c739ef5d7be92d6c340c4aa86138fdf;hb=306320428497edce10fbd9e0b36204c8764b7273;hpb=9f6084eae3580af1d1878008a4563ba444587f56 diff --git a/Entiers/Entiers.java b/Entiers/Entiers.java index fe92652..adcc822 100644 --- a/Entiers/Entiers.java +++ b/Entiers/Entiers.java @@ -27,21 +27,33 @@ class Entiers { } public boolean inserer(int value) { - if (is_full()) { + if (isFull()) { System.out.println("Tableau plein"); return false; } - int pos = binarySearch(0, getCurrentSize(), value); - if (pos != -1) - { - System.out.println("Valeur à inserer déjà présente"); - return false; - } - int i; - for (i = getCurrentSize() - 1; (i >= 0 && int_array[i] > value); i--) { - int_array[i + 1] = int_array[i]; + if (isEmpty()) { + int_array[0] = value; + current_size++; + return true; + } else { + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + return true; + } else if (int_array[i] > value) { + for (int j = getCurrentSize(); j > i; j--) { + int_array[j] = int_array[j - 1]; + } + int_array[i] = value; + current_size++; + return true; + } + } } - int_array[i + 1] = value; + /** + * The current value to add is > to all elements in the tab. + * So add it at the end. + */ + int_array[getCurrentSize()] = value; current_size++; return true; } @@ -58,39 +70,35 @@ class Entiers { } public boolean supprimer(int value) { - if (is_empty()) { + if (isEmpty()) { System.out.println("Aucune valeur à supprimer"); return false; } - // Find position of element to be deleted - int pos = binarySearch(0, getCurrentSize(), value); - - if (pos == -1) - { - System.out.println("Valeur à supprimer inexistante"); - return false; - } - - // Deleting element - for (int i = pos; i < getCurrentSize() - 1; i++) { - int_array[i] = int_array[i + 1]; - } - - current_size--; - return true; + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + // Deleting the element in the tab + for (int j = i; j < getCurrentSize() - 1; j++) { + int_array[j] = int_array[j + 1]; + } + current_size--; + return true; + } + } + return true; } - private boolean is_full() { + private boolean isFull() { return (getCurrentSize() >= getSize()); } - private boolean is_empty() { + private boolean isEmpty() { return (getCurrentSize() == 0); } public void afficher() { - for (int i = 0; i < getSize(); i++) { + System.out.println("----"); + for (int i = 0; i < getCurrentSize(); i++) { System.out.println("element " + i + " " + int_array[i]); } }