doubly linked lists Sept. 20/21, 2017 1 Singly linked list head - - PowerPoint PPT Presentation

doubly linked lists
SMART_READER_LITE
LIVE PREVIEW

doubly linked lists Sept. 20/21, 2017 1 Singly linked list head - - PowerPoint PPT Presentation

COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element head Each node has a reference to the next node and to the previous node . tail 3 class DNode< E


slide-1
SLIDE 1

1

COMP 250

Lecture 6

doubly linked lists

  • Sept. 20/21, 2017
slide-2
SLIDE 2

Singly linked list

2

head tail

slide-3
SLIDE 3

Doubly linked list

3

Each node has a reference to the next node and to the previous node.

head tail next prev element

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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.

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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 )

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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 ?
slide-10
SLIDE 10

Avoid edge cases with “dummy nodes”

10

dummyHead dummyTail null null null null

i = 0 i = 1 i = 2 i = 3

slide-11
SLIDE 11

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

slide-12
SLIDE 12

12

DLinkedList< Shape >

  • bject

dummyHead size 4 dummyTail

Q: How many objects in total in this figure? A:

slide-13
SLIDE 13

13

DLinkedList< Shape >

  • bject

dummyHead size 4 dummyTail

Q: How many objects in total in this figure? A: 1 + 6 + 4 = 11

slide-14
SLIDE 14

E get( i ) {

node = getNode(i); // getNode() to be discussed next slide return node.element;

}

14

dummyHead dummyTail null null null

i = 0 i = 1 i = 2 i = 3

slide-15
SLIDE 15

getNode( i ) { // returns a DNode

// verify that 0 <= i < size (omitted) node = dummyHead.next for (k = 0; k < i ; k ++) node = node.next return node

}

15

dummyHead dummyTail null null null

i = 0 i = 1 i = 2 i = 3

slide-16
SLIDE 16

getNode( i ) { // returns a DNode

if ( i < size/2 ){ // iterate from head node = dummyHead.next for (k = 0; k < i; k ++) node = node.next } else{ // iterate from tail node = dummyTail.prev for ( k = size-1; k > i; k -- ) node = node.prev } return node }

More efficient getNode()… half the time

16

slide-17
SLIDE 17

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)

slide-18
SLIDE 18

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 ) ? ? ?

slide-19
SLIDE 19

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.

slide-20
SLIDE 20

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.
slide-21
SLIDE 21

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.

slide-22
SLIDE 22

Java LinkedList class

https://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

slide-23
SLIDE 23

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 constant list.addFirst( new E( …. ) );

slide-24
SLIDE 24

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 constant list.addFirst( new E( …. ) ); // or addLast(..) A: 𝟐 + 𝟐 + 𝟐 + … . 𝟐 = 𝑶 ⇒ 𝑷( 𝑶 ) where ‘1′ means constant.

slide-25
SLIDE 25

25

Q: What is the time complexity of the following ? : : for (k = 0; k < list.size(); k ++) // size == N list.get( k ); Assume here that getNode(i) always starts at the head.

slide-26
SLIDE 26

26

Q: What is the time complexity of the following ? : : for (k = 0; k < list.size(); k ++) // size == N list.get( k ); Assume here that getNode(i) always starts at the head. A: 𝟐 + 𝟑 + 𝟒 + … . 𝐎

slide-27
SLIDE 27

27

Q: What is the time complexity of the following ? : : for (k = 0; k < list.size(); k ++) // size == N list.get( k ); Assume here that getNode(i) always starts at the head. A: 𝟐 + 𝟑 + 𝟒 + … . 𝐎 =

𝑶 𝑶+𝟐 𝟑

⇒ 𝑷( 𝑶𝟑)

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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).

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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