Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev - - PowerPoint PPT Presentation

java linkedlist
SMART_READER_LITE
LIVE PREVIEW

Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev - - PowerPoint PPT Presentation

COMP 250 Lecture 5 doubly linked lists Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev element head Each node has a reference to the next node and to the previous node. tail 2 class DNode< E > { DNode< E


slide-1
SLIDE 1

1

COMP 250

Lecture 5

doubly linked lists Java LinkedList

  • Sept. 16, 2016
slide-2
SLIDE 2

Doubly linked lists

2

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

head tail next prev element

slide-3
SLIDE 3

class DNode< E > { DNode< E > next; Dnode< E > prev; E element; // constructor DNode( E e ) { element = e; prev = null; next = null; } }

3

next element prev

slide-4
SLIDE 4

4

Motivation: 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. This took time

O(N) where N is the size

  • f the list.
slide-5
SLIDE 5

5

removeLast(){ tail = tail.prev tail.next = null size = size – 1 : }

For a doubly linked list, removing the last element is much faster.

head tail

next prev element

slide-6
SLIDE 6

Time Complexity (N = list size)

6

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-7
SLIDE 7

List Operations

get(i) set(i,e) add(i,e) remove(i) :

7

For a linked list, many operations require access to node i. The “edge cases” (i = 0, i = size – 1) usually require extra code, which can lead to coding errors.

head tail

null null

slide-8
SLIDE 8

Common linked list trick: avoid edge cases with “dummy nodes”

8

dummyHead dummyTail null null null null

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

slide-9
SLIDE 9

9

class DLinkedList<E>{

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;

}

dummyHead dummyTail null null null null

slide-10
SLIDE 10

10

DLinkedList< Shape >

  • bject

dummyHead size 4 dummyTail

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

slide-11
SLIDE 11

remove( i ) { // recall end of lecture 3 on arrays

node = getNode( i ) // next slide node.next.prev = node.prev node.prev.next = node.next size = size - 1 }

11

i – 1 i i + 1

BEFORE AFTER

node

next prev element next prev element

slide-12
SLIDE 12

getNode( i ) {

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

[SLIDE WAS ADDED AFTER LECTURE]

12

slide-13
SLIDE 13

getNode( i ) {

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 (half the time)…

13

slide-14
SLIDE 14

Time Complexity (N = list size)

14

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-15
SLIDE 15

Time Complexity (N = list size)

15

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-16
SLIDE 16

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

16

slide-17
SLIDE 17

17

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(..) for (k = 0; k < k < list.size(); k ++) list.get( k );

slide-18
SLIDE 18

18

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: 𝟐 + 𝟐 + 𝟐 + … . 𝟐 = 𝑶 𝑷( 𝑶 ) for (k = 0; k < k < list.size(); k ++) list.get( k );

I am omitting what I would do with this element since that’s not the point here e.g. I could print it.

slide-19
SLIDE 19

19

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: 𝟐 + 𝟐 + 𝟐 + … . 𝟐 = 𝑶 𝑷( 𝑶 ) for (k = 0; k < k < list.size(); k ++) // size == N list.get( k ); A: 𝟐 + 𝟑 + 𝟒 + … . 𝐎 =

𝑶 𝑶+𝟐 𝟑

𝑷( 𝑶𝟑)

Here I am assuming the first getNode(i) is used, which always starts at the head. See the Exercises for the expression when the more efficient getNode(i) method is used.

slide-20
SLIDE 20

ASIDE: Java ‘enhanced for loop’

for (k = 0; k < list.size(); k ++) ……. A more efficient way to iterate through elements in a LinkedList object is to use:

for (E e : list)

// ‘list’ references the LinkedList< E > object // Do something with each element e in list

20

slide-21
SLIDE 21

What about “Space Complexity” ?

21

null null

All three data structures use space O(N) for a list of N elements.

slide-22
SLIDE 22

Java terminology (time permitting)

  • method “signature”
  • name
  • number and type of parameters,
  • return type
  • method “overloading”
  • add( int index, E element)
  • add( E element )
  • remove(E element)
  • remove(int i)

22

slide-23
SLIDE 23

Java terminology

What is method “overloading” vs. “overriding” ? Classes can “inherit” methods from other classes. Sometimes you do not want a class to inherit the method, however, and so you “override” it by writing a more suitable one. We will learn about inheritance formally at the end

  • f the course…..

23

slide-24
SLIDE 24

Announcements

  • Assignment 1 posted (due in ~2 weeks)
  • Exercises for singly linked lists (practice coding)
  • Eclipse (IDE) tutorials next week.

Goodbye DrJava ! Hello Eclipse !

24

slide-25
SLIDE 25

I asked the TA’s which IDE they use:

  • Eclipse or sublime
  • jetbrains, IntelliJ
  • Eclipse
  • Eclipse, also IntelliJ
  • Eclipse
  • Simple text editor / vim + command line. (However I

had to use Eclipse and DrJava in the past.)

  • Eclipse, but I like using text editor + command line for small things.
  • Eclipse (netbeans for GUIs)

25