X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=blobdiff_plain;f=Structure%2FListe.java;fp=Structure%2FListe.java;h=d6bd9633bc15ac7382312afdf9b2d51e02ae849d;hp=0000000000000000000000000000000000000000;hb=f4ad9443f6fc46958bbfdacdf13278ff2a96c072;hpb=009c5b1e63be46a50ba2c551c96369f461f44a93 diff --git a/Structure/Liste.java b/Structure/Liste.java new file mode 100644 index 0000000..d6bd963 --- /dev/null +++ b/Structure/Liste.java @@ -0,0 +1,137 @@ + + +public class Liste extends Structure { + + private 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; + } + + } + + private IntNode headNode; + private int list_counter; + + Liste() { + setHeadNode(null); + setSize(0); + } + + private boolean isEmpty() + { + return getHeadNode() == null; + } + + public int getSize() { + return list_counter; + } + + public void setSize(int size) { + list_counter = size; + } + + public void setHeadNode(IntNode node) { + headNode = node; + } + + public IntNode getHeadNode() { + return headNode; + } + + public boolean inserer(int value) { + boolean found = false; + if (isEmpty()) { + headNode = new IntNode(value); + list_counter++; + return true; + } else if (value == headNode.getData()) { + found = true; + return true; + } else { + IntNode nodeCursorNext = headNode.getNext(); + while (nodeCursorNext != null) { + if (value == nodeCursorNext.getData()) { + found = true; + break; + } else { + nodeCursorNext = nodeCursorNext.getNext(); + } + } + if (!found) { + headNode = new IntNode(value, headNode); + list_counter++; + } + // Insertion in a linked list can't fail + return true; + } + } + + 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()); + } + } + +}