1 / 54

LINKED LIST

LINKED LIST. History of Linked List. Dikembangkan tahun 1955-1956 oleh Allen Newell, Cliff Shaw dan Herbert Simon di RAND Corporation sebagai struktur data utama untuk bahasa Information Processing Language (IPL).

andra
Télécharger la présentation

LINKED LIST

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LINKED LIST

  2. History of Linked List • Dikembangkan tahun 1955-1956 oleh Allen Newell, Cliff Shaw dan Herbert Simon di RAND Corporation sebagaistruktur data utamauntukbahasa Information Processing Language (IPL). • IPL dibuatuntukmengembangkan program artificial intelligence, sepertipembuatan Chess Solver. • Victor Yngvedi Massachusetts Institute of Technology (MIT) jugamenggunakan linked list pada natural language processing dan machine transitions padabahasapemrograman COMMIT.

  3. Definisi • Linked list : struktur data yang dibangun dari satu atau lebih node yang menempati alokasi memori secara dinamis. • Node : tempat penyimpanan data yang terdiri dari dua bagian/field. • Field 1 adalah Data, digunakan untuk menyimpan data/nilai. • Field 2 adalah Pointer, untuk menyimpan alamat tertentu.

  4. Linked List • Jika linked list hanya berisi satu node maka pointernya akan menunjuk ke NULL. • Jika linked list memiliki lebih dari satu node maka pointer menyimpan alamat dari node berikutnya. Sehingga antara node satu dengan node yang lain akan terhubung. Kecuali node paling ujung akan menunjuk ke NULL. • Pointer disebut juga sebagai link.

  5. Array VS Linked List

  6. Array VS Linked List a b c d e c a e d b • Menyimpankoleksielemensecaranon-contiguously. • Elemendapatterletakpadalokasi memory yang salingberjauhan. Bandingkandengan array dimanatiap-tiapelemenakanterletakpadalokasi memory yang berurutan. Array representation Linked list representation

  7. Array VS Linked List • Mengizinkanoperasipenambahanataupenghapusanelemenditengah-tengahkoleksidenganhanyamembutuhkanjumlahperpindahanelemen yang konstan. • Bandingkandengan array. Berapabanyakelemen yang harusdipindahkanbilaakanmenyisipielemenditengah-tengah array?

  8. Linked List • Linked list dibedakan menjadi 2 : • Single linked list • Double linked list

  9. Gambaran Struktur Node null Double linked-list null Single linked-list null Link atau pointer data

  10. Single Linked List • Single : artinya pointer-nya hanya satu buah dan satu arah, yaitu menunjuk ke node sesudahnya. • Node terakhirakanmenunjukke NULL yang akandigunakansebagaikondisiberhentipadasaatpembacaanisi linked list. • ilustrasi single linked list yang memiliki 4 node :

  11. Ilustrasi Single Linked List • Ilustrasi single linked list pada memory : • Node e tidak menunjuk ke node manapun sehingga pointer dari node e adalah NULL. Dapat disimpulkan bahwa node ini adalah node yang paling belakang (node ekor). c a e d b Ekor

  12. Kepala Ilustrasi Single Linked List • Ilustrasi single linked list pada memory : • Karena node tidak ditunjuk oleh node manapun maka node ini adalah node yang paling depan (node kepala). c a e d b

  13. A0 A1 A2 A3 Ilustrasi Single Linked List • Linked list yang memiliki 4 node, dimana node terakhir menunjuk ke NULL. Kepala Ekor

  14. pointer data “Single” Representation Penjelasan: • Pembuatanclass bernamaNode yang berisi 2 field/variabel, yaitudatabertipeObject danpointer yang bertipeclass Node. • Field data : digunakan untuk menyimpan data/nilai pada linked list. Field pointer : digunakan untuk menyimpan alamat node berikutnya. class Node { Object data; Node pointer; } Ilustrasi :

  15. Pembentukan Obyek Node • Deklarasi atau pembentukan obyek Node menggunakan perintah new. • Bentuknya adalah : new Node();

  16. pointer data Contoh program public class LinkedList1 { public static void main(String[] args) { Node head = new Node(); } } Ilustrasi : head

  17. Pengaksesan Field pada Node • Untuk mengakses field dari node menggunakan object node kemudian diikuti dengan tanda . (titik) • Contoh : • Mengakses data dari head perintahnya : head.data; • Mengakses pointer dari head perintahnya : head.pointer;

  18. null null Contoh program public class LinkedList1 { public static void main(String[] args) { Node head = new Node(); System.out.println(“data : " + head.data); System.out.println("pointer: " + head.pointer); } } Ilustrasi : head Output :

  19. Pengisian Data pada Field • Untuk mengisikan data pada field digunakan operator assigment (=). • Contoh : memberikan data “A” pada head perintahnya adalah : head.data = “A”;

  20. null A Contoh program public class LinkedList1 { public static void main(String[] args) { Node head= new Node(); head.data = "A"; System.out.println(“data : " + head.data); System.out.println("pointer: " + head.pointer); } } Ilustrasi : head Output :

  21. Pointer Head • Untuk mengingat node yg paling depan (node kepala) digunakan sebuah pointer yang akan menyimpan alamat dari node depan. • Pointer ini biasanya diberi nama head. head head

  22. Pointer Tail • Untuk mengingat node yg paling belakang (node ekor) digunakan sebuah pointer yang akan menyimpan alamat dari node belakang. • Pointer ini biasanya diberi nama tail. tail tail

  23. A0 A1 A2 A3 Contoh • Linked list yang memiliki 4 node : head tail

  24. Operasi Linked List • Inisialisasi • isEmpty • size • Penambahan • Penghapusan • Penyisipan • Pencarian • Pengaksesan

  25. null null null element next element Class Node Constructor 1 public class Node { Object data; Node pointer; Node() { } Node(Object data) { this.data = data; } Node(Object data, Node pointer) { this.data = data; this.pointer = pointer; } } Constructor 2 Constructor 3

  26. (1) inisialisasi • Proses ini digunakan untuk mendeklarasi sekaligus memberikan nilai awal (inisialisasi) pada pointer head dan tail. • Nilai awal kedua pointer tersebut adalah NULL. Yang menandakan bahwa linked list dalam kondisi kosong (belum ada node yang terbentuk). Node head,tail; void inisialisasi() { head=tail=null; }

  27. (2)isEmpty • Digunakan untuk mengetahui linked dalam kondisi kosong. • Kondisi kosong : jika size = 0 atau jika head=tail=null. booleanisEmpty() { return size==0; }

  28. (3) size • Digunakan untuk mengetahui banyak node pada linked list. • Size akan bertambah 1 setiap ada node baru yang ditambahkan pada linked list. • Size akan berkurang 1 setiap ada penghapusan node. int size() { return size; }

  29. (4) Penambahan • Dibedakan menjadi : • Penambahan dari depan • Penambahan dari belakang • Penambahan setelah node tertentu • Penambahan sebelum node tertentu

  30. Penambahan dari Depan • Jika kondisi awal node kosong maka head dan tail akan sama-sama menunjuk ke node input. • Jika pada linked list telah ada node, maka head akan menunjuk ke node input (hanya head yang bergerak). void addFirst(Node input){ if (isEmpty()){ head=input; tail=input; } else { input.pointer = head; head = input; } size++; }

  31. Ilustrasi : addFirst(x) a b c d x a b c d head Node input • Menambahkan Xpadalokasi paling depan. x Kondisi awal pada linked list : head Setelah penambahan node x didepan:

  32. Penambahan dari Belakang • Jika kondisi awal node kosong maka head dan tail akan sama-sama menunjuk ke node input. • Jika pada linked list telah ada node, maka tail akan menunjuk ke node input (hanya tail yang bergerak). void addLast(Node input){ if (isEmpty()){ head = input; tail = input; } else { tail.pointer = input; tail = input; } size++; }

  33. Ilustrasi : addLast(x) a a b b c c d d Node input x Kondisi awal pada linked list : tail Setelah penambahan node x dibelakang : x tail • menambahkanXpadaakhir list :

  34. Contoh program public class TestLinkedList { public static void main(String[] args) { LinkedList1 list = new LinkedList1(); System.out.println("head : " + list.head); System.out.println("tail : " + list.tail); list.addFirst(new Node()); System.out.println("head : " + list.head); System.out.println("tail : " + list.tail); list.addFirst(new Node()); System.out.println("head : " + list.head); System.out.println("tail : " + list.tail); list.addLast(new Node()); System.out.println("head : " + list.head); System.out.println("tail : " + list.tail); } }

  35. output head : null tail : null head : asd.Node@42e816 tail : asd.Node@42e816 head : asd.Node@9304b1 tail : asd.Node@42e816 head : asd.Node@9304b1 tail : asd.Node@190d11

  36. Penambahan setelah Node tertentu • Dilakukan pencarian node yang memiliki data yang sama dengan key. void insertAfter(Object key,Node input){ Node temp = head; do{ if(temp.data==key){ input.pointer = temp.pointer; temp.pointer = input; size++; System.out.println("Insert data is succeed."); break; } temp = temp.pointer; }while (temp!=null); }

  37. Ilustrasi : Insert After(a) a b c d a x b c d x temp temp • Menyisipkan X pada lokasi setelah temp.

  38. Penambahan sebelum Node tertentu void insertBefore(Object key,Node input){ Node temp = head; while (temp != null){ if ((temp.data == key)&&(temp == head)) { this.addFirst(input); System.out.println("Insert data is succeed."); break; } else if (temp.pointer.data == key) { input.pointer = temp.pointer; temp.pointer = input; System.out.println("Insert data is succeed."); break; } temp = temp.pointer; } }

  39. (5) Penghapusan • Dibedakan menjadi : • Hapus node depan • Hapus node belakang • Hapus node tertentu

  40. Hapus node depan void removeFirst(){ Node temp = head; if (!isEmpty()){ if (head == tail) head = tail = null; else { temp = temp.pointer; head = temp; temp = null; } size--; } else System.out.println("Data is empty!"); }

  41. Hapus node belakang void removeLast(){ Node temp = head; if (!isEmpty()){ if (tail == head){ head = tail = null; } else { while (temp.pointer != tail){ temp = temp.pointer; } temp.pointer = null; tail = temp; temp = null; } size--; } else System.out.println("Data is empty!"); }

  42. Hapus node tertentu void remove(Object key){ Node temp = head; if (!isEmpty()){ while (temp != null){ if (temp.pointer.data == key){ temp.pointer = temp.pointer.pointer; if(temp.pointer == null) tail=temp; break; } else if ((temp.data == key)&&(temp == head)){ this.removeFirst(); break; } temp = temp.pointer; } } else System.out.println("Data is empty!"); size--; }

  43. Linked Lists: menghapus elemen X a b x temp a b x temp a b temp Hasil akhir : Prosesmenghapusdilakukandenganmengabaikanelemen yang hendakdihapusdengancaramelewati pointer (reference) darielementersebutlangsungpadaelemenselanjutnya. Elemenxdihapusdenganmeng-assignfieldnextpadaelemenadenganalamatb.

  44. a b x temp Langkah-langkahmenghapuselemen • Tidakadaelemen lain yang menyimpanalamatnode x. • Node x tidakbisadiakseslagi. • Java Garbage Collectorakanmembersihkanalokasi memory yang tidakdipakailagiatautidakbisadiakses. • Dengankata lain, menghapus node x.

  45. Pengaksesan • Digunakan untuk mencetak data seluruh node mulai dari yang paling depan sampai ketemu NULL. public void print() { Node p = head; while (p != null) { System.out.println (p.data); p = p.pointer; } }

  46. Operasi Linked List dengan Index • Pengaksesan data node • Penambahan data • Penghapusan data • Pengaksesan index

  47. Method checkIndex(int index) void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); }

  48. Method get(int index) public Object get(int index) { checkIndex(index); Node currentNode = head; for (inti = 0; i < index; i++) currentNode = currentNode.pointer; return currentNode.data; }

  49. Method indexOf(Object theElement) public intindexOf(Object theElement) { // search the chain for theElement Node currentNode = head; int index = 0; // index of currentNode while (currentNode != null && !currentNode.data.equals(theElement)) { // move to next node currentNode = currentNode.pointer; index++; } // make sure we found matching element if (currentNode == null) return -1; else return index; }

  50. Method remove(int index) public Object remove(int index) { checkIndex(index); Object removedElement; if (index == 0) // remove first node { removedElement = head.data; head = head.pointer; } else { // use q to get to predecessor of desired node Node q = head; for (inti = 0; i < index - 1; i++) q = q.pointer; removedElement = q.pointer.data; q.pointer = q.pointer.pointer; // remove desired node tail=q; } size--; return removedElement; }

More Related