linked lists + exam return · linked list • insert “l” after “e” • find the location...

13
2/24/14 1 Linked Lists + Exam Return List ArrayList

Upload: others

Post on 26-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

2/24/14  

1  

Linked Lists + Exam Return

List •  ArrayList

Page 2: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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!

Page 3: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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

Page 4: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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)  

Page 5: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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?

Page 6: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 7: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 8: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 9: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 10: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 11: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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?

Page 12: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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  

Page 13: Linked Lists + Exam Return · Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to

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