Commit | Line | Data |
---|---|---|
d3d2d15c JB |
1 | |
2 | class Pile { | |
3 | private int int_array[]; | |
4 | private int array_size; | |
b48ca56a | 5 | private int stack_head_index; |
d3d2d15c JB |
6 | |
7 | private void setSize(int size) { | |
8 | array_size = size; | |
9 | } | |
10 | ||
11 | private int getSize() { | |
12 | return array_size; | |
13 | } | |
14 | ||
15 | private void setHeadIndex(int index) { | |
b48ca56a | 16 | stack_head_index = index; |
d3d2d15c JB |
17 | } |
18 | ||
19 | private int getHeadIndex() { | |
b48ca56a | 20 | return stack_head_index; |
d3d2d15c JB |
21 | } |
22 | ||
23 | Pile(int size) { | |
24 | int_array = new int[size]; | |
25 | setSize(size); | |
26 | setHeadIndex(0); | |
27 | } | |
28 | ||
29 | public void empiler(int value) { | |
30 | if (!plein()) { | |
b48ca56a JB |
31 | int_array[stack_head_index] = value; |
32 | stack_head_index++; | |
d3d2d15c JB |
33 | } else { |
34 | System.out.println("La pile est pleine"); | |
35 | } | |
36 | } | |
37 | ||
38 | public int depiler() { | |
39 | if (!vide()) { | |
b48ca56a JB |
40 | stack_head_index--; |
41 | return int_array[stack_head_index]; | |
d3d2d15c JB |
42 | } else { |
43 | return -1; | |
44 | } | |
45 | } | |
46 | ||
47 | public boolean plein() { | |
48 | return (getHeadIndex() >= getSize()); | |
49 | } | |
50 | ||
51 | public boolean vide() { | |
52 | return (getHeadIndex() == 0); | |
53 | } | |
54 | ||
55 | public void afficher() { | |
56 | for (int i = 0; i < getSize(); i++) { | |
57 | System.out.println("element " + i + " " + int_array[i]); | |
58 | } | |
59 | } | |
60 | ||
61 | public static void main(String[] args) { | |
b48ca56a JB |
62 | Pile stack = new Pile(5); |
63 | ||
64 | stack.empiler(3); | |
65 | stack.empiler(5); | |
66 | stack.empiler(4); | |
67 | stack.empiler(7); | |
68 | stack.empiler(8); | |
69 | ||
70 | stack.afficher(); | |
71 | ||
72 | System.out.println("stack index " + stack.getHeadIndex()); | |
73 | System.out.println("stack head value " + stack.depiler()); | |
74 | System.out.println("stack index " + stack.getHeadIndex()); | |
75 | System.out.println("stack head value " + stack.depiler()); | |
76 | System.out.println("stack index " + stack.getHeadIndex()); | |
77 | System.out.println("stack head value " + stack.depiler()); | |
78 | System.out.println("stack index " + stack.getHeadIndex()); | |
79 | System.out.println("stack head value " + stack.depiler()); | |
80 | System.out.println("stack index " + stack.getHeadIndex()); | |
81 | System.out.println("stack head value " + stack.depiler()); | |
82 | System.out.println("stack index " + stack.getHeadIndex()); | |
83 | System.out.println("stack head value " + stack.depiler()); | |
84 | System.out.println("stack index " + stack.getHeadIndex()); | |
85 | ||
86 | stack.afficher(); | |
d3d2d15c JB |
87 | } |
88 | } |