Include everything in the same class.
[TP_POO.git] / Listes / Liste.java
index 3b03590d82bdc4c6334f4888d79547e0dcce110a..711b9830aab8c81dd8c72060ca5cb06a1700e02d 100644 (file)
@@ -1,38 +1,39 @@
 
 
-class IntNode {
-    private int data;
-    private IntNode next;
+public class Liste {
 
-    IntNode(int value) {
-        setData(value);
-        setNext(null);
-    }
+    private class IntNode {
+        private int data;
+        private IntNode next;
 
-    IntNode(int value, IntNode nextNode) {
-        setData(value);
-        setNext(nextNode);
-    }
+        IntNode(int value) {
+            setData(value);
+            setNext(null);
+        }
 
-    public int getData() {
-        return data;
-    }
+        IntNode(int value, IntNode nextNode) {
+            setData(value);
+            setNext(nextNode);
+        }
 
-    public void setData(int value) {
-        data = value;
-    }
+        private int getData() {
+            return data;
+        }
 
-    public IntNode getNext() {
-        return next;
-    }
+        private void setData(int value) {
+            data = value;
+        }
 
-    public void setNext(IntNode nextNode) {
-        next = nextNode;
-    }
+        private IntNode getNext() {
+            return next;
+        }
 
-}
+        private void setNext(IntNode nextNode) {
+            next = nextNode;
+        }
+
+    }
 
-class Liste {
     private IntNode headNode;
     private int list_counter;
 
@@ -63,34 +64,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++;
             }
         }