e7d4b4deac667aa2c934567be2bd81e94d840c3f
[TP_POO.git] / Pile.java
1
2 class Pile {
3 private int int_array[];
4 private int array_size;
5 private int heap_head_index;
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) {
16 heap_head_index = index;
17 }
18
19 private int getHeadIndex() {
20 return heap_head_index;
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()) {
31 int_array[heap_head_index] = value;
32 heap_head_index++;
33 } else {
34 System.out.println("La pile est pleine");
35 }
36 }
37
38 public int depiler() {
39 if (!vide()) {
40 heap_head_index--;
41 return int_array[heap_head_index];
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) {
62 Pile heap = new Pile(5);
63
64 heap.empiler(3);
65 heap.empiler(5);
66 heap.empiler(4);
67 heap.empiler(7);
68 heap.empiler(8);
69
70 heap.afficher();
71
72 System.out.println("Heap index " + heap.getHeadIndex());
73 System.out.println("Heap head value " + heap.depiler());
74 System.out.println("Heap index " + heap.getHeadIndex());
75 System.out.println("Heap head value " + heap.depiler());
76 System.out.println("Heap index " + heap.getHeadIndex());
77 System.out.println("Heap head value " + heap.depiler());
78 System.out.println("Heap index " + heap.getHeadIndex());
79 System.out.println("Heap head value " + heap.depiler());
80 System.out.println("Heap index " + heap.getHeadIndex());
81 System.out.println("Heap head value " + heap.depiler());
82 System.out.println("Heap index " + heap.getHeadIndex());
83 System.out.println("Heap head value " + heap.depiler());
84 System.out.println("Heap index " + heap.getHeadIndex());
85
86 heap.afficher();
87 }
88 }