Fix the Compactable implementation in Entiers and Listes classes.
[TP_POO.git] / TP2 / Liste.java
index 201d8750af17c7f0110e8bc852811ed446fea0c7..ceab4993c1f80e4da6edc998b99c0d10ffe1a6dc 100644 (file)
@@ -143,19 +143,20 @@ public class Liste extends Structure {
             return;
         } else if (nElements == 0) {
             return;
-        } else if (headNode.getNext() == null) {
+        } else if (headNode != null && headNode.getNext() == null) {
             headNode = null;
         } else {
             // We have at least 2 nodes in the linked list
-            IntNode nodeCursor = headNode.getNext();
+            IntNode nodeCursor = headNode;
             int i = 0;
-            // Go to the node at the nElements + 1 place
-            while (i < nElements - 1 && nodeCursor.getNext() != null) {
+            // Go to the node at the nElements place
+            while (i < nElements -1 && nodeCursor.getNext() != null && nElements > 1) {
                 nodeCursor = nodeCursor.getNext();
                 i++;
             }
             // Set the nElements + 1 node as the root node
-            setHeadNode(nodeCursor);
+            // It might be null
+            setHeadNode(nodeCursor.getNext());
         }
     }