Entiers, Pile: Print only the values in the stack.
[TP_POO.git] / Piles / Pile.java
1
2 /**
3 *
4 */
5 public class Pile {
6 private int int_array[];
7 private int array_size;
8 private int stack_head_index;
9
10 /**
11 * set the size of the internal array
12 * @param int size the size of the array
13 */
14 private void setSize(int size) {
15 array_size = size;
16 }
17
18 /**
19 * get the size of the internal array
20 * @return the integer size of the internal array
21 */
22 private int getSize() {
23 return array_size;
24 }
25
26 /**
27 * set the stack head index
28 * @param int index the stack head index
29 */
30 private void setHeadIndex(int index) {
31 stack_head_index = index;
32 }
33
34 /**
35 * get the stack head current index
36 * @return the integer stack head index
37 */
38 private int getHeadIndex() {
39 return stack_head_index;
40 }
41
42 /**
43 * [Pile description]
44 * @param int size [description]
45 */
46 Pile(int size) {
47 int_array = new int[size];
48 setSize(size);
49 setHeadIndex(0);
50 }
51
52 /**
53 * [empiler description]
54 * @param int value [description]
55 */
56 public void empiler(int value) {
57 if (!plein()) {
58 int_array[stack_head_index] = value;
59 stack_head_index++;
60 } else {
61 System.out.println("La pile est pleine");
62 }
63 }
64
65 /**
66 * [depiler description]
67 * @return [description]
68 */
69 public int depiler() {
70 if (!vide()) {
71 stack_head_index--;
72 return int_array[stack_head_index];
73 } else {
74 return -1;
75 }
76 }
77
78 /**
79 * [plein description]
80 * @return [description]
81 */
82 private boolean plein() {
83 return (getHeadIndex() >= getSize());
84 }
85
86 /**
87 * [vide description]
88 * @return [description]
89 */
90 private boolean vide() {
91 return (getHeadIndex() == 0);
92 }
93
94 /**
95 * [afficher description]
96 */
97 public void afficher() {
98 for (int i = 0; i < getHeadIndex(); i++) {
99 System.out.println("element " + i + " " + int_array[i]);
100 }
101 }
102
103 /**
104 * The main() function
105 * @param String[] args main() function arguments array
106 */
107 public static void main(String[] args) {
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
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());
131
132 stack.afficher();
133 }
134 }