| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 
 |  public class PileArray implements Pile{
    private int [] P;
    private int sommet;
public PileArray () {
	P= new int [1000];
	sommet=0;
}
public PileArray (int taille){
	P= new int [taille];
	sommet=0;
}
public PileArray ( PileArray p){
	P= new int [p.P.length];
	sommet = p.sommet;
	for (int i=0; i<sommet; i++){
		P[i]=p.P[i];
	}
}
public boolean estVide(){
	return sommet<=0;
}
public boolean estPleine(){
 
	return sommet>P.length;
}
public int nbElement(){
	return sommet;
}
public void empiler (int x){
	if (estPleine()){
		System.out.println("PileArray :: empiler: Erreur la pile est pleine");
		System.exit(-1);
	}
	else
	{
		P[sommet]=x;
		sommet= sommet+1;
	}
}
public int depiler() {
	if (estVide()){
		System.out.println("on peut pas dépiler quand il n'ya pas d'éléments");
		System.exit(-1);
	}
		sommet= sommet-1;
		return (P[sommet]);
	}
 
 
public String toString(){
	String result="[";
	for (int i=0; i<nbElement()-1; i++){
		result+=" "+P[i]+",";
	}
	if((! estVide())){
		result+=" "+ P[nbElement()-1]+" ";
	}	
	result +="]\n";
	return result;
 
}
public int sommetPile(){
	return (P[sommet-1]);
}
public boolean appartient(int e){
for (int i=0; i<sommet; i++){
	if(P[i]==e){
		return (true);
	}//if
}//for
 
     return (false);
	}
 
public void inverse (){
int tmp;	
PileArray P2 = new PileArray(this);
for (int i=0; i<sommet; i++){
	P[i]=P2.depiler();
}
}
public static void main (String args[]){
	PileArray P1 = new PileArray();
	P1.empiler(1);
	P1.empiler(2);
	P1.empiler(3);
	System.out.println("P1="+P1);
    P1.inverse();
    System.out.println("L'inverse de P1="+P1);
    System.out.println("est ce que 5 appartient à P1?");
    System.out.println(P1.appartient(5));
    System.out.println("est ce que 2 appartient à P1?");
    System.out.println(P1.appartient(2));
}
} | 
Partager