| 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 | String className = this.getClass().getSimpleName(); |
| 120 | int i = 0; |
| 121 | System.out.println("---- " + className + " ----"); |
| 122 | if (isEmpty()) { |
| 123 | return; |
| 124 | } else if (headNode.getNext() == null) { |
| 125 | System.out.println("element " + i + " : " + headNode.getData()); |
| 126 | } else { |
| 127 | IntNode nodeCursorNext = headNode.getNext(); |
| 128 | System.out.println("element " + i + " : " + headNode.getData()); |
| 129 | i++; |
| 130 | while (nodeCursorNext != null) { |
| 131 | System.out.println("element " + i + " : " + nodeCursorNext.getData()); |
| 132 | nodeCursorNext = nodeCursorNext.getNext(); |
| 133 | i++; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public static void main(String[] args) { |
| 139 | Liste LinkedList = new Liste(); |
| 140 | |
| 141 | LinkedList.inserer(2); |
| 142 | LinkedList.inserer(1); |
| 143 | LinkedList.inserer(4); |
| 144 | LinkedList.afficher(); |
| 145 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 146 | |
| 147 | LinkedList.inserer(2); |
| 148 | LinkedList.inserer(10); |
| 149 | LinkedList.inserer(0); |
| 150 | LinkedList.afficher(); |
| 151 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 152 | |
| 153 | LinkedList.supprimer(4); |
| 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(0); |
| 160 | LinkedList.afficher(); |
| 161 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 162 | LinkedList.supprimer(10); |
| 163 | LinkedList.afficher(); |
| 164 | System.out.println("Taille de la liste : " + LinkedList.getSize()); |
| 165 | } |
| 166 | |
| 167 | } |