Linked Lists - PowerPoint PPT Presentation

1 / 154
About This Presentation
Title:

Linked Lists

Description:

Chapter 7 Linked Lists – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 155
Provided by: William1404
Learn more at: http://www.mscs.mu.edu
Category:

less

Transcript and Presenter's Notes

Title: Linked Lists


1
Chapter 7
Linked Lists
2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
doe
ray
me
null
22
(No Transcript)
23
doe
ray
me
null
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
myLinked.head
ray
doe
me
null
29
(No Transcript)
30
(No Transcript)
31
myLinked.head
newEntry
null
null
ray
doe
me
null
32
(No Transcript)
33
myLinked.head
newEntry
tea
null
ray
doe
me
null
34
(No Transcript)
35
newEntry
myLinked.head
tea
ray
doe
me
null
36
(No Transcript)
37
newEntry
myLinked.head
tea
ray
doe
me
null
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
(No Transcript)
49
An iterator is an object that enables a user to
loop through a collection without accessing the
collections fields.
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
53
(No Transcript)
54
head
element next
ray
doe
me
null
next
55
head
element next
ray
doe
me
null
next
56
(No Transcript)
57
(No Transcript)
58
(No Transcript)
59
(No Transcript)
60
(No Transcript)
61
(No Transcript)
62
(No Transcript)
63
(No Transcript)
64
(No Transcript)
65
(No Transcript)
66
(No Transcript)
67
(No Transcript)
68
(No Transcript)
69
(No Transcript)
70
(No Transcript)
71
(No Transcript)
72
(No Transcript)
73
(No Transcript)
74
(No Transcript)
75
(No Transcript)
76
(No Transcript)
77
(No Transcript)
78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
To print myList in reverse order itr
myList.listIterator (myList.size( )) while
(itr.hasPrevious( )) System.out.println
(itr.previous( ))
82
Another ListIterator method / Inserts an
element into the LinkedList in front of the
element that would be returned by next() and
in back of the element that would be returned by
previous(). / public void add (E
element)
83
LinkedListltDoublegtmyList new LinkedListltDoublegt(
) myList.add (0.0) myList.add
(1.0) ListIteratorltDoublegt itr
myList.listIterator() itr.next() itr.add (0.8)
84
The LinkedList would now have 0.0, 0.8, 1.0
85
Another ListIterator method / Removes the
last returned element. / public void
remove()
86
(No Transcript)
87
Users guide for choosing ArrayList or
LinkedList If the application entails a lot of
accessing and/or modifying elements at widely
varying indexes, an ArrayList will be much
faster than a LinkedList. If a large part of the
application consists of iterating through a list
and making insertions and/or removals during the
iterations, a LinkedList will be much faster
than an ArrayList.
88
Fields and implementation of the LinkedList
class There are two fields  
  private
transient int size 0    private transient
EntryltEgt header new Entry (null, null,
null)
89
Fields and implementation of the LinkedList
class There are two fields  
Field not
saved if object is serialized   private
transient int size 0    private transient
Entry header new Entry (null, null,
null)
90
private static class EntryltEgt E element
EntryltEgt next EntryltEgt previous Entry (ltEgt
element, EntryltEgt next, EntryltEgt previous)  
this.element element this.next
next this.previous previous //
constructor // class Entry
91
public LinkedList() header.next
header.previous header For
example, ListltStringgt names new
LinkedListltStringgt()
92
Empty LinkedList
93
LinkedList with on element
94
LinkedList with two elements
95
The significance of the header entry is that
there is always an entry in back of an in front
of any entry. That simplifies insertions and
removals.
96
Details of insertion into a LinkedList
  • names.add (1, Don)
  • The method heading for this method is
  • public void add (int index, E element)
  • This add method calls
  • addBefore (element, entry (index))
  • and the heading for addBefore is
  • private EntryltEgt addBefore (E element, EntryltEgt
    e)

97
  • So Don will be inserted in front of the entry
    at index 1.

98
Start
99
Step 1 Code // insert newEntry in front of e
EntryltEgt newEntry new
Entry(element, e, e.previous)
100
Step 1
101
Step 2 // make newEntry follow its
predecessor newEntry.previous.next newEntry

102
Step 2
103
Step 3 Code // make newEntry precede its
successor, e newEntry.next.previous newEntry

104
Step 3
105
The same strategy works for names.add (0,
Kalena) // inserts between header entry and
entry at // index 0 names.add (names.size(),
Hana) // inserts between entry at index size()
1 // and header entry
106
Group exercise Determine the output from the
following
107
LinkedListltStringgt myList new
LinkedListltStringgt() myList.add ("a")
myList.add ("b") myList.add ("c")
myList.add ("d") myList.add ("e")
myList.add (2, "r") myList.remove (4)
ListIteratorltStringgt itr myList.listIterator
(3) itr.previous() itr.add ("x")
itr.next() itr.remove() itr
myList.listIterator (myList.size()) while
(itr.hasPrevious())
System.out.println (itr.previous())
108
(No Transcript)
109
  • Line editor A program that manipulates test,
    line by line.
  • First line line 0
  • One line is designated the current line.
  • Each editing command begins with .

110
(No Transcript)
111
(No Transcript)
112
(No Transcript)
113
(No Transcript)
114
(No Transcript)
115
(No Transcript)
116
(No Transcript)
117
(No Transcript)
118
(No Transcript)
119
(No Transcript)
120
(No Transcript)
121
(No Transcript)
122
(No Transcript)
123
(No Transcript)
124
(No Transcript)
125
(No Transcript)
126
(No Transcript)
127
(No Transcript)
128
(No Transcript)
129
(No Transcript)
130
(No Transcript)
131
(No Transcript)
132
(No Transcript)
133
(No Transcript)
134
(No Transcript)
135
(No Transcript)
136
(No Transcript)
137
(No Transcript)
138
(No Transcript)
139
(No Transcript)
140
(No Transcript)
141
(No Transcript)
142
(No Transcript)
143
(No Transcript)
144
(No Transcript)
145
protected void line (int m) if (m lt
0) throw new RuntimeException
(M_LESS_THAN_ZERO) if (m gt
text.size()) throw new
RuntimeException (M_TOO_LARGE)
current text.listIterator (m) // method line
146
(No Transcript)
147
(No Transcript)
148
(No Transcript)
149
(No Transcript)
150
(No Transcript)
151
(No Transcript)
152
(No Transcript)
153
(No Transcript)
154
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com