Linked List First Link Data next Null Linked

  • Slides: 31
Download presentation
Linked List First Link Data next Null

Linked List First Link Data next Null

Linked List Operasi: u Penyisipan sebuah item di awal list. u Penghapusan sebuah item

Linked List Operasi: u Penyisipan sebuah item di awal list. u Penghapusan sebuah item di awal list. u Menampilkan isi list

Linked List (insert. First Method) Linked List Link Data First next Null Link Data

Linked List (insert. First Method) Linked List Link Data First next Null Link Data next

Linked List (delete. First Method) Linked List First Link Data next Null

Linked List (delete. First Method) Linked List First Link Data next Null

Linked List (display. List Method) Linked List First Link Data next Null hasil statement

Linked List (display. List Method) Linked List First Link Data next Null hasil statement hasil statement sekarang = firstsekarang = sekarang. next sekarang =sekarang. next = sekarang. next

class Link { public int i. Data; public double d. Data; public Link next;

class Link { public int i. Data; public double d. Data; public Link next; // ------------------------------public Link(int id, double dd) { i. Data = id; d. Data = dd; } // ------------------------------public void tampilkan. Link() { System. out. print("{" + i. Data + ", " + d. Data + "} "); } }

class Link. List { private Link pertama; // ------------------------------public Link. List() { pertama =

class Link. List { private Link pertama; // ------------------------------public Link. List() { pertama = null; } // ------------------------------public boolean kosong() { return (pertama==null); } // ------------------------------}

class Link. List (lanjutan) public void sisip. Pertama(int id, double dd) { Link link.

class Link. List (lanjutan) public void sisip. Pertama(int id, double dd) { Link link. Baru = new Link(id, dd); link. Baru. next = pertama; pertama = link. Baru; } // ------------------------------public Link hapus. Pertama() { Link temp = pertama; pertama = pertama. next; return temp; } // -------------------------------

class Link. List (lanjutan) public void tampilkan. List() { System. out. print("List (pertama-->terakhir): ");

class Link. List (lanjutan) public void tampilkan. List() { System. out. print("List (pertama-->terakhir): "); Link sekarang = pertama; while(sekarang != null) { sekarang. tampilkan. Link(); sekarang = sekarang. next; } System. out. println(""); } // ------------------------------}

Link. List. App class Link. List. App { public static void main(String[] args) {

Link. List. App class Link. List. App { public static void main(String[] args) { Link. List list. Q = new Link. List(); list. Q. sisip. Pertama(22, 2. 99); list. Q. sisip. Pertama(44, 4. 99); list. Q. sisip. Pertama(66, 6. 99); list. Q. sisip. Pertama(88, 8. 99); list. Q. tampilkan. List(); while( !list. Q. kosong() ) { Link link. Q = list. Q. hapus. Pertama(); link. Q. tampilkan. Link(); System. out. print(" terhapus"); System. out. println(""); } list. Q. tampilkan. List(); } }

find Method // ------------------------------public Link find(int key) { Link current = first; while(current. i.

find Method // ------------------------------public Link find(int key) { Link current = first; while(current. i. Data != key) { if(current. next == null) return null; else current = current. next; } return current; } // -------------------------------

delete Method public Link delete(int key) { Link current = first; Link previous =

delete Method public Link delete(int key) { Link current = first; Link previous = first; while(current. i. Data != key) { if(current. next == null) return null; else { previous = current;

current = current. next; } } if(current == first) first = first. next; else

current = current. next; } } if(current == first) first = first. next; else previous. next = current. next; return current; } // ----------------------------

delete Linked List First Link Data next Null previous current

delete Linked List First Link Data next Null previous current

double-Ended List Linked List First Link Data next Null Last

double-Ended List Linked List First Link Data next Null Last

double-ended list Linked List First Null Last Null Link Data next

double-ended list Linked List First Null Last Null Link Data next

program class Link { public long d. Data; public Link next; // ---------------------------public Link(long

program class Link { public long d. Data; public Link next; // ---------------------------public Link(long d) { d. Data = d; } // ---------------------------public void display. Link() { System. out. print(“(“+d. Data+”)” + “ ” ); } // ---------------------------}

class First. Last. List { private Link first; private Link last; // ------------------------------public First.

class First. Last. List { private Link first; private Link last; // ------------------------------public First. Last. List() { first = null; last = null; } // ------------------------------public boolean is. Empty() { return first==null; } // ------------------------------public void insert. First(long dd) { Link new. Link = new Link(dd); if( is. Empty() ) last = new. Link; new. Link. next = first; first = new. Link; } // -------------------------------

// ------------------------------public void insert. Last(long dd) { Link new. Link = new Link(dd); if(

// ------------------------------public void insert. Last(long dd) { Link new. Link = new Link(dd); if( is. Empty() ) first = new. Link; else last. next = new. Link; last = new. Link; } // ------------------------------public long delete. First() { long temp = first. d. Data; if(first. next == null) last = null; first = first. next; return temp; } // -------------------------------

public void display. List() { System. out. print(“List (first-->last): “); Link current = first;

public void display. List() { System. out. print(“List (first-->last): “); Link current = first; while(current != null) { current. display. Link(); current = current. next; } System. out. println(“”); } // ------------------------------}

class First. Last. App { public static void main(String[] args) { First. Last. List

class First. Last. App { public static void main(String[] args) { First. Last. List the. List = new First. Last. List(); the. List. insert. First(22); the. List. insert. First(44); the. List. insert. First(66); the. List. insert. Last(11); the. List. insert. Last(33); the. List. insert. Last(55); the. List. display. List(); the. List. delete. First(); the. List. display. List(); } // end main() } // end class First. Last. App

Doubly linked-list Linked List First Link Data next Null prev Last Null Link Data

Doubly linked-list Linked List First Link Data next Null prev Last Null Link Data next prev

Linked List First Link Data next Null prev Last prev Null Link Data next

Linked List First Link Data next Null prev Last prev Null Link Data next prev

Program class Link { public long d. Data; public Link next; public Link previous;

Program class Link { public long d. Data; public Link next; public Link previous; // ------------------------------public Link(long d) { d. Data = d; } // ------------------------------public void display. Link() { System. out. print(d. Data + “ ”); } // ------------------------------} // end class Link

class Doubly. Linked. List { private Link first; private Link last; // ------------------------------public Doubly.

class Doubly. Linked. List { private Link first; private Link last; // ------------------------------public Doubly. Linked. List() { first = null; last = null; } // ------------------------------public boolean is. Empty() { return first==null; } // -------------------------------

// ------------------------------public void insert. First(long dd) { Link new. Link = new Link(dd); if(

// ------------------------------public void insert. First(long dd) { Link new. Link = new Link(dd); if( is. Empty() ) last = new. Link; else first. previous = new. Link; new. Link. next = first; first = new. Link; } // ------------------------------public void insert. Last(long dd) { Link new. Link = new Link(dd); if( is. Empty() ) first = new. Link; else { last. next = new. Link; new. Link. previous = last; } last = new. Link; } // -------------------------------

// ------------------------------public Link delete. First() { Link temp = first; if(first. next == null)

// ------------------------------public Link delete. First() { Link temp = first; if(first. next == null) last = null; else first. next. previous = null; first = first. next; return temp; } // ------------------------------public Link delete. Last() { Link temp = last; if(first. next == null) first = null; else last. previous. next = null; last = last. previous; return temp; } // -------------------------------

// ------------------------------// insert dd setelah key public boolean insert. After(long key, long dd) {

// ------------------------------// insert dd setelah key public boolean insert. After(long key, long dd) { Link current = first; while(current. d. Data != key) { current = current. next; if(current == null) return false; } Link new. Link = new Link(dd); if(current==last) { new. Link. next = null; last = new. Link; } else { new. Link. next = current. next; // new. Link <-- old next current. next. previous = new. Link; } new. Link. previous = current; current. next = new. Link; return true; } // -------------------------------

// ------------------------------public Link delete. Key(long key) { Link current = first; while(current. d. Data

// ------------------------------public Link delete. Key(long key) { Link current = first; while(current. d. Data != 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; } // -------------------------------

// ------------------------------public void display. Forward() { System. out. print(“List (first-->last): “); Link current =

// ------------------------------public void display. Forward() { System. out. print(“List (first-->last): “); Link current = first; while(current != null) { current. display. Link(); current = current. next; } System. out. println(“”); } // ------------------------------public void display. Backward() { System. out. print(“List (last-->first): “); Link current = last; while(current != null) { current. display. Link(); current = current. previous; } System. out. println(“”); } // ------------------------------} // end class Doubly. Linked. List

class Doubly. Linked. App { public static void main(String[] args) { Doubly. Linked. List

class Doubly. Linked. App { public static void main(String[] args) { Doubly. Linked. List the. List = new Doubly. Linked. List(); the. List. insert. First(22); the. List. insert. First(44); the. List. insert. First(66); the. List. insert. Last(11); the. List. insert. Last(33); the. List. insert. Last(55); the. List. display. Forward(); the. List. display. Backward(); the. List. delete. First(); the. List. delete. Last(); the. List. delete. Key(11); the. List. display. Forward(); the. List. insert. After(22, 77); the. List. insert. After(33, 88); the. List. display. Forward(); } }