singly linked lists Sept. 18, 2017 1 Recall last lecture: Java - - PowerPoint PPT Presentation

singly linked lists
SMART_READER_LITE
LIVE PREVIEW

singly linked lists Sept. 18, 2017 1 Recall last lecture: Java - - PowerPoint PPT Presentation

COMP 250 Lecture 5 singly linked lists Sept. 18, 2017 1 Recall last lecture: Java array array array array of int of Shape (unspecified objects type) 34 657 -232 -823 23 1192 0 null 0 null I have drawn each of these as array


slide-1
SLIDE 1

1

COMP 250

Lecture 5

singly linked lists

  • Sept. 18, 2017
slide-2
SLIDE 2

Recall last lecture: Java array

null null 34 657

  • 232
  • 823

23 1192

I have drawn each of these as array lists.

array

  • f Shape
  • bjects

array of int

2

array (unspecified type)

slide-3
SLIDE 3

Java ArrayList class

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

It uses an array as the underlying data structure It grows the array (by 50%, not 100%) when the array is full and a new element is added. You don’t use the usual array notation a[ ]. Instead, use get() and set() and other methods.

3

slide-4
SLIDE 4

Java generic type

An array of what? ArrayList<T> Example:

ArrayList< Shape > shape = new ArrayList< Shape >(); // initializes the array length (capacity) to 10 ArrayList< Shape > shape = new ArrayList< Shape >( 23 ); // initializes the array length to 23

4

slide-5
SLIDE 5

Java ArrayList object

null null

Has private field that holds the number of elements in the list (size). Has a private field that references an array

  • bject.

6 These Shape objects do not belong to the ArrayList object. Rather they are referenced by it.

5

a (private)

slide-6
SLIDE 6

Lists

  • array list
  • singly linked list (today)
  • doubly linked list (next lecture)

:

6

slide-7
SLIDE 7

7

array list linked list

size = 4 “nodes”

null null null null

slide-8
SLIDE 8

8

array list linked list

null null null null

Array slots are in consecutive locations (addresses) in memory, but objects can be anywhere. Linked list “nodes” and

  • bjects can be anywhere

in memory.

slide-9
SLIDE 9

9

Singly linked list node (“S” for singly)

class SNode<E> { SNode<E> next; E element; : }

e.g. E might be Shape next element

slide-10
SLIDE 10

10

A linked list consists of a sequence of nodes, along with a reference to the first (head) and last (tail) node.

head tail

slide-11
SLIDE 11

11

class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : private class SNode<E> { // inner class SNode<E> next; E element; : } }

slide-12
SLIDE 12

Linked list operations

  • addFirst ( e )
  • removeFirst( )
  • addLast ( e )
  • removeLast( )
  • ……. many other list operations

12

slide-13
SLIDE 13

13

addFirst ( ) BEFORE AFTER

head tail head tail

slide-14
SLIDE 14

14

construct newNode newNode.element = e newNode.next = head

addFirst ( e ) pseudocode

head newNode next element

e etc

slide-15
SLIDE 15

15

construct newNode newNode.element = e newNode.next = head // edge case if head == null tail = newNode head = newNode size = size+1

addFirst ( e ) pseudocode

head newNode next element

e etc

head newNode next element

e etc

BEFORE AFTER

slide-16
SLIDE 16

16

removeFirst ( ) BEFORE AFTER

head tail head tail

slide-17
SLIDE 17

17

tmp = head

removeFirst ( ) pseudocode

head tmp next element

slide-18
SLIDE 18

18

tmp = head head = head.next tmp.next = null size = size – 1

removeFirst ( ) pseudocode

head tmp next element head tmp next element null

BEFORE AFTER

slide-19
SLIDE 19

19

tmp = head if (size == 0) throw exception head = head.next tmp.next = null size = size – 1 if (size == 0) // size was 1 tail = null

removeFirst() edge cases (size is 0 or 1)

head tmp next element head tmp next element null

BEFORE AFTER

null

slide-20
SLIDE 20

Worse Case Time Complexity

(N = size)

20

array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 )

slide-21
SLIDE 21

Worse Case Time Complexity (N = size)

21

array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 ) addLast O( 1 )* ? removeLast O( 1 ) ?

*if array is not full

slide-22
SLIDE 22

22

addLast ( ) BEFORE AFTER

head tail head tail

slide-23
SLIDE 23

23

newNode = construct a new node newNode.element = the new list element tail.next = newNode // … and then after what // figure shows we do: tail = tail.next size = size+1

addLast ( )

newNode next element tail

:

slide-24
SLIDE 24

24

removeLast ( ) BEFORE AFTER

head tail head tail

Problem: we have no direct way to access the node before tail.

slide-25
SLIDE 25

25

removeLast ( )

if (head == tail){ head = null tail = null } else { tmp = head while (tmp.next != tail) tmp = tmp.next tail = tmp tail.next = null } size = size - 1 // to return the element, you need to do a bit more

head tmp tail next element

slide-26
SLIDE 26

Time Complexity

(N = list size)

26

array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 ) addLast O( 1 )* O( 1 ) removeLast O( 1 ) O( N )

*if array is not full

slide-27
SLIDE 27

27

class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : // various methods private class SNode<E> { // inner class SNode<E> next; E element; : } }

slide-28
SLIDE 28

28

class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : }

head size 4 tail SLinkedList

  • bject
slide-29
SLIDE 29

How many objects?

29

head size 4 tail SLinkedList

  • bject
slide-30
SLIDE 30

How many objects?

30

head size 4 tail SLinkedList

1 + 4 + 4 = 9

SNode Shape

slide-31
SLIDE 31

Announcements

  • When I make mistakes on slides/lecture notes/exercises,

please email me rather posting on discussion board.

(However, compare the date on your version with the one on the public web page. I may have already corrected it.)

  • Assignment 1 should be posted tomorrow (due in 2 weeks)
  • Quiz 1 on Monday, Sept 25 (lectures 1-2, 4-6). Online.
  • Coding tutorials on lists (coming soon)

31