Commit | Line | Data |
---|---|---|
d3d2d15c | 1 | |
bc48a285 JB |
2 | /** |
3 | * | |
4 | */ | |
9b4b86b9 | 5 | public class Pile { |
d3d2d15c JB |
6 | private int int_array[]; |
7 | private int array_size; | |
b48ca56a | 8 | private int stack_head_index; |
d3d2d15c | 9 | |
bc48a285 JB |
10 | /** |
11 | * [setSize description] | |
12 | * @param int size [description] | |
13 | */ | |
d3d2d15c JB |
14 | private void setSize(int size) { |
15 | array_size = size; | |
16 | } | |
17 | ||
bc48a285 JB |
18 | /** |
19 | * [getSize description] | |
20 | * @return [description] | |
21 | */ | |
d3d2d15c JB |
22 | private int getSize() { |
23 | return array_size; | |
24 | } | |
25 | ||
bc48a285 JB |
26 | /** |
27 | * [setHeadIndex description] | |
28 | * @param int index [description] | |
29 | */ | |
d3d2d15c | 30 | private void setHeadIndex(int index) { |
b48ca56a | 31 | stack_head_index = index; |
d3d2d15c JB |
32 | } |
33 | ||
bc48a285 JB |
34 | /** |
35 | * [getHeadIndex description] | |
36 | * @return [description] | |
37 | */ | |
d3d2d15c | 38 | private int getHeadIndex() { |
b48ca56a | 39 | return stack_head_index; |
d3d2d15c JB |
40 | } |
41 | ||
bc48a285 JB |
42 | /** |
43 | * [Pile description] | |
44 | * @param int size [description] | |
45 | */ | |
d3d2d15c JB |
46 | Pile(int size) { |
47 | int_array = new int[size]; | |
48 | setSize(size); | |
49 | setHeadIndex(0); | |
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()) { | |
b48ca56a JB |
58 | int_array[stack_head_index] = value; |
59 | stack_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()) { | |
b48ca56a JB |
71 | stack_head_index--; |
72 | return int_array[stack_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 JB |
97 | public void afficher() { |
98 | for (int i = 0; i < getSize(); i++) { | |
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 | } |