Linked List - PowerPoint PPT Presentation

About This Presentation
Title:

Linked List

Description:

Linked List First Linked List next next next next Link Link Link Link Data Data Data Data Null – PowerPoint PPT presentation

Number of Views:271
Avg rating:3.0/5.0
Slides: 32
Provided by: infor68
Category:
Tags: doubly | link | linked | list

less

Transcript and Presenter's Notes

Title: Linked List


1
Linked List
Linked List
Link
Link
Link
Link
Null
2
Linked List
  • Operasi
  • Penyisipan sebuah item di awal list.
  • Penghapusan sebuah item di awal list.
  • Menampilkan isi list

3
Linked List (insertFirst Method)
4
Linked List (deleteFirst Method)
Linked List
Link
Link
Null
5
Linked List (displayList Method)
Linked List
Link
Link
Link
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 (pertamanull)
  • // -----------------------------------------------
    --------------

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
class LinkList (lanjutan)
  • public void tampilkanList()
  • System.out.print("List (pertama--gtterakhir) ")
  • 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
Linked List
Link
Link
Link
Link
Null
previous
current
15
double-Ended List
Linked List
Link
Link
Link
Link
First Last
Null
16
double-ended list
Linked List
First Last
Null
Null
17
program
  • class Link
  • public long dData
  • public Link next
  • // -----------------------------------------------
    --------
  • public Link(long d)
  • dData d
  • // -----------------------------------------------
    --------
  • public void displayLink()
  • System.out.print((dData) )
  • // -----------------------------------------------
    --------

18
  • class FirstLastList
  • private Link first
  • private Link last
  • // -----------------------------------------------
    --------------
  • public FirstLastList()
  • first null
  • last null
  • // -----------------------------------------------
    --------------
  • public boolean isEmpty()
  • return firstnull
  • // -----------------------------------------------
    --------------
  • public void insertFirst(long dd)
  • Link newLink new Link(dd)
  • if( isEmpty() )
  • last newLink

19
  • // -----------------------------------------------
    --------------
  • public void insertLast(long dd)
  • Link newLink new Link(dd)
  • if( isEmpty() )
  • first newLink
  • else
  • last.next newLink
  • last newLink
  • // -----------------------------------------------
    --------------
  • public long deleteFirst()
  • long temp first.dData
  • if(first.next null)
  • last null
  • first first.next
  • return temp

20
  • public void displayList()
  • System.out.print(List (first--gtlast) )
  • Link current first
  • while(current ! null)
  • current.displayLink()
  • current current.next
  • System.out.println()
  • // -----------------------------------------------
    --------------

21
  • class FirstLastApp
  • public static void main(String args)
  • FirstLastList theList new
    FirstLastList()
  • theList.insertFirst(22)
  • theList.insertFirst(44)
  • theList.insertFirst(66)
  • theList.insertLast(11)
  • theList.insertLast(33)
  • theList.insertLast(55)
  • theList.displayList()
  • theList.deleteFirst()
  • theList.deleteFirst()
  • theList.displayList()
  • // end main()
  • // end class FirstLastApp

22
Doubly linked-list
Linked List
First Last
23
Linked List
First Last
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 firstnull
  • // -----------------------------------------------
    --------------

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

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

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(currentlast)
  • newLink.next null
  • last newLink
  • else

29
  • // -----------------------------------------------
    --------------
  • public Link deleteKey(long key)
  • Link current first
  • while(current.dData ! key)
  • current current.next
  • if(current null)
  • return null
  • if(currentfirst)
  • first current.next
  • else
  • current.previous.next current.next
  • if(currentlast)
  • last current.previous
  • else
  • current.next.previous current.previous
  • return current

30
  • // -----------------------------------------------
    --------------
  • public void displayForward()
  • System.out.print(List (first--gtlast) )
  • Link current first
  • while(current ! null)
  • current.displayLink()
  • current current.next
  • System.out.println()
  • // -----------------------------------------------
    --------------
  • public void displayBackward()
  • System.out.print(List (last--gtfirst) )
  • Link current last
  • while(current ! null)

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)
Write a Comment
User Comments (0)
About PowerShow.com