From: Jérôme Benoit Date: Thu, 8 Feb 2018 21:18:23 +0000 (+0100) Subject: Add ordered simply linked list exercice code. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f1af410600beae0521ecd4abe9439a324ab2250f;p=TP_POO.git Add ordered simply linked list exercice code. Signed-off-by: Jérôme Benoit --- diff --git a/Listes/Liste.java b/Listes/Liste.java index d290a9c..6a62aea 100644 --- a/Listes/Liste.java +++ b/Listes/Liste.java @@ -1,23 +1,171 @@ -class Liste { + +class IntNode { private int data; + private IntNode next; + + IntNode(int value) { + setData(value); + setNext(null); + } + + IntNode(int value, IntNode nextNode) { + setData(value); + setNext(nextNode); + } + + public int getData() { + return data; + } + + public void setData(int value) { + data = value; + } + + public IntNode getNext() { + return next; + } + + public void setNext(IntNode nextNode) { + next = nextNode; + } + +} + +class Liste { + private IntNode headNode; private int list_counter; - private Liste next; Liste() { + setheadNode(null); + setSize(0); + } + private boolean isEmpty() + { + return headNode == null; } private int getSize() { return list_counter; } - public boolean inserer(int value) { + private void setSize(int size) { + list_counter = size; + } + + private void setheadNode(IntNode node) { + headNode = node; + } + private IntNode getheadNode() { + return headNode; + } + + public void inserer(int value) { + boolean inserted = false; + if (isEmpty()) { + headNode = new IntNode(value); + list_counter++; + return; + } else if (value < headNode.getData()) { + headNode = new IntNode(value, headNode); + list_counter++; + 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++; + break; + } else { + nodeCursor = nodeCursorNext; + nodeCursorNext = nodeCursorNext.getNext(); + } + } + if (!inserted) { + nodeCursor.setNext(new IntNode(value)); + list_counter++; + } + } } public boolean supprimer(int value) { + boolean deleted = false; + if (isEmpty()) { + return deleted; + } else if (value == headNode.getData()) { + headNode = headNode.getNext(); + deleted = true; + list_counter--; + } else { + IntNode nodeCursor = headNode; + IntNode nodeCursorNext = headNode.getNext(); + while (nodeCursorNext != null) { + if (value == nodeCursorNext.getData()) { + nodeCursor.setNext(nodeCursorNext.getNext()); + deleted = true; + list_counter--; + break; + } else { + nodeCursor = nodeCursorNext; + nodeCursorNext = nodeCursorNext.getNext(); + } + } + } + return deleted; + } + + public void afficher() { + if (isEmpty()) { + System.out.println("Liste vide"); + } else if (headNode.getNext() == null) { + System.out.println("Valeur du noeud 0 : " + headNode.getData()); + } else { + IntNode nodeCursor = headNode; + int i = 0; + while (nodeCursor.getNext() != null) { + System.out.println("Valeur du noeud " + i + " : " + nodeCursor.getData()); + nodeCursor = nodeCursor.getNext(); + i++; + } + System.out.println("Valeur du noeud " + i++ + " : " + nodeCursor.getData()); + } + } + + public static void main(String[] args) { + Liste LinkedList = new Liste(); + + LinkedList.inserer(2); + LinkedList.inserer(1); + LinkedList.inserer(4); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); + + LinkedList.inserer(2); + LinkedList.inserer(10); + LinkedList.inserer(0); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); + LinkedList.supprimer(4); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); + LinkedList.supprimer(0); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); + LinkedList.supprimer(0); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); + LinkedList.supprimer(10); + LinkedList.afficher(); + System.out.println("Taille de la liste : " + LinkedList.getSize()); } }