LinkedList in Java - PowerPoint PPT Presentation

About This Presentation
Title:

LinkedList in Java

Description:

??????????????????????????????????????????. ????????????????? ... Public E set(int index, E element) ???????????????? index ??????????????????????????????????? ... – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 77
Provided by: Toe
Category:
Tags: linkedlist | eset | java

less

Transcript and Presenter's Notes

Title: LinkedList in Java


1
LinkedList in Java
  • Vishnu Kotrajaras, Ph.D.

2
????????????????????????????????
  • ??????????????????????????????????????????
  • ?????????????????????????????????????

3
??????????? ?????????????? ArrayList
  • ????????????????????????
  • ??????????????????????????????????????????????????
  • ????????????????????????? System.arraycopy
    ??????????

4
???????????????
  • ??????????????????????????????
  • ????????????????????????????????????????
  • ??????????????????????????? ???????????????????
    (???????????????????????????????????? LinkedList
    ?????????) ????????? O(n) ??????????????????????
    ???????????????????????????
  • ????????????? ??????????????????????????????????
    ????????????????? O(n) ???????????????????????????
    ??????
  • ??????? ???????????????????????????
    ??????????????????????????????????????????

5
??????????????????????????
  • ???????????????????????????????
    ????????????????????????????????????????????
    (????????????????????????????????
    ???????????????)
  • ????? ensureCapacity ??? trimToSize
  • ??????? 6 ????????????????????????????

6
????????????????????????????
  • ????????? 6 ?????
  • addFirst(E element)
  • getFirst
  • removeFirst
  • addLast(E element)
  • getLast
  • removeLast
  • ??????????????????????????????????????
  • ?????????????????????????????????
    ????????????????????????? ????????????????????????
    ?????????? ???????????????????????????????????????
    ??????????????????????????????????????????????????
    ???

7
????????????????????????????(???)
  • ??????????????????????????????????????????? ????
  • removeFirst() ????????? remove(0) ??????
  • ?????? removeFirst() ??????????? ????????????

8
???????????????????????????????????????
  • public boolean add(E Element)
  • ??????????????????
  • LinkedList ????????? ????????????????????????????
    ???????????????????????
  • ArrayList ??????????????????? ???????????????????
    ??? O(n)
  • ????????????????? n ?????
  • LinkedList ????????? O(n) ?????????? n ?????
  • ArrayList ?????????????????????
    ?????????????????????????????????????
    ?????????????????? O(n) ?????????

9
???????????????????????????????????????(2)
  • public E get(int index)
  • ????????????????????????????????? index ????
  • LinkedList ???????????????????? ???????????????
    O(n)
  • ArrayList ??? random access ?????? ???????????
    ????????
  • Public E set(int index, E element)
  • ???????????????? index ???????????????????????????
    ????????
  • LinkedList O(n) ????????????????? get
  • ArrayList ?????????????????

10
???????????????????????????????????
  • ????????????????????? 1000 ??? ?????? 1000 Entry
    object ????????
  • ????????????? ????????????????????????????????????
    ????????????????????????
  • ??????????????????????????????????????????????????
    ?????????????????????????????????????????
    ??????????????????????????????????????????????????
    ?????????????????????????????????

11
Internal class
????????????????????????????????????????????
  • private static class EntryltEgt
  • E element
  • EntryltEgt next
  • EntryltEgt previous
  • ??????????????????????? ?????????????

???????????????????????????????????
12
Internal class(2)
  • Entry(E element, EntryltEgt next, EntryltEgt
    previous)
  • this.element element
  • this.next next
  • this.previous previous

13
????????????????? LinkedList
  • public class LinkedListltEgt extends
    AbstractSequentialListltEgt, implements ListltEgt,
    java.lang.Cloneable, java.io.Serializable
  • private transient int size 0
    //??????????????????
  • private transient EntryltEgt header new
    EntryltEgt(null,null,null)

