Minggu, 18 April 2010

Java stackstatik

class NilaiMatKul{
String nim;
String nama;
double nilai;

NilaiMatKul(){
}
}

class Stack{
int top;
NilaiMatKul[] data = new NilaiMatKul[10];

Stack(){
for(int i=0;i<10;i++){
data[i] = new NilaiMatKul();
}
}

/*--------------------------*/

void createEmpty(){


top = -1;

}

/*--------------------------*/

boolean isEmpty(){

boolean hasil = false;

if(top == -1){
hasil = true;
}

return hasil;

}

/*--------------------------*/

boolean isFull(){

boolean hasil = false;
if(top == 9){
hasil = true;
}

return hasil;

}

/*--------------------------*/

void push(String nim, String nama, double nilai){


if(isFull() == true){
/*jika stack penuh*/
System.out.println("stack penuh");
}
else{
if(isEmpty() == true){
/*jika stack kosong*/
top = 0;
data[0].nim = nim;
data[0].nama = nama;
data[0].nilai = nilai;
}
else{
/*jika stack tidak kosong*/
top = top + 1;
data[top].nim = nim;
data[top].nama = nama;
data[top].nilai = nilai;

}
}

}

/*--------------------------*/

void pop(){

if(top == 0){
/*jika stack berisi satu elemen*/
top = -1;
}
else{
if(top != -1){
/*jika stack tidak kosong*/
top = top - 1;
}
}

}

/*--------------------------*/

void printStack(){

if(top != -1){
System.out.println("-------isi stack-------");
int i;
for(i = top;i >= 0; i--){
System.out.println("=====================");
System.out.println("elemen ke :" + i);
System.out.println("nim : " + data[i].nim);
System.out.println("nama : " + data[i].nama);
System.out.println("nilai : " + data[i].nilai);
}
System.out.println("-----------------------");
}
else{
/*proses jika stack kosong*/
System.out.println("stack kosong");
}

}

/*--------------------------*/
}
/*--------------------------*/

class CobaStackStatik{

public static void main(String[] args){

Stack S = new Stack();

S.createEmpty();
S.printStack();
System.out.println("================");
S.push("13507701", "Nana", 64.75);
S.push("13507702", "Rudi", 75.11);
S.push("13507703", "Dea", 84.63);
S.printStack();
System.out.println("================");

S.pop();
S.pop();
S.printStack();
System.out.println("================");
}
}