Commit | Line | Data |
---|---|---|
f4ad9443 JB |
1 | |
2 | ||
3 | public class Liste extends Structure { | |
4d9f4a58 | 4 | private Node<Integer> headNode; |
f4ad9443 JB |
5 | private int list_counter; |
6 | ||
7 | Liste() { | |
8 | setHeadNode(null); | |
9 | setSize(0); | |
10 | } | |
11 | ||
12 | private boolean isEmpty() | |
13 | { | |
14 | return getHeadNode() == null; | |
15 | } | |
16 | ||
17 | public int getSize() { | |
18 | return list_counter; | |
19 | } | |
20 | ||
21 | public void setSize(int size) { | |
22 | list_counter = size; | |
23 | } | |
24 | ||
4d9f4a58 | 25 | public void setHeadNode(Node<Integer> node) { |
f4ad9443 JB |
26 | headNode = node; |
27 | } | |
28 | ||
4d9f4a58 | 29 | public Node<Integer> getHeadNode() { |
f4ad9443 JB |
30 | return headNode; |
31 | } | |
32 | ||
33 | public boolean inserer(int value) { | |
34 | boolean found = false; | |
35 | if (isEmpty()) { | |
4d9f4a58 | 36 | headNode = new Node<Integer>(value); |
f4ad9443 JB |
37 | list_counter++; |
38 | return true; | |
39 | } else if (value == headNode.getData()) { | |
40 | found = true; | |
41 | return true; | |
42 | } else { | |
4d9f4a58 | 43 | Node<Integer> nodeCursorNext = headNode.getNext(); |
f4ad9443 JB |
44 | while (nodeCursorNext != null) { |
45 | if (value == nodeCursorNext.getData()) { | |
46 | found = true; | |
47 | break; | |
48 | } else { | |
49 | nodeCursorNext = nodeCursorNext.getNext(); | |
50 | } | |
51 | } | |
52 | if (!found) { | |
4d9f4a58 | 53 | headNode = new Node<Integer>(value, headNode); |
f4ad9443 JB |
54 | list_counter++; |
55 | } | |
56 | // Insertion in a linked list can't fail | |
57 | return true; | |
58 | } | |
59 | } | |
60 | ||
61 | public boolean supprimer(int value) { | |
62 | boolean deleted = false; | |
63 | if (isEmpty()) { | |
64 | return deleted; | |
65 | } else if (value == headNode.getData()) { | |
66 | headNode = headNode.getNext(); | |
67 | deleted = true; | |
68 | list_counter--; | |
69 | } else { | |
4d9f4a58 JB |
70 | Node<Integer> nodeCursor = headNode; |
71 | Node<Integer> nodeCursorNext = headNode.getNext(); | |
f4ad9443 JB |
72 | while (nodeCursorNext != null) { |
73 | if (value == nodeCursorNext.getData()) { | |
74 | nodeCursor.setNext(nodeCursorNext.getNext()); | |
75 | deleted = true; | |
76 | list_counter--; | |
77 | break; | |
78 | } else { | |
79 | nodeCursor = nodeCursorNext; | |
80 | nodeCursorNext = nodeCursorNext.getNext(); | |
81 | } | |
82 | } | |
83 | } | |
84 | return deleted; | |
85 | } | |
86 | ||
87 | public void afficher() { | |
5731ae5f JB |
88 | String className = this.getClass().getSimpleName(); |
89 | int i = 0; | |
90 | System.out.println("---- " + className + " ----"); | |
f4ad9443 | 91 | if (isEmpty()) { |
c7c510eb | 92 | return; |
f4ad9443 | 93 | } else if (headNode.getNext() == null) { |
5731ae5f | 94 | System.out.println("element " + i + " : " + headNode.getData()); |
f4ad9443 | 95 | } else { |
4d9f4a58 | 96 | Node<Integer> nodeCursorNext = headNode.getNext(); |
5731ae5f JB |
97 | System.out.println("element " + i + " : " + headNode.getData()); |
98 | i++; | |
99 | while (nodeCursorNext != null) { | |
100 | System.out.println("element " + i + " : " + nodeCursorNext.getData()); | |
101 | nodeCursorNext = nodeCursorNext.getNext(); | |
f4ad9443 JB |
102 | i++; |
103 | } | |
f4ad9443 JB |
104 | } |
105 | } | |
106 | ||
107 | } |