????????? Entry object ??????? ??????????????????
??????????????????????????
??????????????????????????????????????????????????
????????????????
14
?????????????
  • public LinkedList()
  • header.next header.previous header
  • public LinkedList(Collection c)
  • this()
  • addAll(c)

15
????????????????????
  • LinkedListltStringgt name new LinkedListltStringgt()

?????? circular linked list
name.add(Rana)
16
????????????????????(2)
  • name.add(0, Moo)

????????????????????????????????? ???????????
(???????????????????? index ??????????????
????????????????)
17
add(int index, E element)
  • ?????????????????? index ???? ????????????????????
    ????? index lt size/2 ??????????????????????????
    ?????????
  • ????? private method ??????? entry
  • ????????????????????????????????????????
  • ????? private method ??????? addBefore

18
????? addBefore
  • private EntryltEgt addBefore(E element, EntryltEgt e)
  • ??? ???????????????? ?????? element ????
    ????????????????? e (????????????????????????????)
  • ???????? ?????????????????????????????????????????
    ?????? ?????? element ?????????
  • //??????????? newEntry ???????? e
    (??????????????????????)
  • EntryltEgt newEntry new EntryltEgt(element,
    e, e.previous)
  • //???????????????
  • newEntry.previous.next newEntry
  • newEntry.next.previous newEntry
  • size
  • modCount
  • return newEntry

19
??????? add(int index, E element)
  • public void add(int index, E element)
  • if(index size)
  • addBefore(element, header)
  • else
  • addBefore(element, entry(index))

20
???????????????????? LinkedList (jdk 1.4 )
  • public Object getFirst()
  • if (size0)
  • throw new NoSuchElementException()
  • return header.next.element
  • public Object getLast()
  • if (size0)
  • throw new NoSuchElementException()
  • return header.previous.element

21
  • public Object removeFirst()
  • Object first header.next.element
  • remove(header.next)
  • return first
  • public Object removeLast()
  • Object last header.previous.element
  • remove(header.previous)
  • return last

22
  • public void addFirst(Object o)
  • addBefore(o, header.next)
  • public void addLast(Object o)
  • addBefore(o, header)
  • public boolean contains(Object o)
  • return indexOf(o) ! -1

23
  • public int size()
  • return size
  • public boolean add(Object o)
  • addBefore(o, header)
  • return true
  • public boolean addAll(Collection c)
  • return addAll(size, c)

24
  • public boolean remove(Object o)
  • if (onull)
  • for (Entry e header.next e !
    header e e.next)
  • if (e.elementnull)
  • remove(e)
  • return true
  • else
  • for (Entry e header.next e !
    header e e.next)
  • if (o.equals(e.element))
  • remove(e)
  • return true
  • return false

????????????????????????????
25
  • public boolean addAll(int index, Collection c)
  • int numNew c.size()
  • if (numNew0)
  • return false
  • modCount
  • Entry successor (indexsize ? header
    entry(index))
  • Entry predecessor successor.previous
  • Iterator it c.iterator()
  • for (int i0 iltnumNew i)
  • Entry e new Entry(it.next(),
    successor, predecessor)
  • predecessor.next e
  • predecessor e
  • successor.previous predecessor
  • size numNew
  • return true

26
  • public void clear()
  • modCount
  • header.next header.previous header
  • size 0
  • public Object get(int index)
  • return entry(index).element
  • public Object set(int index, Object element)
  • Entry e entry(index)
  • Object oldVal e.element
  • e.element element
  • return oldVal

27
  • public Object remove(int index)
  • Entry e entry(index)
  • remove(e)
  • return e.element
  • private Entry entry(int index)
  • if (index lt 0 index gt size)
  • throw new IndexOutOfBoundsException("I
    ndex "index

  • ", Size "size)
  • Entry e header
  • if (index lt (size gtgt 1))
  • for (int i 0 i lt index i)
  • e e.next
  • else
  • for (int i size i gt index i--)
  • e e.previous
  • return e

