Fix the Liste code to not be a linked list of ordered values ...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Feb 2018 09:04:57 +0000 (10:04 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Feb 2018 09:04:57 +0000 (10:04 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
Listes/Liste.java

index 3b03590d82bdc4c6334f4888d79547e0dcce110a..6dd1c5fb58abc7194f905d2bda87f75b385b9139 100644 (file)
@@ -63,34 +63,26 @@ class Liste {
     }
 
     public void inserer(int value) {
-        boolean inserted = false;
+        boolean found = false;
         if (isEmpty()) {
             headNode = new IntNode(value);
             list_counter++;
             return;
-        } else if (value < headNode.getData()) {
-            headNode = new IntNode(value, headNode);
-            list_counter++;
+        } else if (value == headNode.getData()) {
+            found = true;
             return;
         } else {
-            IntNode nodeCursor = headNode;
             IntNode nodeCursorNext = headNode.getNext();
             while (nodeCursorNext != null) {
-                if (value == nodeCursor.getData() || value == nodeCursorNext.getData()) {
-                    inserted = true;
-                    break;
-                } else if (value > nodeCursor.getData() && value < nodeCursorNext.getData()) {
-                    nodeCursor.setNext(new IntNode(value, nodeCursorNext));
-                    inserted = true;
-                    list_counter++;
+                if (value == nodeCursorNext.getData()) {
+                    found = true;
                     break;
                 } else {
-                    nodeCursor = nodeCursorNext;
                     nodeCursorNext = nodeCursorNext.getNext();
                 }
             }
-            if (!inserted) {
-                nodeCursor.setNext(new IntNode(value));
+            if (!found) {
+                headNode = new IntNode(value, headNode);
                 list_counter++;
             }
         }