Minggu, 18 April 2010

Java queuedinamik

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

NilaiMatKul(){
}
}

class Elemen{
NilaiMatKul elmt;
Elemen next;

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

class Queue{
Elemen first;
Elemen last;

Queue(){
}

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

void createEmpty(){


first = null;
last = null;

}

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

boolean isEmpty(){

boolean hasil = false;

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

return hasil;

}

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

int countElement(){


int hasil = 0;

if(first != null){
/*queue tidak kosong*/

Elemen elmt;

/*inisialisasi*/
elmt = first;

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

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

}

return hasil;

}

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

void add(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 = null;
if(first == null){
first = elmt;
}
else{
last.next = elmt;
}
last = elmt;
elmt = null;

}

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

void del(){

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

}

}

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

void printQueue(){

if(first != null){
System.out.println("-------isi queue-------");
Elemen elmt = first;
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 queue kosong*/
System.out.println("queue kosong");
}

}

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

class CobaQueueDinamik{

public static void main(String[] args){

Queue Q = new Queue();

Q.createEmpty();
Q.printQueue();
System.out.println("================");
Q.add("13507701", "Nana", 64.75);
Q.add("13507702", "Rudi", 75.11);
Q.add("13507703", "Dea", 84.63);
Q.printQueue();
System.out.println("================");
Q.del();
Q.del();
Q.printQueue();
System.out.println("================");
}
}