Minggu, 18 April 2010

Java stackdinamik

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

NilaiMatKul(){
}
}

class Elemen{
NilaiMatKul elmt;
Elemen next;

Elemen(){
elmt = new NilaiMatKul();
}
}

class Stack{
Elemen top;

Stack(){
}

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

void createEmpty(){


top = null;

}

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

boolean isEmpty(){

boolean hasil = false;

if(top == null){
hasil = true;
}

return hasil;

}

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

int countElement(){


int hasil = 0;

if(top != null){
/*stack tidak kosong*/

Elemen elmt;

/*inisialisasi*/
elmt = top;

while(elmt != null){
/*proses*/
hasil = hasil + 1;

/*iterasi*/
elmt = elmt.next;
}

}

return hasil;

}

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

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


Elemen elmt;
elmt = new Elemen();

elmt.elmt.nim = nim;
elmt.elmt.nama = nama;
elmt.elmt.nilai = nilai;
elmt.next = top;
top = elmt;
elmt = null;

}

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

void pop(){

if(top != null){
/*jika stack bukan list kosong*/
Elemen elmt = top;
top = top.next;
elmt.next = null;

}

}

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

void printStack(){

if(top != null){
System.out.println("-------isi stack-------");
Elemen elmt = top;
int i = 1;

while(elmt != null){
System.out.println("=====================");
System.out.println("elemen ke :" + i);
System.out.println("nim : " + elmt.elmt.nim);
System.out.println("nama : " + elmt.elmt.nama);
System.out.println("nilai : " + elmt.elmt.nilai);

/*iterasi*/
elmt = elmt.next;
i = i + 1;

}
System.out.println("-----------------------");
}
else{
/*proses jika stack kosong*/
System.out.println("stack kosong");
}

}

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

class CobaStackDinamik{

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("================");
}
}