28
  • public int indexOf(Object o)
  • int index 0
  • if (onull)
  • for (Entry e header.next e !
    header e e.next)
  • if (e.elementnull)
  • return index
  • index
  • else
  • for (Entry e header.next e !
    header e e.next)
  • if (o.equals(e.element))
  • return index
  • index
  • return -1

29
  • public int lastIndexOf(Object o)
  • int index size
  • if (onull)
  • for (Entry e header.previous e !
    header e e.previous)
  • index--
  • if (e.elementnull)
  • return index
  • else
  • for (Entry e header.previous e !
    header e e.previous)
  • index--
  • if (o.equals(e.element))
  • return index
  • return -1

30
??????????????? LinkedList ??????
  • ??????????? ListItr ???????????????? ListIterator
    interface
  • ListItr ???? private class ????? LinkedList
  • ???????????????????????????????????????????
  • ??????????????????????????????????????

31
???????????????????????????????? ListIterator
  • public ListIteratorltEgt listIterator()
  • ???????? ListIterator ????????????????????????????
    ?
  • ????????
  • ListIteratorltStringgt itr1 animal.listIterator()
  • public ListIteratorltEgt listIterator(final int
    index)
  • ???????? ListIterator ????????????????????????????
    index
  • ??????????????? O(n)
  • ????????
  • ListIteratorltStringgt itr2 animals.listIterator(3
    )

32
  • public ListIterator listIterator(int index)
  • return new ListItr(index)
  • ListItr(int index)
  • if (index lt 0 index gt size)
  • throw new IndexOutOfBoundsException("Index
    "index
  • ", Size "size)
  • if (index lt (size gtgt 1))
  • next header.next
  • for (nextIndex0 nextIndexltindex
    nextIndex)
  • next next.next
  • else
  • next header
  • for (nextIndexsize nextIndexgtindex
    nextIndex--)
  • next next.previous

Jdk 1.4
33
For loop ?????????????????????
  • for(String s animals)
  • System.out.println(s)

34
????????????????? ListItr
  • private class ListItr implements ListIterator
  • private Entry lastReturned header
  • private Entry next
  • private int nextIndex
  • private int expectedModCount modCount
  • . ??????????

35
???????? ListItr ?????????????? ListIterator
???????????????????????
  • public boolean hasNext()
  • public E next()

36
  • public boolean hasNext()
  • return nextIndex ! size
  • public Object next()
  • checkForComodification()
  • if (nextIndex size)
  • throw new NoSuchElementException()
  • lastReturned next
  • next next.next
  • nextIndex
  • return lastReturned.element

37
  • public boolean hasPrevious()
  • public E previous()

38
public boolean hasPrevious() return
nextIndex ! 0 public Object previous()
if (nextIndex 0) throw new
NoSuchElementException() lastReturned
next next.previous nextIndex--
checkForComodification() return
lastReturned.element
39
??????????? previous ???????????(????????????)
???????????????????????????????? ??????????
previous ?????? ?????????????? dummy
node ?????????????????
?????????????????
dummy
40
????????????????????????????????
????????????????????????????????????? ?????????
previous ???????????????????? ????????????????????
????????????
??????????????????????????????????????????????????
?????????
Dummy node ???????????
  • ListIterator itr animals.listIterator(animals.si
    ze())
  • while (itr.hasPrevious( ))
  • itr.previous( )

41
??????????????????????????????(1)
  • public void add(E element)
  • ??????????????????????????????????????????????????
    next() (?????)
  • ??????????????????????????????????????????????????
    ?? previous() ???? (?????)
  • ???????????????????????? ?????????????????????????
    ???????????????????????????????
  • ????????????????????????????????????????????
    next() ???? previous() ??

42
??????????? add ????????????????
  • ?????????????????????????? ??? ??? ?? ???
    ????????????????? ?? ?????????????????????
    ???????????????
  • ????????????
  • ListIteratorltStringgt itr mylist.listIterator()
  • while(itr.hasNext())
  • itr.next()
  • itr.add(??)

