X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=blobdiff_plain;f=TP2%2FListe.java;h=6928b00737e0656548f61f6b49cc791812f80b77;hp=201d8750af17c7f0110e8bc852811ed446fea0c7;hb=78c725c5e287068ee24d4abb950de2ab9520d76a;hpb=5731ae5f40887e5a299eeca6d9047c06cd17ab41 diff --git a/TP2/Liste.java b/TP2/Liste.java index 201d875..6928b00 100644 --- a/TP2/Liste.java +++ b/TP2/Liste.java @@ -1,5 +1,8 @@ - +@ClassPreamble ( + author = "Jérôme Benoit", + date = "05/12/2011" +) public class Liste extends Structure { private class IntNode { @@ -139,23 +142,22 @@ public class Liste extends Structure { public void compacter(int nElements) { // Remove the first nElements - if (isEmpty()) { - return; - } else if (nElements == 0) { + if (isEmpty() || 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()); } }