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