class simpul{
int data;
simpul next, prev;
public simpul(int d){ data=d; next=prev=null; }
int getdata(){ return data; }
}
class doubleLL{
simpul head, tail;
int jumlah;
public doubleLL(){ jumlah=0; head=tail=null; }
void tambahdepan(int d){
simpul baru = new simpul(d);
if(head==null){
head=tail=baru;
}
else{
head.prev = baru;
baru.next = head;
head = baru;
}
}
void hapusdepan(){
if (head==null){
System.out.println("Linked List kosong...");
}
else if(head.next==null){ //linked list tinggal 1 simpul
head = tail = null;
}
else{ //linked list memiliki > 1 simpul
head = head.next;
head.prev = null;
}
}
void tampil(){
simpul temp;
System.out.print("Baca maju : ");
for(temp=head; temp!=null; temp=temp.next){
System.out.print(temp.getdata()+" ");
}
System.out.print("\nBaca mundur : ");
for(temp=tail; temp!=null; temp=temp.prev){
System.out.print(temp.getdata()+" ");
}
System.out.println();
}
}
public class appdoublelinkedlist {
public static void main(String h[]){
doubleLL dll = new doubleLL();
dll.tambahdepan(7);
dll.tampil();
dll.tambahdepan(6);
dll.tampil();
dll.tambahdepan(8);
dll.tampil();
dll.hapusdepan();
dll.tampil();
dll.hapusdepan();
dll.tampil();
dll.hapusdepan();
dll.tampil();
dll.hapusdepan();
}
}
No comments:
Post a Comment