??????????????
43
????????????????????????????
itr
???
??
???
???
????????????
?????????????????????????
itr
???
??
???
???
??????? add ?? ????????????????? ???????????????
????? next() ??? previous()
itr
????????????????
???
??
???
???
??
44
(No Transcript)
45
??????????????????????????????(2)
  • public void remove()
  • ????????????????????????????????? next() ????
    previous() ???????????
  • ?????????????????? next() ???? previous()?????????
    ???????????????
  • ???????????????????????? ListIterator.add()
    ??????????????? next() ???? previous()
    ???????????

46
????????????????????????????????????????????
  • ????????????????????? ????????????????????????????
    ????????????????????????????????
  • ????????????
  • ListIteratorltStringgt itr mylist.listIterator(1)
  • while(itr.hasNext())
  • itr.next()
  • itr.remove()
  • if(itr.hasNext())
  • itr.next()

47
???????????????????????????? 1
???
??
???
???
??
??
??
??
??????? ???? itr.next()
?????????? ?? ??????????????????????????
???
??
???
???
??
??
??
??
Itr.remove() ????????????? ??
?????????????????????????
???
??
???
???
??
??
??
????????????????????? if
?????????? ??? ??????????????????????????
???
??
???
???
??
??
??
48
??????????? ?? next() ???
?????????? ?? ??????????????????????????
???
??
???
???
??
??
??
Itr.remove() ????????????? ??
?????????????????????????
???
??
???
???
??
??
???????????????????? ?? ??????????????
49
??????????? ???????
  • ???????????????????????????????? ??
    ?????????????????? ??????????? ???????????????????
    ???
  • ????????????????????

50
  • ListIteratorltStringgt itr mylist.listIterator()
  • while(itr.hasNext())
  • if(itr.next().equals(??))
  • itr.remove()

51
??????????????????????????????
?????????????????????????????
???
??
???
???
??
??
??
??
????????????????????? if ????????????????????????
???????????? ???????????? ????????????????????????
????????? ??
???
??
???
???
??
??
??
??
?????????? ?? ?????????????????????????? ???????
?????????????????? ????????? itr.remove() ???
????????? ?? ????????????
???
??
???
???
??
??
??
???????????????????????? ????????????? ??
?????????????????????????
52
??????????????????????????????(3)
  • public void set(E o)
  • ??? o ????????????????????????????????????????????
    ???????? next() ???? previous()
  • ?????????????????????????????????
    ListIterator.add() ??? ListIterator.remove()
    ??????????????? next() ???? previous()
    ???????????

53
?????????????? set
  • ????????????????????????? ?? ???????????? ???
  • ListIteratorltStringgt itr mylist.listIterator()
  • while(itr.hasNext())
  • if(itr.next().equals(??))
  • itr.set(???)

54
???????????? Line editor
  • ????????????????????????????? (???????????????????
    ????????????????????????????????????????????)
  • ???????????????????????????????? 75 ???
  • ???????????????????????????? 0
  • ??????????????????????
  • ???????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ????????

55
?????? Insert
???????? current line
  • Insert
  • ???????????????????????????????????????
  • ?????????????????????????

??????????????????????????????????????? ??????????
??????????????? gt
???? current line ????
56
????? current line
??????? ??????????????????????? ??????????????????
???????? ???????????????????????? gt???????????????
??????????????
??????? ??????????????????????? ??????????????????
???????? ???????????????????????? ?????????
???????????? ???????? ?????????????????????????? gt
?????????????????????????????
Insert ????????? ???????????? ???????? ??????????
????????????????
57
?????? Delete m n
  • ???????????????????????? m ???????????? n
    (???????????? m ??? n ????)
  • Current line ?????????????????????????????????????
    ??????
  • ????? current line ???????????????????????????????
    ??????????????
  • ???????????????????????? current line
    ??????????????????????
  • ?? error message ??????????????
  • m gt n
  • m lt 0
  • n gt ??????????????????????
  • m ??? n ?????????????????????????

