Commit | Line | Data |
---|---|---|
c05c2289 | 1 | |
2b29b63e JB |
2 | @ClassPreamble ( |
3 | author = "Jérôme Benoit", | |
4 | date = "05/12/2011" | |
5 | ) | |
c05c2289 | 6 | public class Liste extends Structure { |
bb6a4f9a | 7 | private Node<Integer> headNode; |
c05c2289 JB |
8 | private int list_counter; |
9 | ||
10 | Liste() { | |
11 | setHeadNode(null); | |
12 | setSize(0); | |
13 | } | |
14 | ||
15 | private boolean isEmpty() | |
16 | { | |
17 | return getHeadNode() == null; | |
18 | } | |
19 | ||
20 | public int getSize() { | |
21 | return list_counter; | |
22 | } | |
23 | ||
24 | public void setSize(int size) { | |
25 | list_counter = size; | |
26 | } | |
27 | ||
bb6a4f9a | 28 | public void setHeadNode(Node<Integer> node) { |
c05c2289 JB |
29 | headNode = node; |
30 | } | |
31 | ||
bb6a4f9a | 32 | public Node<Integer> getHeadNode() { |
c05c2289 JB |
33 | return headNode; |
34 | } | |
35 | ||
36 | public boolean inserer(int value) { | |
37 | boolean found = false; | |
38 | if (isEmpty()) { | |
bb6a4f9a | 39 | headNode = new Node<Integer>(value); |
c05c2289 JB |
40 | list_counter++; |
41 | return true; | |
42 | } else if (value == headNode.getData()) { | |
43 | found = true; | |
44 | return true; | |
45 | } else { | |
bb6a4f9a | 46 | Node<Integer> nodeCursorNext = headNode.getNext(); |
c05c2289 JB |
47 | while (nodeCursorNext != null) { |
48 | if (value == nodeCursorNext.getData()) { | |
49 | found = true; | |
50 | break; | |
51 | } else { | |
52 | nodeCursorNext = nodeCursorNext.getNext(); | |
53 | } | |
54 | } | |
55 | if (!found) { | |
bb6a4f9a | 56 | headNode = new Node<Integer>(value, headNode); |
c05c2289 JB |
57 | list_counter++; |
58 | } | |
59 | // Insertion in a linked list can't fail | |
60 | return true; | |
61 | } | |
62 | } | |
63 | ||
64 | public boolean supprimer(int value) { | |
65 | boolean deleted = false; | |
66 | if (isEmpty()) { | |
67 | return deleted; | |
68 | } else if (value == headNode.getData()) { | |
69 | headNode = headNode.getNext(); | |
70 | deleted = true; | |
71 | list_counter--; | |
72 | } else { | |
bb6a4f9a JB |
73 | Node<Integer> nodeCursor = headNode; |
74 | Node<Integer> nodeCursorNext = headNode.getNext(); | |
c05c2289 JB |
75 | while (nodeCursorNext != null) { |
76 | if (value == nodeCursorNext.getData()) { | |
77 | nodeCursor.setNext(nodeCursorNext.getNext()); | |
78 | deleted = true; | |
79 | list_counter--; | |
80 | break; | |
81 | } else { | |
82 | nodeCursor = nodeCursorNext; | |
83 | nodeCursorNext = nodeCursorNext.getNext(); | |
84 | } | |
85 | } | |
86 | } | |
87 | return deleted; | |
88 | } | |
89 | ||
90 | public void afficher() { | |
91 | String className = this.getClass().getSimpleName(); | |
92 | int i = 0; | |
93 | System.out.println("---- " + className + " ----"); | |
94 | if (isEmpty()) { | |
95 | return; | |
96 | } else if (headNode.getNext() == null) { | |
97 | System.out.println("element " + i + " : " + headNode.getData()); | |
98 | } else { | |
bb6a4f9a | 99 | Node<Integer> nodeCursorNext = headNode.getNext(); |
c05c2289 JB |
100 | System.out.println("element " + i + " : " + headNode.getData()); |
101 | i++; | |
102 | while (nodeCursorNext != null) { | |
103 | System.out.println("element " + i + " : " + nodeCursorNext.getData()); | |
104 | nodeCursorNext = nodeCursorNext.getNext(); | |
105 | i++; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | public void compacter(int nElements) { | |
111 | // Remove the first nElements | |
112 | if (isEmpty() || nElements == 0) { | |
113 | return; | |
114 | } else if (headNode != null && headNode.getNext() == null) { | |
115 | headNode = null; | |
116 | } else { | |
117 | // We have at least 2 nodes in the linked list | |
bb6a4f9a | 118 | Node<Integer> nodeCursor = headNode; |
c05c2289 JB |
119 | int i = 0; |
120 | // Go to the node at the nElements place | |
121 | while (i < nElements - 1 && nodeCursor.getNext() != null && nElements > 1) { | |
122 | nodeCursor = nodeCursor.getNext(); | |
123 | i++; | |
124 | } | |
125 | // Set the nElements + 1 node as the root node | |
126 | // It might be null | |
127 | setHeadNode(nodeCursor.getNext()); | |
128 | } | |
129 | } | |
130 | ||
131 | } |