Minggu, 18 April 2010

Java listoflust

class Mahasiswa{
String nim;
String nama;

Mahasiswa(){
}
}

class MatKul{
String kode;
String nilai;

MatKul(){
}
}

class EBaris{
Mahasiswa elmt;
EKolom col;
EBaris next;

EBaris(){
elmt = new Mahasiswa();
}
}

class EKolom{
MatKul elmt;
EKolom next;

EKolom(){
elmt = new MatKul();
}
}

class List{
EBaris first;

List(){
}

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


first = null;

}

/*----------------------------------*/
int countElementB(){


int hasil = 0;

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

EBaris elmt;

/*inisialisasi*/
elmt = first;

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

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

}

return hasil;

}

/*----------------------------------*/
int countElementK(EBaris L){


int hasil = 0;

if(L.col != null){
/*list tidak kosong*/

EKolom elmt;

/*inisialisasi*/
elmt = L.col;

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

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

}

return hasil;

}

/*----------------------------------*/
void addFirstB(String nim, String nama){

EBaris elmt;
elmt = new EBaris();

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

}

/*----------------------------------*/
void addFirstK(String kode, String nilai, EBaris L){


EKolom elmt;
elmt = new EKolom();

elmt.elmt.kode = kode;
elmt.elmt.nilai = nilai;
elmt.next = L.col;
L.col = elmt;
elmt = null;

}

/*----------------------------------*/
void addAfterB(EBaris prec, String nim, String nama){


EBaris elmt;
elmt = new EBaris();

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

}

/*----------------------------------*/
void addAfterK(EKolom prec, String kode, String nilai){

EKolom elmt;
elmt = new EKolom();

elmt.elmt.kode = kode;
elmt.elmt.nilai = nilai;
elmt.next = prec.next;
prec.next = elmt;
elmt = null;

}

/*----------------------------------*/
void addLastB(String nim, String nama){


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

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

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

while(last.next != null){

/*iterasi*/
last = last.next;

}

last.next = elmt;
elmt = null;
}

}

/*----------------------------------*/
void addLastK(String kode, String nilai, EBaris L){


if(L.col == null){
/*jika list adalah list kosong*/
addFirstK(kode, nilai, L);
}
else{
/*jika list tidak kosong*/
EKolom elmt;
elmt = new EKolom();

elmt.elmt.kode = kode;
elmt.elmt.nilai = nilai;
elmt.next = null;

/*mencari elemen terakhir list*/
EKolom last = L.col;

while(last.next != null){

/*iterasi*/
last = last.next;

}

last.next = elmt;
elmt = null;
}

}

/*----------------------------------*/
void delFirstB(){

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

}

}

/*----------------------------------*/
void delFirstK(EBaris L){


if(L.col != null){
/*jika list bukan list kosong*/
EKolom elmt = L.col;
L.col = L.col.next;
elmt.next = null;

}

}

/*----------------------------------*/
void delAfterB(EBaris prec){


EBaris elmt = prec.next;
prec.next = elmt.next;
elmt.next = null;


}

/*----------------------------------*/
void delAfterK(EKolom prec){


EKolom elmt = prec.next;
prec.next = elmt.next;
elmt.next = null;


}

/*----------------------------------*/
void delLastB(){

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

}
else{

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

while(last.next != null){

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

}

before_last.next = null;


}
}

}

/*----------------------------------*/
void delLastK(EBaris L){

if(L.col != null){
/*jika list tidak kosong*/
if(countElementK(L) == 1){
/*jika list terdiri dari satu elemen*/
delFirstK(L);

}
else{

/*mencari elemen terakhir list*/
EKolom last = L.col;
EKolom 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*/
EBaris 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);
EKolom eCol = elmt.col;
while(eCol != null){

System.out.println("kode kuliah : " + eCol.elmt.kode);
System.out.println("nilai : " + eCol.elmt.nilai);

eCol = eCol.next;

}

System.out.println("------------");

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

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

}

/*----------------------------------*/
void delAllB(){

if(countElementB() != 0){

int i;

for(i=countElementB();i>=1;i--){

/*proses menghapus elemen list*/
delLastB();
}

}

}

/*----------------------------------*/
void delAllK(EBaris L){

if(countElementK(L) != 0){

int i;

for(i=countElementK(L);i>=1;i--){

/*proses menghapus elemen list*/
delLastK(L);
}

}

}

} /*end kelas List*/

/*----------------------------------*/
class CobaListOfList{

public static void main(String[] args){

List L = new List();

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

L.addFirstB("13501019", "Andik");
L.addFirstK("IF40K1", "A", L.first);
L.addAfterK(L.first.col,"IF40Z1", "A");
L.addLastK("IF40Z2","A", L.first);

L.addAfterB(L.first, "13501037", "Shalahuddin");
L.addFirstK("TI5141", "A", L.first.next);
L.addLastK("IF5012","A", L.first.next);

L.addLastB("13501058", "Rosa");
L.addFirstK("IF5321", "A", L.first.next.next);

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

L.delAllB();
L.printElement();

System.out.println("================");

}
}