Minggu, 18 April 2010

Java listdinamik

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

NilaiMatKul(){
}
}

class Elemen{
NilaiMatKul elmt;
Elemen next;

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

class List{
Elemen first;

List(){
}

/*----------------------------------*/
void createList(){


first = null;

}

/*----------------------------------*/
int countElement(){


int hasil = 0;

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

Elemen elmt;

/*inisialisasi*/
elmt = first;

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

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

}

return hasil;

}

/*----------------------------------*/
void addFirst(String nim, String nama, String nilai){


Elemen elmt;
elmt = new Elemen();

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

}

/*----------------------------------*/
void addAfter(Elemen prec, String nim, String nama, String nilai){


Elemen elmt;
elmt = new Elemen();

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

}

/*----------------------------------*/
void addLast(String nim, String nama, String nilai){


if(first == null){
/*jika list adalah list kosong*/
addFirst(nim, nama, nilai);
}
else{
/*jika list tidak kosong*/
Elemen elmt;
elmt = new Elemen();

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

/*mencari elemen terakhir list*/
Elemen last = first;

while(last.next != null){

/*iterasi*/
last = last.next;

}

last.next = elmt;
elmt = null;
}

}

/*----------------------------------*/
void delFirst(){

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

}

}

/*----------------------------------*/
void delAfter(Elemen prec){


Elemen elmt = prec.next;
prec.next = elmt.next;
elmt.next = null;


}

/*----------------------------------*/
void delLast(){

if(first != null){
/*jika list tidak kosong*/
if(countElement() == 1){
/*jika list terdiri dari satu elemen*/
delFirst();

}
else{

/*mencari elemen terakhir list*/
Elemen last = first;
Elemen before_last = null;

while(last.next != null){

/*iterasi*/
before_last = last;
last = last.next;

}

before_last.next = null;


}
}

}

/*----------------------------------*/
void printElement(){


if(first != null){
/*jika list tidak kosong*/
/*inisialisasi*/
Elemen elmt = first;
int i = 1;

while(elmt != null){
/*proses*/
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);
System.out.println("------------");

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

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

}

/*----------------------------------*/
void delAll(){

if(countElement() != 0){

int i;

for(i=countElement();i>=1;i--){
/*proses menghapus elemen list*/
delLast();
}

}

}

} /*end kelas List*/

/*----------------------------------*/
class CobaListDinamik{

public static void main(String[] args){

List L = new List();

L.createList();
L.printElement();
System.out.println("================");

L.addFirst("13501019", "Andik", "A");


L.addAfter(L.first,"13501037", "Shalahuddin", "A");
L.addLast("13501058", "Rosa", "A");
L.printElement();
System.out.println("================");
L.delLast();
L.delAfter(L.first);
L.delFirst();
L.printElement(); System.out.println("================");

}
}