linked lists + exam return · linked list • insert “l” after “e” • find the location...
TRANSCRIPT
2/24/14
1
Linked Lists + Exam Return
List • ArrayList
2/24/14
2
List • ArrayList
List • ArrayList
• List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • and many more!
2/24/14
3
List • ArrayList
• List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • and many more!
List • ArrayList
• Array – a data structure in which elements may be located by index
2/24/14
4
List • ArrayList implements List • inherits all methods from List • ArrayList implements List’s methods using an array
ArrayList<String> array = new ArrayList<String>();!!List<String> array = new ArrayList<String>();!
List • What are ArrayLists good for?
• Constant time O(1) • size • isEmpty • get • set • add*
• Linear time O(N) • everything else
* add is ~O(1)
2/24/14
5
C o m p s c i
• list.add(“C”); • list.add(“o”); • list.add(“m”); • list.add(“p”); • list.add(“s”); • list.add(“c”); • list.add(“i”);
List
List
C o m p s c i
• How would you • add to the end, beginning, middle? • remove an element?
2/24/14
6
Linked List • LinkedList • List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • an many more!
Linked List • LinkedList • different implementation of List from ArrayList
h e l o
myHead myTail
2/24/14
7
Linked List • LinkedList • Nodes • data • pointer to the next node
• Pointer to beginning and (sometimes) end
h e l o
myHead myTail
Linked List • insert “l” after “e”
C o m p h e l o
myHead myTail
2/24/14
8
Linked List • insert “l” after “e” • find the location
C o m p h e l o
myHead myTail
Linked List • insert “l” after “e” • find the location
C o m p h e l o
myHead myTail
2/24/14
9
Linked List • insert “l” after “e” • find the location • create a new node
C o m p h e l o
myHead myTail l
Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’
C o m p h e l o
myHead myTail l
myNext
2/24/14
10
Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to point to newNode
C o m p h e l o
myHead myTail l myNext
Linked List • Singly linked list
• doubly linked list
C o m p h e l o
C o m p h e l o
2/24/14
11
Linked List • LinkedList vs. ArrayList
get add at end add in middle
LinkedList O(N) O(1) Search time + O(1)
ArrayList O(1) O(1) but O(N) worst case
Shift elements
Bad Joke • What did the ArrayList say to the LinkedList?
2/24/14
12
Next time • Linked lists and code!
Exam
0
10
20
30
40
50
60
70
80
C B A
# of S
tude
nts
Letter Grade
2/24/14
13
Exam • Average – 71 ±11 points • Median – 74.5 point
• Highest grade – 85 points
Exam • Grade correction requests • Write ONLY the problem section and number on
the front page • If there is a summation error, add “SUM” after the
problem number
• Hand you exam back to us BEFORE you leave class