X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=blobdiff_plain;f=Piles%2FPile.java;fp=Piles%2FPile.java;h=e7d4b4deac667aa2c934567be2bd81e94d840c3f;hp=0000000000000000000000000000000000000000;hb=d3d2d15cbc23943aa4bd255cfa85dad9c4cf5609;hpb=fc3673c1d53e435dfd1e3c06540db11c7bf68176 diff --git a/Piles/Pile.java b/Piles/Pile.java new file mode 100644 index 0000000..e7d4b4d --- /dev/null +++ b/Piles/Pile.java @@ -0,0 +1,88 @@ + +class Pile { + private int int_array[]; + private int array_size; + private int heap_head_index; + + private void setSize(int size) { + array_size = size; + } + + private int getSize() { + return array_size; + } + + private void setHeadIndex(int index) { + heap_head_index = index; + } + + private int getHeadIndex() { + return heap_head_index; + } + + Pile(int size) { + int_array = new int[size]; + setSize(size); + setHeadIndex(0); + } + + public void empiler(int value) { + if (!plein()) { + int_array[heap_head_index] = value; + heap_head_index++; + } else { + System.out.println("La pile est pleine"); + } + } + + public int depiler() { + if (!vide()) { + heap_head_index--; + return int_array[heap_head_index]; + } else { + return -1; + } + } + + public boolean plein() { + return (getHeadIndex() >= getSize()); + } + + public boolean vide() { + return (getHeadIndex() == 0); + } + + public void afficher() { + for (int i = 0; i < getSize(); i++) { + System.out.println("element " + i + " " + int_array[i]); + } + } + + public static void main(String[] args) { + Pile heap = new Pile(5); + + heap.empiler(3); + heap.empiler(5); + heap.empiler(4); + heap.empiler(7); + heap.empiler(8); + + heap.afficher(); + + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + + heap.afficher(); + } +}