Download presentation
Presentation is loading. Please wait.
Published byValentine Booker Modified over 8 years ago
1
Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null
2
Linked List Operasi: PPPPenyisipan sebuah item di awal list. PPPPenghapusan sebuah item di awal list. MMMMenampilkan isi list
3
Linked List (insertFirst Method) First Linked List next Data Link next Data Link Null next Data Link Null
4
Linked List (deleteFirst Method) First Linked List next Data Link next Data Link next Data Link next Data Link Null
5
Linked List (displayList Method) First next Data Linked List Link next Data Link next Data Link next Data Link Null hasil statement sekarang = first hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next hasil statement sekarang = sekarang.next
6
class Link class Link { public int iData; public double dData; public Link next; // ------------------------------------------------------------- public Link(int id, double dd) { iData = id; dData = dd; } // ------------------------------------------------------------- public void tampilkanLink() { System.out.print("{" + iData + ", " + dData + "} "); } }
7
class LinkList class LinkList { private Link pertama; // ------------------------------------------------------------- public LinkList() { pertama = null; } // ------------------------------------------------------------- public boolean kosong() { return (pertama==null); } // ------------------------------------------------------------- }
8
class LinkList (lanjutan) public void sisipPertama(int id, double dd) { Link linkBaru = new Link(id, dd); linkBaru.next = pertama; pertama = linkBaru; } // ------------------------------------------------------------- public Link hapusPertama() { Link temp = pertama; pertama = pertama.next; return temp; } // -------------------------------------------------------------
9
public void tampilkanList() { System.out.print("List (pertama-->terakhir): "); Link sekarang = pertama; while(sekarang != null) { sekarang.tampilkanLink(); sekarang = sekarang.next; } System.out.println(""); } // ------------------------------------------------------------- }
10
LinkListApp class LinkListApp { public static void main(String[] args) { LinkList listQ = new LinkList(); listQ.sisipPertama(22, 2.99); listQ.sisipPertama(44, 4.99); listQ.sisipPertama(66, 6.99); listQ.sisipPertama(88, 8.99); listQ.tampilkanList(); while( !listQ.kosong() ) { Link linkQ = listQ.hapusPertama(); linkQ.tampilkanLink(); System.out.print(" terhapus"); System.out.println(""); } listQ.tampilkanList(); } }
11
find Method // ------------------------------------------------------------- public Link find(int key) { Link current = first; while(current.iData != key) { if(current.next == null) return null; else current = current.next; } return current; } // -------------------------------------------------------------
12
delete Method public Link delete(int key) { Link current = first; Link previous = first; while(current.iData != key) { if(current.next == null) return null; else{ previous = current;
13
current = current.next; }} if(current == first) first = first.next; else previous.next = current.next; return current; } // -------------------------------------------------------
14
delete First next Data Linked List Link next Data Link next Data Link next Data Link Null previouscurrent
15
double-Ended List First Last next Data Linked List Link next Data Link next Data Link next Data Link Null
16
next Data Link next Data Link double-ended list First Last Linked List next Data Link Null
17
program class Link { public long dData; public long dData; public Link next; public Link next; // ------------------------------------------------------- public Link(long d) public Link(long d) { dData = d; dData = d; } // ------------------------------------------------------- public void displayLink() public void displayLink() { System.out.print(“(“+dData+”)” + “ ” ); System.out.print(“(“+dData+”)” + “ ” ); } // ------------------------------------------------------- }
18
class FirstLastList { private Link first; private Link first; private Link last; private Link last; // ------------------------------------------------------------- public FirstLastList() { first = null; first = null; last = null; last = null; } // ------------------------------------------------------------- public boolean isEmpty() { return first==null; } { return first==null; } // ------------------------------------------------------------- public void insertFirst(long dd) { Link newLink = new Link(dd); Link newLink = new Link(dd); if( isEmpty() ) if( isEmpty() ) last = newLink; last = newLink; newLink.next = first; newLink.next = first; first = newLink; first = newLink; } // -------------------------------------------------------------
19
public void insertLast(long dd) { Link newLink = new Link(dd); Link newLink = new Link(dd); if( isEmpty() ) if( isEmpty() ) first = newLink; first = newLink; else else last.next = newLink; last.next = newLink; last = newLink; last = newLink;} // ------------------------------------------------------------- public long deleteFirst() { long temp = first.dData; long temp = first.dData; if(first.next == null) if(first.next == null) last = null; last = null; first = first.next; first = first.next; return temp; return temp; } // -------------------------------------------------------------
20
public void displayList() { System.out.print(“List (first-->last): “); System.out.print(“List (first-->last): “); Link current = first; Link current = first; while(current != null) while(current != null) { current.displayLink(); current.displayLink(); current = current.next; current = current.next; } System.out.println(“”); System.out.println(“”); } // ------------------------------------------------------------- }
21
class FirstLastApp { public static void main(String[] args) public static void main(String[] args) { FirstLastList theList = new FirstLastList(); FirstLastList theList = new FirstLastList(); theList.insertFirst(22); theList.insertFirst(22); theList.insertFirst(44); theList.insertFirst(44); theList.insertFirst(66); theList.insertFirst(66); theList.insertLast(11); theList.insertLast(11); theList.insertLast(33); theList.insertLast(33); theList.insertLast(55); theList.insertLast(55); theList.displayList(); theList.displayList(); theList.deleteFirst(); theList.deleteFirst(); theList.displayList(); theList.displayList(); } // end main() } // end main() } // end class FirstLastApp } // end class FirstLastApp
22
Link next prev Data Doubly linked-list First Last Linked List Link next prev Data next prev Data next prev Data Null
23
Link next prev Data First Last Linked List Link next prev Data Link next prev Data next prev Data Null
24
Program class Link { public long dData; public Link next; public Link previous; // ------------------------------------------------------------ public Link(long d) { dData = d; } // ------------------------------------------------------------ public void displayLink() { System.out.print(dData + “ ”); } // ------------------------------------------------------------ } // end class Link
25
class DoublyLinkedList { private Link first; private Link last; // ------------------------------------------------------------- public DoublyLinkedList() { first = null; last = null; } // ------------------------------------------------------------- public boolean isEmpty() { return first==null; } // -------------------------------------------------------------
26
public void insertFirst(long dd) { Link newLink = new Link(dd); if( isEmpty() ) last = newLink; else first.previous = newLink; newLink.next = first; first = newLink; } // ------------------------------------------------------------- public void insertLast(long dd) { Link newLink = new Link(dd); if( isEmpty() ) first = newLink; else{ last.next = newLink; newLink.previous = last; } last = newLink; } // -------------------------------------------------------------
27
public Link deleteFirst() { Link temp = first; if(first.next == null) last = null; else first.next.previous = null; first = first.next; return temp; } // ------------------------------------------------------------- public Link deleteLast() { Link temp = last; if(first.next == null) first = null; else last.previous.next = null; last = last.previous; return temp; } // -------------------------------------------------------------
28
// insert dd setelah key public boolean insertAfter(long key, long dd) { Link current = first; while(current.dData != key) { current = current.next; if(current == null) return false; } Link newLink = new Link(dd); if(current==last){ newLink.next = null; last = newLink; }else{ newLink.next = current.next; // newLink <-- old next current.next.previous = newLink; } newLink.previous = current; current.next = newLink; return true; } // -------------------------------------------------------------
29
public Link deleteKey(long key) { Link current = first; while(current.dData != key) { current = current.next; if(current == null) return null; }if(current==first) first = current.next; else current.previous.next = current.next; if(current==last) last = current.previous; else current.next.previous = current.previous; return current; } // -------------------------------------------------------------
30
public void displayForward() { System.out.print(“List (first-->last): “); Link current = first; while(current != null) {current.displayLink(); current = current.next; }System.out.println(“”);} // ------------------------------------------------------------- public void displayBackward() { System.out.print(“List (last-->first): “); Link current = last; while(current != null) {current.displayLink(); current = current.previous; }System.out.println(“”);} // ------------------------------------------------------------- } // end class DoublyLinkedList
31
class DoublyLinkedApp { public static void main(String[] args) { DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(22);theList.insertFirst(44);theList.insertFirst(66);theList.insertLast(11);theList.insertLast(33);theList.insertLast(55);theList.displayForward();theList.displayBackward();theList.deleteFirst();theList.deleteLast();theList.deleteKey(11);theList.displayForward(); theList.insertAfter(22, 77); theList.insertAfter(33, 88); theList.displayForward();}}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.