58
??????? ??????????????????????? ??????????????????
???????? gt???????????????????????? ?????????
???????????? ???????? ?????????????????????????? ?
????????????????????????????
Delete 1 3
??????? ??????????????????????? gt?????????????????
????????? ?????????????????????????????
59
??????? ??????????????????????? gt?????????????????
????????? ?????????????????????????????
Delete 2 2
??????? ??????????????????????? ??????????????????
???????? gt
Current line ???????????????????????????????? ????
?????????????
60
?????? Line m
?? error message ????????????????????? ???????????
? 0 ????????? m ????????????????
  • ???????????? m ???????? current line

????????? ??????????????????? ???????????????????
??????? ??????????????????????????? ??????????????
???????????? ???????????????????????? gt???????????
???????????????? ???????????????????????????
gt????????? ??????????????????? ??????????????????
???????? ??????????????????????????? ?????????????
????????????? ???????????????????????? gt??????????
????????????????? ???????????????????????????
Line 0
61
?????? Done
  • ???????????????????????????? ?????????????????????
    ????????????????????????

62
??????????????????
  • ?????????????????????????????????????????????
  • ????????????????????????????????????????????????
  • ????????????????????????????????
  • ??????????????????????????
  • ?????????? ???????????????????????
    ????????????????????

63
??????????????????? Editor
  • public Editor()
  • ????????????? ??????????????????????????????
  • public String interpret(String s)
  • ???????????????????????????? ????????????????
    ???????????? ??????????????????
  • ????????????????????? s ?????????? ???????? null
    ??? s ???????????
  • throw RuntimeException ??? s ?????????????????????
    ?? ?????????????????????????????????

64
  • protected void insert(String s)
  • ??? s ???????????????????????????????? current
    line
  • throw RuntimeException ??? s ?????????????????????
    MAX_LINE_LENGTH
  • protected void delete(int m, int n)
  • ??????????? m ??? n ????????????????? Delete
  • Throw RuntimeException ????????????????? Delete
  • protected tryToDelete(StringTokenizer tokens)
  • ??????????????????????????????????????????????????
    ???????????????????
  • ???????????????? delete ?????

65
  • protected void line(int m)
  • ?????????????????? current line ??????? m
  • ??????????????????????????? Line
  • protected tryToSetLine(StringTokenizer tokens)
  • ??????????????????????????????????????????????????
    ?????????????????????
  • ????????????????? line ???
  • protected String done()
  • ?????????????????????????????????????????

66
??????????????
  • ?????????????????
  • text ????????????????????????????????????????????
    ???
  • current ????????????????????? ???????? current
    line ???????
  • inserting ???? boolean ??????????????????????????
    ? Insert ???????

67
????????????? ???????????????????
  • public static final char COMMAND_START
  • public static final String INSERT_COMMAND
    Insert
  • public static final String DELETE_COMMAND
    Delete
  • public static final String LINE_COMMAND
    Line
  • public static final String DONE_COMMAND
    Done
  • public static final String BAD_LINE_MESSAGE
  • Error ??????????????????? COMMAND_START
    \n
  • public static final String BAD_COMMAND_MESSAGE
  • Error ????????????????????? \n
  • public static final String INTEGER_NEEDED
  • Error ????????????????????????? ???
    ???????????????? \n
  • public static final String TWO_INTEGERS_NEEDED
  • Error ????????????????????????? ?????????
    ???????? ?????????????????? \n

68
  • public static final String FIRST_GREATER
  • Error ????????????????????????????????????????
    ???????? \n
  • public static final String FIRST_LESS_THAN_ZERO
  • Error ???????????????????????????? 0 \n
  • public static final String SECOND_TOO_LARGE
  • Error ????????????????????????????????????????
    ??????? \n
  • public static final String M_LESS_THAN_ZERO
  • Error ??????????????????? 0 \n
  • public static final String M_TOO_LARGE
  • Error ???????????????????????? \n
  • public static final String LINE_TOO_LONG
  • Error ????????????????????????????????????????
    ??? \n
  • public static final int MAX_LINE_LENGTH 75
  • protected LinkedListltStringgt text
  • protected ListIteratorltStringgt current
  • protected boolean inserting

