Jumat, 21 Oktober 2011

queue program

class Program_Antrian{
static int ekor =-1;
static int maxEkor =9;

public static void addQueue(String antrian[], String data){
    if (ekor >= maxEkor){
        System.out.println("maaf antrian penuh");
    }
    else{
        System.out.println("addQueue:"+data);
        ekor++;
        antrian [ekor]=data;
    }
}
public static String deQueue(String antrian[]){
    String hasil="";
    if (ekor < 0)// antrian kosong
    { hasil = "maaf, antrian kosong";
}
else{
    hasil = antrian[0];
    antrian[0]=null;
    //menggeser antrian satu langkah kedepan
    for (int i=1; i<=ekor; i++){
        antrian[i-1]=antrian[i];
        antrian [i]=null;
    }
    ekor--;
}
return (hasil);
}
public static void bacaAntrian(String antrian []){
    System.out.println("KONDISI ANTRIAN:");

    for(int i=0; i<= maxEkor;i++){

        if(i==ekor)
System.out.println(i +"."+antrian [i]+"ekor");
    else
        System.out.println(i+"."+antrian[i]);
    }
}
public static void main(String args[]){
    String antrian[]=new String [10];

    addQueue(antrian, "Mobil A");
    addQueue(antrian, "Mobil B");
    addQueue(antrian, "Mobil C");
    addQueue(antrian, "Mobil D");
    System.out.println("");

    bacaAntrian(antrian);


    addQueue(antrian, "Mobil E");
    addQueue(antrian, "Mobil F");
    addQueue(antrian, "Mobil G");
    bacaAntrian(antrian);

    addQueue(antrian, "Mobil H");
    addQueue(antrian, "Mobil I");
    addQueue(antrian, "Mobil J");
    addQueue(antrian, "Mobil K");
    addQueue(antrian, "Mobil L");
    bacaAntrian(antrian);
    System.out.println("deQueue"+deQueue(antrian));
    bacaAntrian(antrian);
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    bacaAntrian(antrian);
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    System.out.println("deQueue"+deQueue(antrian));
    bacaAntrian(antrian);

}
}

Tidak ada komentar:

Posting Komentar