130 likes | 308 Views
DLL. Doubly Linked List. prev. next. A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes. elem. node. trailer. nodes/positions. header.
E N D
Doubly Linked List prev next • A doubly linked list provides a natural implementation of the List ADT • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes elem node trailer nodes/positions header elements Linked Lists
Insertion • We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C Linked Lists
Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Linked Lists
p A B C D Deletion • We visualize remove(p), where p = last() A B C p D A B C Linked Lists
Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t Linked Lists
ADT DLL class NodeDLL{ int data; NodeDLLprev,next; } public class DoubleLinkedList { private NodeDLLpKepala, pEkor; public DoubleLinkedList(){pKepala= null; pEkor= null;} public void sisipDipKepala(intdt){} public void sisipDipEkor(int data){} public void cetak(String kom){} public void hapusDataTertentu(intdataHapus){} public void sisipDataTerurut(int data){} }
DLL.sisipDipKepala public void sisipDipKepala(intdt){ NodeDLLbaru = new NodeDLL(); baru.data = dt; if (pKepala == null) { baru.prev = pKepala; baru.next = pEkor; pKepala = baru; pEkor = baru; } else { baru.next = pKepala; pKepala.prev = baru; pKepala = baru; baru.prev = pKepala; } }
DLL.sisipDipEkor public void sisipDipEkor(int data){ NodeDLLbaru = new NodeDLL(); baru.data = data; if (pEkor == null) { baru.prev = pKepala; baru.next = pEkor; pKepala = baru; pEkor = baru; } else { baru.prev = pEkor; pEkor.next = baru; pEkor = baru; baru.next = pEkor; } }
DLL.cetak public void cetak(String kom){ System.out.println(kom); NodeDLL p = pKepala; while (p!=pEkor.next){ System.out.print(p.data+"->"); p = p.next; } System.out.println(); }
Diskusi public void hapusDataTertentu(intdataHapus){ ??? } public void sisipDataTerurut(int data){ ??? }
TugasKelompok k-2 • Suatuangkatanmahasiswaterdiridaribeberapamahasiswa, setiapmahasiswapadasuatu semester 3 keatasdapatmengambilbeberapamatakuliah yang tidaksamaantarasatumahasiswadenganmahasiswa yang lain. Susun ADT persoalaninimenggunakan DLL. • Contoh • Angkatan : 2011 Semester : 3 • Jumlahmhs : 5 IP Rata-rata : .. • DaftarMhs • 1. Ali 011000011 2 3 • SBD B 3 • ASD B 5 2. Toni 0110000012 1 3.5 SO B+ 3 3 … DikumpulkanMinggudepantgl 27 Maret 2013 (HC) • Kembangkan SC (dapatdidownloaddi e-learning) Dikumpulksndi Recording (SebelumPulang)