| 1 | |
| 2 | |
| 3 | public class Liste { |
| 4 | |
| 5 | private class IntNode { |
| 6 | private int data; |
| 7 | private IntNode next; |
| 8 | |
| 9 | IntNode(int value) { |
| 10 | setData(value); |
| 11 | setNext(null); |
| 12 | } |
| 13 | |
| 14 | IntNode(int value, IntNode nextNode) { |
| 15 | setData(value); |
| 16 | setNext(nextNode); |
| 17 | } |
| 18 | |
| 19 | private int getData() { |
| 20 | return data; |
| 21 | } |
| 22 | |
| 23 | private void setData(int value) { |
| 24 | data = value; |
| 25 | } |
| 26 | |
| 27 | private IntNode getNext() { |
| 28 | return next; |
| 29 | } |
| 30 | |
| 31 | private void setNext(IntNode nextNode) { |
| 32 | next = nextNode; |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
| 37 | private IntNode headNode; |
| 38 | private int list_counter; |
| 39 | |
| 40 | Liste() { |
| 41 | setHeadNode(null); |
| 42 | setSize(0); |
| 43 | } |
| 44 | |
| 45 | private boolean isEmpty() |
| 46 | { |
| 47 | return getHeadNode() == null; |
| 48 | } |
| 49 | |
| 50 | private int getSize() { |
| 51 | return list_counter; |
| 52 | } |
| 53 | |
| 54 | private void setSize(int size) { |
| 55 | list_counter = size; |
| 56 | } |
| 57 | |
| 58 | private void setHeadNode(IntNode node) { |
| 59 | headNode = node; |
| 60 | } |
| 61 | |
| 62 | private IntNode getHeadNode() { |
| 63 | return headNode; |
| 64 | } |
| 65 | |
| 66 | public void inserer(int value) { |
| 67 | boolean found = false; |
| 68 | if (isEmpty()) { |
| 69 | headNode = new IntNode(value); |
| 70 | list_counter++; |
| 71 | return; |
| 72 | } else if (value == headNode.getData()) { |
| 73 | found = true; |
| 74 | return; |
| 75 | } else { |
| 76 | IntNode nodeCursorNext = headNode.getNext(); |
| 77 | while (nodeCursorNext != null) { |
| 78 | if (value == nodeCursorNext.getData()) { |
| 79 | found = true; |
| 80 | break; |
| 81 | } else { |
| 82 | nodeCursorNext = nodeCursorNext.getNext(); |
| 83 | } |
| 84 | } |
| 85 | if (!found) { |
| 86 | headNode = new IntNode(value, headNode); |
| 87 | list_counter++; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public boolean supprimer(int value) { |
| 93 | boolean deleted = false; |
| 94 | if (isEmpty()) { |
| 95 | return deleted; |
| 96 | } else if (value == headNode.getData()) { |
| 97 | headNode = headNode.getNext(); |
| 98 | deleted = true; |
| 99 | list_counter--; |
| 100 | } else { |
| 101 | IntNode nodeCursor = headNode; |
| 102 | IntNode nodeCursorNext = headNode.getNext(); |
| 103 | while (nodeCursorNext != null) { |
| 104 | if (value == nodeCursorNext.getData()) { |
| 105 | nodeCursor.setNext(nodeCursorNext.getNext()); |
| 106 | deleted = true; |
| 107 | list_counter--; |
| 108 | break; |
| 109 | } else { |
| 110 | nodeCursor = nodeCursorNext; |
| 111 | nodeCursorNext = nodeCursorNext.getNext(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return deleted; |
| 116 | } |
| 117 | |
| 118 | public void afficher() { |
| 119 | if (isEmpty()) { |
| 120 | System.out.println("Liste vide"); |
| 121 | } else if (headNode.getNext() == null) { |
| 122 | System.out.println("Valeur du noeud 0 : " + headNode.getData()); |
| 123 | } else { |
| 124 | IntNode nodeCursor = headNode; |
| 125 | int i = 0; |
| 126 | while (nodeCursor.getNext() != null) { |
| 127 | System.out.println("Valeur du noeud " + i + " : " + nodeCursor.getData()); |
| 128 | nodeCursor = nodeCursor.getNext(); |
| 129 | i++; |
| 130 | } |
| 131 | System.out.println("Valeur du noeud " + i++ + " : " + nodeCursor.getData()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public static void main(String[] args) { |
| 136 | Liste LinkedList = new Liste(); |
| 137 | |
| 138 | LinkedList.inserer(2); |
| 139 | LinkedList.inserer(1); |
| 140 | LinkedList.inserer(4); |
| 141 | LinkedList.afficher(); |
| 142 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 143 | |
| 144 | LinkedList.inserer(2); |
| 145 | LinkedList.inserer(10); |
| 146 | LinkedList.inserer(0); |
| 147 | LinkedList.afficher(); |
| 148 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 149 | |
| 150 | LinkedList.supprimer(4); |
| 151 | LinkedList.afficher(); |
| 152 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 153 | LinkedList.supprimer(0); |
| 154 | LinkedList.afficher(); |
| 155 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 156 | LinkedList.supprimer(0); |
| 157 | LinkedList.afficher(); |
| 158 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 159 | LinkedList.supprimer(10); |
| 160 | LinkedList.afficher(); |
| 161 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 162 | } |
| 163 | |
| 164 | } |