Commit | Line | Data |
---|---|---|
d3d2d15c | 1 | |
bc48a285 JB |
2 | /** |
3 | * | |
4 | */ | |
9b4b86b9 | 5 | public class Pile { |
fa0086ca | 6 | private int[] int_array; |
d3d2d15c | 7 | private int array_size; |
c3a259ee | 8 | private int head_index; |
d3d2d15c | 9 | |
b5d597d8 JB |
10 | /** |
11 | * [Pile description] | |
12 | * @param int size [description] | |
13 | */ | |
14 | Pile(int size) { | |
15 | int_array = new int[size]; | |
16 | setSize(size); | |
17 | setHeadIndex(0); | |
18 | } | |
19 | ||
bc48a285 | 20 | /** |
8122cb54 JB |
21 | * set the size of the internal array |
22 | * @param int size the size of the array | |
bc48a285 | 23 | */ |
d3d2d15c JB |
24 | private void setSize(int size) { |
25 | array_size = size; | |
26 | } | |
27 | ||
bc48a285 | 28 | /** |
8122cb54 JB |
29 | * get the size of the internal array |
30 | * @return the integer size of the internal array | |
bc48a285 | 31 | */ |
d3d2d15c JB |
32 | private int getSize() { |
33 | return array_size; | |
34 | } | |
35 | ||
bc48a285 | 36 | /** |
8122cb54 JB |
37 | * set the stack head index |
38 | * @param int index the stack head index | |
bc48a285 | 39 | */ |
d3d2d15c | 40 | private void setHeadIndex(int index) { |
c3a259ee | 41 | head_index = index; |
d3d2d15c JB |
42 | } |
43 | ||
bc48a285 | 44 | /** |
8122cb54 JB |
45 | * get the stack head current index |
46 | * @return the integer stack head index | |
bc48a285 | 47 | */ |
d3d2d15c | 48 | private int getHeadIndex() { |
c3a259ee | 49 | return head_index; |
d3d2d15c JB |
50 | } |
51 | ||
bc48a285 JB |
52 | /** |
53 | * [empiler description] | |
54 | * @param int value [description] | |
55 | */ | |
d3d2d15c JB |
56 | public void empiler(int value) { |
57 | if (!plein()) { | |
c3a259ee JB |
58 | int_array[head_index] = value; |
59 | head_index++; | |
d3d2d15c JB |
60 | } else { |
61 | System.out.println("La pile est pleine"); | |
62 | } | |
63 | } | |
64 | ||
bc48a285 JB |
65 | /** |
66 | * [depiler description] | |
67 | * @return [description] | |
68 | */ | |
d3d2d15c JB |
69 | public int depiler() { |
70 | if (!vide()) { | |
c3a259ee JB |
71 | head_index--; |
72 | return int_array[head_index]; | |
d3d2d15c JB |
73 | } else { |
74 | return -1; | |
75 | } | |
76 | } | |
77 | ||
bc48a285 JB |
78 | /** |
79 | * [plein description] | |
80 | * @return [description] | |
81 | */ | |
82 | private boolean plein() { | |
d3d2d15c JB |
83 | return (getHeadIndex() >= getSize()); |
84 | } | |
85 | ||
bc48a285 JB |
86 | /** |
87 | * [vide description] | |
88 | * @return [description] | |
89 | */ | |
90 | private boolean vide() { | |
d3d2d15c JB |
91 | return (getHeadIndex() == 0); |
92 | } | |
93 | ||
bc48a285 JB |
94 | /** |
95 | * [afficher description] | |
96 | */ | |
d3d2d15c | 97 | public void afficher() { |
30632042 | 98 | for (int i = 0; i < getHeadIndex(); i++) { |
d3d2d15c JB |
99 | System.out.println("element " + i + " " + int_array[i]); |
100 | } | |
101 | } | |
102 | ||
bc48a285 JB |
103 | /** |
104 | * The main() function | |
27869e5b | 105 | * @param String[] args main() function arguments array |
bc48a285 | 106 | */ |
d3d2d15c | 107 | public static void main(String[] args) { |
b48ca56a JB |
108 | Pile stack = new Pile(5); |
109 | ||
110 | stack.empiler(3); | |
111 | stack.empiler(5); | |
112 | stack.empiler(4); | |
113 | stack.empiler(7); | |
114 | stack.empiler(8); | |
115 | ||
116 | stack.afficher(); | |
117 | ||
bc48a285 JB |
118 | System.out.println("Stack index " + stack.getHeadIndex()); |
119 | System.out.println("Stack head value " + stack.depiler()); | |
120 | System.out.println("Stack index " + stack.getHeadIndex()); | |
121 | System.out.println("Stack head value " + stack.depiler()); | |
122 | System.out.println("Stack index " + stack.getHeadIndex()); | |
123 | System.out.println("Stack head value " + stack.depiler()); | |
124 | System.out.println("Stack index " + stack.getHeadIndex()); | |
125 | System.out.println("Stack head value " + stack.depiler()); | |
126 | System.out.println("Stack index " + stack.getHeadIndex()); | |
127 | System.out.println("Stack head value " + stack.depiler()); | |
128 | System.out.println("Stack index " + stack.getHeadIndex()); | |
129 | System.out.println("Stack head value " + stack.depiler()); | |
130 | System.out.println("Stack index " + stack.getHeadIndex()); | |
b48ca56a JB |
131 | |
132 | stack.afficher(); | |
d3d2d15c JB |
133 | } |
134 | } |