X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=blobdiff_plain;f=Listes%2FListe.java;h=d174ad575fbf25047f11449238124e4ec769502c;hp=6a62aea93ac02feeaba37c34db1421ae8a06750e;hb=d7dd93e3ad431005e39992df5a36bfa463641560;hpb=f1af410600beae0521ecd4abe9439a324ab2250f diff --git a/Listes/Liste.java b/Listes/Liste.java index 6a62aea..d174ad5 100644 --- a/Listes/Liste.java +++ b/Listes/Liste.java @@ -1,49 +1,50 @@ -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; Liste() { - setheadNode(null); + setHeadNode(null); setSize(0); } private boolean isEmpty() { - return headNode == null; + return getHeadNode() == null; } private int getSize() { @@ -54,43 +55,35 @@ class Liste { list_counter = size; } - private void setheadNode(IntNode node) { + private void setHeadNode(IntNode node) { headNode = node; } - private IntNode getheadNode() { + private IntNode getHeadNode() { return headNode; } 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++; } }