DLL

13
DLL

description

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. - PowerPoint PPT Presentation

Transcript of DLL

Page 1: DLL

DLL

Page 2: DLL

Linked Lists 2

Doubly Linked List

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

prev next

elem

trailerheader nodes/positions

elements

node

Page 3: DLL

Linked Lists 3

Insertion

We visualize operation insertAfter(p, X), which returns position q

A B X C

A B C

p

A B C

p

X

q

p q

Page 4: DLL

Linked Lists 4

Insertion Algorithm

Algorithm insertAfter(p,e):Create a new node vv.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}

Page 5: DLL

Linked Lists 5

Deletion We visualize remove(p), where p = last()

A B C D

p

A B C

D

p

A B C

Page 6: DLL

Linked Lists 6

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

Page 7: DLL

Implemnetasi

Page 8: DLL

ADT DLL

class NodeDLL{int data;NodeDLL prev,next;

} public class DoubleLinkedList {

private NodeDLL pKepala, pEkor;public DoubleLinkedList(){pKepala = null; pEkor = null;} public void sisipDipKepala(int dt){}public void sisipDipEkor(int data){}public void cetak(String kom){} public void hapusDataTertentu(int dataHapus){}public void sisipDataTerurut(int data){}

Page 9: DLL

DLL.sisipDipKepala

public void sisipDipKepala(int dt){NodeDLL baru = 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;}}

Page 10: DLL

DLL.sisipDipEkor

public void sisipDipEkor(int data){NodeDLL baru = 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;

}}

Page 11: DLL

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();

}

Page 12: DLL

Diskusi

public void hapusDataTertentu(int dataHapus){

???}

public void sisipDataTerurut(int data){

???}

Page 13: DLL

Tugas Kelompok k-2

Suatu angkatan mahasiswa terdiri dari beberapa mahasiswa, setiap mahasiswa pada suatu semester 3 ke atas dapat mengambil beberapa matakuliah yang tidak sama antara satu mahasiswa dengan mahasiswa yang lain. Susun ADT persoalan ini menggunakan DLL.

Contoh Angkatan : 2011 Semester : 3 Jumlah mhs : 5 IP Rata-rata : .. Daftar Mhs 1. Ali011000011 2 3

▪ SBD B 3▪ ASD B 52. Toni 0110000012 1 3.5

SO B+ 33 …

Dikumpulkan Minggu depan tgl 27 Maret 2013 (HC) Kembangkan SC (dapat didownload di e-learning)

Dikumpulksn di Recording (Sebelum Pulang)