tutorialpemrograman.wordpress.com - 2009
tutorialpemrograman.wordpress.com - 2009
tutorialpemrograman.wordpress.com - 2009
Linked List dan Implementasinya dalam Bahasa Java if ((temp.data == key)&&(temp == head)){ this.addFirst(input); /* jika insert pada awal linked list maka call method addFirst */ System.out.println("Insert data is succeed."); break; } else if (temp.next.data == key){ input.next = temp.next; //dari temp --> temp.next menjadi temp.next = input; //temp --> input --> temp.next System.out.println("Insert data is succeed."); break; } temp = temp.next; } } void removeFirst(){ Node temp = head; if (!isEmpty()){ if (head == tail){ head = tail = null; } else { temp = temp.next; head = temp; temp = null; } } else System.out.println("Data }
//jika element linked list hanya 1, //maka head dan tail menjadi null //sehingga linked list kosong //memajukan temp ke temp.next //kemudian head dipindah ke temp //kemudian temp di-null (optional) is empty!");
void removeLast(){ Node temp = head; if (!isEmpty()){ if (tail == head){ //jika element linked list hanya 1 head = tail = null; //maka head dan tail menjadi null } //sehingga linked list kosong else { while (temp.next != tail){ temp = temp.next; //memajukan temp hingga satu elemen } //sebelum tail. temp.next = null; //temp.next di-null,dan jadi akhir LL tail = temp; //tail dipindah ke temp temp = null; } } else System.out.println("Data is empty!"); } void remove(int key){ Node temp = head; if (!isEmpty()){ while (temp != null){ if (temp.next.data == key){ //mengganti temp.next dengan temp.next = temp.next.next; //temp.next.next break; //dari temp --> temp.next -->temp.next.next } //menjadi temp --> temp.next.next tutorialpemrograman.wordpress.com - 2009 5
Linked List dan Implementasinya dalam Bahasa Java else if ((temp.data == key)&&(temp == head)){ this.removeFirst();//jika key berada pada awal linked list, break; //maka call method removeFirst } temp = temp.next; } } else System.out.println("Data is empty!"); } void find (int key){ int i = 0; boolean found = false; Node temp = head; while (temp != null){ if (temp.data == key){ found = true; break; } i++; temp = temp.next; } if (found){ System.out.println(key+" is found at index "+i); } else System.out.println("Data isn't found"); } void printNode(){ Node temp; temp = head; while (temp != null){ System.out.println(temp.data); temp = temp.next; } } }
tutorialpemrograman.wordpress.com - 2009