+import java.lang.reflect.Array;
+
+/**
+ *
+ */
+public class Pile<E> {
+ private E[] array;
+ private int array_size;
+ private int stack_head_index;
+
+ /**
+ * set the size of the internal array
+ * @param int size the size of the array
+ */
+ public void setSize(int size) {
+ array_size = size;
+ }
+
+ /**
+ * get the size of the internal array
+ * @return the integer size of the internal array
+ */
+ public int getSize() {
+ return array_size;
+ }
+
+ /**
+ * set the stack head index
+ * @param int index the stack head index
+ */
+ public void setHeadIndex(int index) {
+ stack_head_index = index;
+ }
+
+ /**
+ * get the stack head current index
+ * @return the integer stack head index
+ */
+ private int getHeadIndex() {
+ return stack_head_index;
+ }
+
+ /**
+ * [Pile description]
+ * @param int size [description]
+ */
+ @SuppressWarnings("unchecked")
+ Pile(int size) {
+ array = (E[])new Object[size];
+ setSize(size);
+ setHeadIndex(0);
+ }
+
+ /**
+ * [Pile description]
+ * @param int size [description]
+ */
+ @SuppressWarnings("unchecked")
+ Pile(Class<E[]> cl, int size) {
+ array = (E[])Array.newInstance(cl, size);
+ setSize(size);
+ setHeadIndex(0);
+ }
+
+ /**
+ * [empiler description]
+ * @param int value [description]
+ */
+ public void empiler(E value) {
+ if (!plein()) {
+ array[stack_head_index] = value;
+ stack_head_index++;
+ } else {
+ System.out.println("La pile est pleine");
+ }
+ }
+
+ /**
+ * [depiler description]
+ * @return [description]
+ */
+ public E depiler() {
+ if (!vide()) {
+ stack_head_index--;
+ return array[stack_head_index];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * [plein description]
+ * @return [description]
+ */
+ private boolean plein() {
+ return (getHeadIndex() >= getSize());
+ }
+
+ /**
+ * [vide description]
+ * @return [description]
+ */
+ private boolean vide() {
+ return (getHeadIndex() == 0);
+ }
+
+ /**
+ * [afficher description]
+ */
+ public void afficher() {
+ for (int i = 0; i < getHeadIndex(); i++) {
+ System.out.println("element " + i + " " + array[i]);
+ }
+ }
+
+ /**
+ * The main() function
+ * @param String[] args main() function arguments array
+ */
+ public static void main(String[] args) {
+ Pile<Integer> stack = new Pile<Integer>(5);
+
+ stack.empiler(3);
+ stack.empiler(5);
+ stack.empiler(4);
+ stack.empiler(7);
+ stack.empiler(8);
+
+ stack.afficher();
+
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+ System.out.println("Stack head value " + stack.depiler());
+ System.out.println("Stack index " + stack.getHeadIndex());
+
+ stack.afficher();
+
+ Pile<String> stackStr = new Pile<String>(5);
+
+ stackStr.empiler("Bonjour");
+ stackStr.empiler("Salut");
+ stackStr.empiler("Hello");
+ stackStr.empiler("Hi");
+ stackStr.empiler("Hugh");
+
+ stackStr.afficher();
+
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+ System.out.println("Stack head value " + stackStr.depiler());
+ System.out.println("Stack index " + stackStr.getHeadIndex());
+
+ stackStr.afficher();
+ }
+}