From 684b6099df9f03c36286252a07272358a98c9091 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Feb 2018 10:04:57 +0100 Subject: [PATCH] Fix the Liste code to not be a linked list of ordered values ... MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- Listes/Liste.java | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Listes/Liste.java b/Listes/Liste.java index 3b03590..6dd1c5f 100644 --- a/Listes/Liste.java +++ b/Listes/Liste.java @@ -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++; } } -- 2.34.1