69
?????????????????
  • public Editor()
  • text new LinkedListltStringgt()
  • current text.listIterator()
    //????????????????????????
  • //
    ???????????
  • inserting false

70
  • public String interpret(String s)
  • StringTokenizer tokens new StringTokenizer(s)
  • String command
  • if(s.charAt(0) ! COMMAND_START)
    //???????????????????? ????????????????????????
  • if(inserting) // ?????????????????????????????
    insert
  • insert(s) //??????????????????????????????????
    ?????????????????? ????????????
  • else
  • throw new RuntimeException(BAD_LINE_MESSAGE)
  • else //??????????????????????????????????
  • command tokens.nextToken()
  • if(command.equals(INSERT_COMMAND))
  • inserting true
  • else // ?????????????????????? insert
  • inserting false
  • if(command.equals(DELETE_COMMAND))
  • tryToDelete(tokens)

71
  • else if(command.equals(LINE_COMMAND))
  • tryToSetLine(tokens)
  • else if(command.equals(DONE_COMMAND))
  • return done()
  • else
  • throw new
    RuntimeException(BAD_COMMAND_MESSAGE)
  • //??????????????? insert
  • // ??????
  • return null
  • // ???????

72
  • protected void insert(String s)
  • if(s.length() gt MAX_LINE_LENGTH)
  • throw new RuntimeException(LINE_TOO_LONG)
  • current.add(s) // ???????????????????????
  • protected void tryToDelete(StringTokenizer
    tokens)
  • try
  • int m Integer.parseInt(tokens.nextToken())
  • int n Integer.parseInt(tokens.nextToken())
  • delete(m,n)
  • catch(RuntimeException e)
  • throw new RuntimeException(TWO_INTEGERS_NEEDED)

73
  • protected void delete(int m, int n)
  • if(mgtn)
  • throw new RuntimeException(FIRST_GREATER)
  • if(mlt0)
  • throw new RuntimeException(FIRST_LESS_THAN_ZERO)
  • if(ngttext.size())
  • throw new RuntimeException(SECOND_TOO_LARGE)
  • current text.listIterator(m)
  • for(int im iltn i) // ??????????????????
  • current.next()
  • current.remove()
  • // current ??????????????????????????????? n
    ????

Worst time n
???? lt n
Big O O(n)
74
  • protected void tryToSetLine(StringTokenizer
    tokens)
  • try
  • int m Integer.parseInt(tokens.nextToken())
  • line(m)
  • catch(RuntimeException e)
  • throw new RuntimeException(INTEGER_NEEDED)
  • protected void line(int m)
  • if(mlt0)
  • throw new RuntimeException(M_LESS_THAN_ZERO)
  • if(m gt text.size())
  • throw new RuntimeException(M_TOO_LARGE)
  • current text.listIterator(m) //
    ?????????????????????????????

Big O O(n)
Worst time n
75
?????????????????????? equals() ???
?????????????????????????????????????????????????
  • protected String done()
  • final String FINAL_TEXT_MESSAGE
  • \n\n ???????????????????? \n
  • String s FINAL_TEXT_MESSAGE
  • ListIterator itr text.listIterator()
  • while(itr.hasNext())
  • if(itr.nextIndex() current.nextIndex())
    //??????????????????????????????????????
  • s s gt itr.next() \n
  • else
  • s s itr.next() \n
  • if(!current.hasNext()) //??? current
    ??????????????????????????? ??????????????????????
    ???
  • s s gt \n
  • return s

???? n
Big O O(n)
76
Driver class
  • ?????????????? ??????????????????????????????????
    ???????????????????????? ???????? ????????????????
Write a Comment
User Comments (0)
About PowerShow.com