lecture 6 - mcgill cimlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · array list versus...
TRANSCRIPT
![Page 1: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/1.jpg)
1
COMP 250
Lecture 6
doubly linked lists
Sept. 20/21, 2017
![Page 2: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/2.jpg)
Singly linked list
2
head
tail
![Page 3: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/3.jpg)
Doubly linked list
3
Each node has a reference to the next node and to the previous node.
head
tail
next prev element
![Page 4: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/4.jpg)
class DNode< E > {
DNode< E > next;Dnode< E > prev;E element;
// constructor
DNode( E e ) { element = e;prev = null;next = null;
}
}
4
next element
prev
![Page 5: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/5.jpg)
5
Motivation for doubly linked lists:recall removeLast ( ) for singly linked lists
head
tmp
tail
next element
The only way to access the element before the tail was to loop through all elements from the head.
![Page 6: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/6.jpg)
6
removeLast(){
tail = tail.prev
tail.next.prev = null
tail.next = null
size = size – 1
:
}
You need to do more work to return it.
For a doubly linked list, removing the last element is much faster.
head
tail
next prev element
![Page 7: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/7.jpg)
Time Complexity (N = list size)
7
array list SLinkedList DLinkedList
addFirst O( N ) O( 1 ) O( 1 )
removeFirst O( N ) O( 1 ) O( 1 )
addLast O( 1 ) O( 1 ) O( 1 )
removeLast O( 1 ) O( N ) O( 1 )
![Page 8: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/8.jpg)
Other List Operations
:
get(i)
set(i,e)
add(i,e)
remove(i)
:
8
Many list operations require access to node i.
head
tail
null
null
![Page 9: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/9.jpg)
9
Suppose we want to access general node i in a linked list.
Two issues arise:
• Edge cases (i = 0, i = size – 1) require extra code. This is a pain and can lead to coding errors.
• How long does it take to access node i ?
![Page 10: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/10.jpg)
Avoid edge cases with “dummy nodes”
10
dummyHead
dummyTail null
null
null
null
i = 0
i = 1
i = 2
i = 3
![Page 11: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/11.jpg)
11
class DLinkedList<E>{ // Java code
DNode<E> dummyHead;DNode<E> dummyTail; int size;
:
// constructor
DLinkedList<E>(){dummyHead = new DNode<E>();dummyTail = new DNode<E>();dummyHead.next = dummyTail;dummyTail.prev = dummyHead;size = 0;
}
private class DNode<E>{ … }
}
dummyHead
dummyTail null
null
null
null
![Page 12: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/12.jpg)
12
DLinkedList< Shape >object
dummyHead
size4
dummyTail
Q: How many objects in total in this figure?
A:
![Page 13: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/13.jpg)
13
DLinkedList< Shape >object
dummyHead
size4
dummyTail
Q: How many objects in total in this figure?
A: 1 + 6 + 4 = 11
![Page 14: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/14.jpg)
E get( i ) {
node = getNode(i); // getNode() to be discussed next slidereturn node.element;
}
14
dummyHead
dummyTail null
null
null
i = 0
i = 1
i = 2
i = 3
![Page 15: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/15.jpg)
getNode( i ) { // returns a DNode
// verify that 0 <= i < size (omitted)
node = dummyHead.nextfor (k = 0; k < i ; k ++)
node = node.nextreturn node
}
15
dummyHead
dummyTail null
null
null
i = 0
i = 1
i = 2
i = 3
![Page 16: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/16.jpg)
getNode( i ) { // returns a DNode
if ( i < size/2 ){ // iterate from headnode = dummyHead.nextfor (k = 0; k < i; k ++)
node = node.next}
else{ // iterate from tailnode = dummyTail.prevfor ( k = size-1; k > i; k -- )
node = node.prev}
return node}
More efficient getNode()… half the time
16
![Page 17: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/17.jpg)
remove( i ) { node = getNode( i )
}
17
i – 1
i
i + 1
BEFORE AFTER
node
next prev element next prev element
Exercise (see online code; also reviewed in upcoming tutorial)
![Page 18: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/18.jpg)
Time Complexity (N = list size)
18
array list SLinkedList DLinkedList
addFirst O( N ) O( 1 ) O( 1 )
removeFirst O( N ) O( 1 ) O( 1 )
addLast O( 1 ) O( 1 ) O( 1 )
removeLast O( 1 ) O( N ) O( 1 )
remove( i ) ? ? ?
![Page 19: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/19.jpg)
Time Complexity in Worst Case(N = list size)
19
array list SLinkedList DLinkedList
addFirst O( N ) O( 1 ) O( 1 )
removeFirst O( N ) O( 1 ) O( 1 )
addLast O( 1 ) O( 1 ) O( 1 )
removeLast O( 1 ) O( N ) O( 1 )
remove( i ) O(N) O( N ) O( N )
As I will discuss that later, “O( )” ignores constant factors.
![Page 20: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/20.jpg)
Array list versus Linked List ?
20
Array lists and linked lists both take O(N) time to add or remove from an arbitrary position in the list.
In practice and when N is large, array lists are faster. But the reasons are subtle and have to do with how computer memory works, in particular, how caches exploit contiguous memory allocation. You will learn about that topic in COMP 273.
![Page 21: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/21.jpg)
Do you ever need Linked Lists ?
21
Yes. Even if you prefer ArrayLists, you still need to understand LinkedLists. Linked lists are special cases of a general and widely used data structure called a tree which we will be discussing extensively.
![Page 22: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/22.jpg)
Java LinkedList classhttps://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
It uses a doubly linked list as the underlying data structure.
It has some methods that ArrayList doesn’t have e.g.:
• addFirst()
• removeFirst()
• addLast()
• removeLast()
Why ?22
![Page 23: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/23.jpg)
23
Q: What is the time complexity of the following ?
LinkedList< E > list = new LinkedList< E >( ) ;
for (k = 0; k < N; k ++) // N is some constantlist.addFirst( new E( …. ) );
![Page 24: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/24.jpg)
24
Q: What is the time complexity of the following ?
LinkedList< E > list = new LinkedList< E >( ) ;
for (k = 0; k < N; k ++) // N is some constantlist.addFirst( new E( …. ) ); // or addLast(..)
A: 𝟏 + 𝟏 + 𝟏 + … . 𝟏 = 𝑵 ⇒ 𝑶( 𝑵 )
where ‘1′ means constant.
![Page 25: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/25.jpg)
25
Q: What is the time complexity of the following ?
::
for (k = 0; k < list.size(); k ++) // size == Nlist.get( k );
Assume here that getNode(i) always starts at the head.
![Page 26: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/26.jpg)
26
Q: What is the time complexity of the following ?
::
for (k = 0; k < list.size(); k ++) // size == Nlist.get( k );
Assume here that getNode(i) always starts at the head.
A: 𝟏 + 𝟐 + 𝟑 + … . 𝐍
![Page 27: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/27.jpg)
27
Q: What is the time complexity of the following ?
::
for (k = 0; k < list.size(); k ++) // size == Nlist.get( k );
Assume here that getNode(i) always starts at the head.
A: 𝟏 + 𝟐 + 𝟑 + … . 𝐍
=𝑵 𝑵+𝟏
𝟐⇒ 𝑶( 𝑵𝟐)
![Page 28: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/28.jpg)
ASIDE: Java ‘enhanced for loop’
A more efficient way to iterate through elements in a Java LinkedList is to use:
for (E e : list)
// ‘list’ references the LinkedList< E > object
// Do something with each element e in list
// But this is sometimes awkward. I don’t recommend it for
// Assignment 1.28
![Page 29: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/29.jpg)
What about “Space Complexity” ?
29
null
null
All three data structures use space O(N) for a list of size N.But linked lists use 2x (single) or 3x (double).
![Page 30: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/30.jpg)
Java terminology (time permitting)
• method “overloading” • add( int index, E element)• add( E element )
• remove(E element) • remove(int i)
• method “signature”• name• number and type of parameters, • return type
30
![Page 31: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/31.jpg)
Java terminology
Method “overriding” vs. “overloading” ?
Classes can “inherit” methods from other classes.
I will cover inheritance formally at the end of the course.
But sometimes you do not want a class to inherit a method. Instead, you “override” the method by writing a more suitable one which has the same signature.
31
![Page 32: Lecture 6 - McGill CIMlanger/250/6-doublylinkedlists-slides.pdf · 2017-09-22 · Array list versus Linked List ? 20 Array lists and linked lists both take O(N) time to add or remove](https://reader033.vdocuments.us/reader033/viewer/2022042313/5edcf180ad6a402d6667d7c6/html5/thumbnails/32.jpg)
Announcements
• Quiz 0 solutions posted
(Let me know if you have trouble viewing them)
• Assignment 1 posted (due on Tues. Oct. 3)• TA office hours posted and will be updated
• Tutorials for linked lists• Assumes you have attended/read up to today
• You need to sign up.
32