class NilaiMatKul{
String nim;
String nama;
String nilai;
NilaiMatKul(){
}
}
class Elemen{
NilaiMatKul elmt;
int next;
Elemen(){
elmt = new NilaiMatKul();
}
}
class List{
int first;
Elemen[] data = new Elemen[10];
List(){
for(int i=0;i<10;i++){
data[i] = new Elemen();
}
}
/*----------------------------------*/
void createList(){
first = -1;
int i;
for(i=0;i<10;i++){
/*proses menginisialisasi isi array*/
data[i].next = -2;
}
}
/*----------------------------------*/
int countElement(){
int hasil = 0;
if(first != -1){
/*list tidak kosong*/
int elmt;
/*inisialisasi*/
elmt = first;
while(elmt != -1){
/*proses*/
hasil = hasil + 1;
/*iterasi*/
elmt = data[elmt].next;
}
}
return hasil;
}
/*----------------------------------*/
int emptyElement(){
int hasil = -1;
if(countElement() < 10){
boolean ketemu = false;
int i = 0;
while((ketemu == false)&&(i < 10)){
if(data[i].next == -2){
hasil = i;
ketemu = true;
}
else{
i = i + 1;
}
}
}
return hasil;
}
/*----------------------------------*/
void addFirst(String nim, String nama, String nilai){
if(countElement() < 10){
int indeks = emptyElement();
data[indeks].elmt.nim = nim;
data[indeks].elmt.nama = nama;
data[indeks].elmt.nilai = nilai;
if(first == -1){
/*jika list kosong*/
data[indeks].next = -1;
}
else{
/*jika list tidak kosong*/
data[indeks].next = data[first].next;
}
first = indeks;
}
else{
/*proses jika array penuh*/
System.out.println("sudah tidak dapat ditambah");
}
}
/*----------------------------------*/
void addAfter(int prec, String nim, String nama, String nilai){
if(countElement() < 10){
int indeks = emptyElement();
data[indeks].elmt.nim = nim;
data[indeks].elmt.nama = nama;
data[indeks].elmt.nilai = nilai;
data[indeks].next = data[prec].next;
data[prec].next = indeks;
}
else{
/*proses jika array penuh*/
System.out.println("sudah tidak dapat ditambah");
}
}
/*----------------------------------*/
void addLast(String nim, String nama, String nilai){
if(first == -1){
/*proses jika list masih kosong*/
int indeks = 0;
data[indeks].elmt.nim = nim;
data[indeks].elmt.nama = nama;
data[indeks].elmt.nilai = nilai;
data[indeks].next = -1;
first = indeks;
}
else{
/*proses jika list telah berisi elemen*/
if(countElement() < 10){
/*proses jika array belum penuh*/
int indeks = emptyElement();
data[indeks].elmt.nim = nim;
data[indeks].elmt.nama = nama;
data[indeks].elmt.nilai = nilai;
data[indeks].next = -1;
/*proses mencari indeks elemen terakhir*/
/*inisialisasi*/
int last = first;
while(data[last].next != -1){
/*iterasi*/
last = data[last].next;
}
data[last].next = indeks;
}
else{
/*proses jika array penuh*/
System.out.println("sudah tidak dapat ditambah");
}
}
}
/*----------------------------------*/
void delFirst(){
if(first != -1){
int elmt = first;
first = data[first].next;
/*elemen awal sebelumnya dikosongkan*/
data[elmt].next = -2;
}
else{
/*proses jika list kosong*/
System.out.println("list kosong");
}
}
/*----------------------------------*/
void delAfter(int prec){
int elmt = data[prec].next;
data[prec].next = data[elmt].next;
/*pengosongan elemen*/
data[elmt].next = -2;
}
/*----------------------------------*/
void delLast(){
if(first != -1){
if(countElement() == 1){
/*proses jika list hanya berisi satu elemen*/
delFirst();
}
else{
int last = first;
int before_last = -1;
while(data[last].next != -1){
/*iterasi*/
before_last = last;
last = data[last].next;
}
/*elemen sebelum elemen terakhir menjadi elemen terakhir*/
data[before_last].next = -1;
/*elemen terakhir sebelumnya dikosongkan*/
data[last].next = -2;
}
}
else{
/*proses jika list kosong*/
System.out.println("list kosong");
}
}
/*----------------------------------*/
void printElement(){
if(first != -1){
/*inisialisasi*/
int elmt = first;
int i = 1;
while(elmt != -1){
/*proses*/
System.out.println("elemen ke : " + i);
System.out.println("nim : " + data[elmt].elmt.nim);
System.out.println("nama : " + data[elmt].elmt.nama);
System.out.println("nilai : " + data[elmt].elmt.nilai);
System.out.println("next : " + data[elmt].next);
System.out.println("------------");
/*iterasi*/
elmt = data[elmt].next;
i = i + 1;
}
}
else{
/*proses jika list kosong*/
System.out.println("list kosong");
}
}
/*----------------------------------*/
void delAll(){
int i;
for(i=countElement();i>=1;i--){
/*proses menghapus elemen list*/
delLast();
}
}
}
/*----------------------------------*/
class CobaListStatik{
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("================");
}
}