Rabu, 29 Juni 2011

link list hapus belakang tampil dari depan

JAN29

import jeliot.io.*;

public class IntObj {
public int value;
public IntObj next;
}
public class MyClass {
public static void main() {
IntObj k= new IntObj();
IntObj isi;
IntObj tail;

k.value = 10;

isi = new IntObj();
isi.value = 20;
k.next = isi;
isi = null;

isi = new IntObj();
isi.value = 30;
k.next.next = isi;
isi = null;

isi = new IntObj();
isi.value = 40;
k.next.next.next = isi;
isi = null;

isi = new IntObj();
isi.value = 50;
k.next.next.next.next = isi;
tail=k.next.next.next.next;
isi = null;

System.out.println("isi data asli :");
isi = k;
while(isi != null) {
System.out.println(isi.value);
isi = isi.next;
}
System.out.print("tail = ");
System.out.println(tail.value);

System.out.println("Masukkan jumlah pointer yang akan dihapus: ");

int a= Input.readInt();

if (a==1){
k.next.next.next.next=null;
tail = k.next.next.next;
}
else if (a==2){
k.next.next.next.next=null;
k.next.next.next=null;
tail = k.next.next;
}
else if (a==3){
k.next.next.next.next=null;
k.next.next.next=null;
k.next.next=null;
tail = k.next;
}
else if (a==4){
k.next.next.next.next=null;
k.next.next.next=null;
k.next.next=null;
k.next=null;
tail = k;
}
else if (a==5){
k.next.next.next.next=null;
k.next.next.next=null;
k.next.next=null;
k.next=null;
k=null;
tail = null;
}
else{
System.out.println("input salah");
}

//list Tampil
System.out.println("isi data yang tersisa :");
isi = k;
while(isi != null) {
System.out.println(isi.value);
isi = isi.next;
}
if (tail !=null) {
System.out.print("tail = ");
System.out.println(tail.value);
}
else{
System.out.println("data dan Tailnya kosong");
}
}
}

0 komentar:

Posting Komentar