module 9: list structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 introduction • recall...

222
Module 9: List Structures Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington University in Saint Louis Thanks to Alan Waldman for comments that improved these slides *

Upload: others

Post on 16-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

Module 9: List Structures

Copyright Ron K. Cytron 2013

Ron K. Cytron *

Prepared for 2u

Semester Online

Department of Computer Science and Engineering Washington University in Saint Louis

Thanks to Alan Waldman for comments that improved these slides

*

Page 2: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.0 Introduction

•  Recall methods –  One method can call another –  A method can even call itself – this is recursion

•  Recall objects –  One object can reference (point to) other objects –  An object can even reference itself, a form of

recursion –  More commonly, an object references another object

of the same time •  These are also called recursive data structures

–  We study such objects here •  Lists, Sets, Maps

–  Are built out of such recursive objects

2

Monolog 0

End of Monologue

Page 3: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Objects can reference other objects

3

Oyster 1

day time

Appointment

Page 4: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Objects can reference other objects

–  Which may contain primitive values

4

hr!

3!

min!

45!

isPM!

true!

Time

day time

Appointment

Page 5: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Objects can reference other objects

–  Which may contain primitive values

–  Or references to other objects

5

hr!

3!

min!

45!

isPM!

true!

Time month! day!

19!

yr!2013!

Date

day time

Appointment

Page 6: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Objects can reference other objects

–  Which may contain primitive values

–  Or references to other objects

–  Which may reference other objects as well

6

hr!

3!

min!

45!

isPM!

true!

Time month! day!

19!

yr!2013!

Date

String

chars!

day time

Appointment

Page 7: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Objects can reference other objects

–  Which may contain primitive values

–  Or references to other objects

–  Which may reference other objects as well

7

hr!

3!

min!

45!

isPM!

true!

Time month! day!

19!

yr!2013!

Date

String

chars!

char[]

'N'! 'o'! 'v'!

day time

Appointment

Page 8: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

8

hr!

3!

min!

45!

isPM!

true!

Time month! day!

19!

yr!2013!

Date

String

chars!

char[]

'N'! 'o'! 'v'!

day time

Appointment

Page 9: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless

9

hr!

3!

min!

45!

isPM!

true!

Time

day time

Appointment

month! day!

19!

yr!2013!

Date

String

chars!

char[]

'N'! 'o'! 'v'!

Page 10: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless

10

Appointment

Page 11: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen

11

Appointment

prev

Page 12: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen

12

Appointment

prev

prev

Appointment

Page 13: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen

13

Appointment

prev

prev

Appointment

Page 14: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen •  Which can go on

14

Appointment

prev

prev

prev

Appointment

Appointment

Page 15: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen •  Which can go on

15

Appointment

prev

prev

prev

Appointment

Appointment

Page 16: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  These structures always reach some fixed size

–  Unless •  We have a pointer to a

type previously seen •  Which can go on •  Ends in null

16

Appointment

prev

prev

prev

Appointment

Appointment

Page 17: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  There could be any number of previous appointments

17

Appointment

prev

prev

prev

Appointment

Appointment

Page 18: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  There could be any number of previous appointments

•  This data structure can grow arbitrarily large

18

Appointment

prev

prev

prev

Appointment

Appointment

Page 19: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Trick –  An Appointment can

reference an Appointment

19

Appointment

prev

prev

prev

Appointment

Appointment

Page 20: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Trick –  An Appointment can

reference an Appointment –  Recursive data type

20

Appointment

prev

prev

prev

Appointment

Appointment

Page 21: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Trick –  An Appointment can

reference an Appointment –  Recursive data type –  Base case is null!

21

Appointment

prev

prev

prev

Appointment

Appointment

Page 22: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Value of interest in the list

22

ListNode!

Page 23: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Could be an int!

23

ListNode!

131!

Page 24: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Could be a boolean!

24

ListNode!

true!

Page 25: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Could be a String!

25

ListNode!

String!

Page 26: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Could be or any other kind of reference!

26

ListNode!

Page 27: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Let's assume it's an int, named value!

27

ListNode!

131!

value!

Page 28: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Let's assume it's an int, named value!–  reference to the next node in the list

28

ListNode!

131!

value!

Page 29: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  has-a –  payload

»  Let's assume it's an int, named value!–  reference to the next node in the list

»  Let's assume it's named next!

29

ListNode!

131!

ListNode!

240!

value! value!next! next!

Page 30: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  to save space we will stack the contents vertically:!

30

ListNode!

131!

ListNode!

240!

value! value!next! next!

value!

next!

value!

next!

Page 31: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  to save space we will stack the contents vertically:!

31

ListNode!

240!

value!next!131!

value!

next!

value!

next!

Page 32: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.1 List Nodes

•  Generic form of this idea –  ListNode!

•  to save space we will stack the contents vertically:!

32

131! 240!

End of Oyster

value!

next!

value!

next!

Page 33: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  What is the difference between –  A person (has-a finger) –  A finger pointing to a person –  A ListNode object –  A ListNode reference

•  The only way to make an object –  new ListNode(….)!–  creates what we show as a circle on these slides –  has-a

• int value • ListNode next

•  The only way to declare a reference –  ListNode ref;!

33

Roundtable 2

Page 34: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below? •  How many references?

34

132!

240! 241!131!

Page 35: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  A reference –  is a red box –  either points at an object (a circle)

35

132!

240! 241!131!

Page 36: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  A reference –  is a red box –  either points at an object (a circle)

36

132!

240! 241!131!

Page 37: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  A reference –  is a red box –  either points at an object (a circle) –  or is null

37

132!

240! 241!131!

Page 38: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  A reference –  is a red box –  it never points to another red box !

38

132!

240! 241!131!No!

Page 39: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below? •  How many references?

39

332!132!

240! 241!131!

Page 40: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below?

40

332!132!

240! 241!131!

1

Page 41: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below?

41

332!132!

240! 241!131!

1 2

Page 42: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below?

42

332!132!

240! 241!131!

1 2 3

Page 43: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below?

43

332!132!

240! 241!131!

1 2 3

4

Page 44: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below?

44

332!132!

240! 241!131!

1 2 3

4 5

Page 45: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many objects do you see below? –  5 objects, each is a ListNode!

45

332!132!

240! 241!131!

1 2 3

4 5

Page 46: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many ListNode references do you see?

46

332!132!

240! 241!131!

Page 47: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.2 Roundtable

•  How many ListNode references do you see? –  10 red boxes – each is a ListNode reference

47

332!132!

240! 241!131!

End of Roundtable

Page 48: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

48

Oyster 3

•  We will implement List<T> using ListNode!–  But first, some exercises using ListNode!

•  We will use either –  iteration –  recursion

•  Our methods will be either –  static, so they accept an ListNode parameter –  nonstatic, so they operate on this ListNode!

Page 49: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! ! !

}!

49

value!

next!

Page 50: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

50

value!

next!

Page 51: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

51

•  These instance variables are missing the private declaration

value!

next!

Page 52: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

52

•  These instance variables are missing the private declaration •  The fields can be set or examined by any class

in the same package as ListNode!

value!

next!

Page 53: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

53

•  These instance variables are missing the private declaration •  The fields can be set or examined by any class

in the same package as ListNode!•  Avoids the need for getters and setters

value!

next!

Page 54: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

54

•  These instance variables are missing the private declaration •  The fields can be set or examined by any class

in the same package as ListNode!•  Avoids the need for getters and setters •  Makes the resulting code less cluttered!

value!

next!

Page 55: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

55

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!

0!

null!

Page 56: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

56

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!

0!

null!

p!

Page 57: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

57

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

0!

null!

0!

null!

p! q!

Page 58: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

58

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value =!

0!

null!

0!

null!

p! q!

Page 59: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

59

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45;!

45!

null!

0!

null!

p! q!

Page 60: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

60

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null

45!

null!

0!

null!

p! q!

Page 61: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

61

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; !

45!

null!

0!

null!

p! q!

Page 62: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

62

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; !

45! 0!

null!

p! q!

Page 63: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

63

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; !

45! 0!

null!

p! q!

Page 64: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

64

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; // q could be null here!

45!

p!null!

q!

Page 65: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

65

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; // q could be null here!

45!

null!

p!null!

q!

Page 66: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

•  Declaration of ListNode!

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!

66

•  Given •  ListNode<Integer> p = new ListNode<Integer>();!•  ListNode<Integer> q = new ListNode<Integer>();!

•  We can say •  p.value = 45; // p cannot be null •  p.next = q; // q could be null here!

45!

p! q!

Page 67: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

67

131 132 240 241 this!

this!

System.out.println(!

);!

Page 68: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

68

131 132 240 241 this!

this!

System.out.println(!

);!

The results of this depend on the implementation of

toString() within ListNode!

Page 69: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

69

131 132 240 241 this!

this!

System.out.println(!

);!

The results of this depend on the implementation of

toString() within ListNode!

"A ListNode"!

Page 70: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

70

131 132 240 241 this!

this!

System.out.println(!

);!

The results of this depend on the implementation of

toString() within ListNode!

"A ListNode with 131"!

Page 71: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

71

131 132 240 241 this!

this!

System.out.println(!

);!

The results of this depend on the implementation of

toString() within ListNode!

Page 72: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

72

131 132 240 241 this!

this.value!

System.out.println(!

);!

131!

Page 73: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

73

131 132 240 241 this!

this.next!

System.out.println(!

);!

"A ListNode with 132"!

Page 74: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

74

131 132 240 241 this!

this.next.value!

System.out.println(!

);!

132!

Page 75: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

75

131 132 240 241 this!

this.next.next.next.value!

System.out.println(!

);!

241!

Page 76: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

76

131 132 240 241 this!

this.next.next.next.next!

System.out.println(!

);!

null!

Page 77: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

77

131 132 240 241 this!

Now let's try changing the values and structure of the list!

Page 78: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

78

131 132 240 241 this!

this.value = 100;!

Page 79: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

79

131 132 240 241 this!

this.value = 100;!

Page 80: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

80

131 132 240 241 this!

this.value = 100;!

Page 81: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

81

100 132 240 241 this!

this.value = 100;!

Page 82: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

82

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 83: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

83

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 84: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

84

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 85: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

85

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 86: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

86

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 87: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

87

100 132 240 241 this!

this.next.next.next.value = 101;!

Page 88: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

88

100 132 240 101 this!

this.next.next.next.value = 101;!

Page 89: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

89

100 132 240 101 this!

Page 90: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

90

100 132 240 101 this!

this.next =!

Page 91: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

91

100 132 240 101 this!

this.next =!

Page 92: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

92

100 132 240 101 this!

this.next = this.next.next;!

Page 93: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

93

100 132 240 101 this!

this.next = this.next.next;!

Page 94: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

94

100 132 240 101 this!

this.next = this.next.next;!

Page 95: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

95

100 132 240 101 this!

this.next = this.next.next;!

Page 96: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

96

100 132 240 101 this!

Page 97: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

97

100 132 240 101 this!

Page 98: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

98

100 132 240 101 this!

The storage associated with this unreachable node would eventually be recycled by the

garbage collector!

Page 99: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

99

131 132 240 241 this!

Returning to the list in its original form …!

Page 100: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

100

131 132 240 241 this!

this = null;!

Page 101: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.3 Some ListNode gymnastics

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

}!

101

131 132 240 241 this!

this = null;!

We are not allowed to change this. It is inherent to an

object and cannot be modified.!

XXXXXXXXXXXXX

End of Oyster

Page 102: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.4 Roundtable

•  Interaction of quiz on the following diagram –  What is the value of …. –  Show me where …. is stored –  Each time turning to online student first

102

131 132 240 241 this!

Roundtable 4

End of Roundtable

Page 103: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

103

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  No use of static here •  Method has access to this as

the first ListNode in the list

Oyster 5a

Page 104: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

104

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

For this problem, we will use recursion!

•  No use of static here •  Method has access to this as

the first ListNode in the list

Page 105: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

105

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

Recursion will provide the length

of this smaller list

For this problem, we will use recursion!

•  No use of static here •  Method has access to this as

the first ListNode in the list

Page 106: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

106

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  No use of static here •  Method has access to this as

the first ListNode in the list •  this.next references the

smaller list

Recursion will provide the length

of this smaller list

Page 107: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

107

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  No use of static here •  Method has access to this as

the first ListNode in the list •  this.next references the

smaller list •  this.next.size() will

compute its size

Recursion will provide the length

of this smaller list

Page 108: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

108

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  Given the length of the smaller list •  this list is just one

bigger

Recursion will provide the length

of this smaller list

Page 109: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

109

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  Given the length of the smaller list •  this list is just one

bigger

Recursion will provide the length

of this smaller list

Page 110: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

110

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

•  Given the length of the smaller list •  this list is just one

bigger

Recursion will provide the length

of this smaller list

Page 111: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

111

this!

Compute the length of a list beginning at this ListNode!

•  Base case •  Only one node

241

Page 112: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

112

this!

Compute the length of a list beginning at this ListNode!

•  Base case •  Only one node

241

Page 113: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

113

Compute the length of a list beginning at this ListNode!

•  Why 1 and not 0? •  Because this can never be

null

this! 241

Page 114: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

114

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

We next compute this.size() for the list below!

Page 115: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

115

this!

Compute the length of a list beginning at this ListNode!

1 + 1 + 1 + 1!

131 132 240 241

Page 116: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

116

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

1 + 1 + 1 + 1!

this!

Page 117: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

117

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

1 + 1 + 1 + 1!

this!

Page 118: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

118

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

1 + 1 + 1 + 1!

this!

Page 119: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

119

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

1 + 1 + 2!

this!

Page 120: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

120

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

1 + 3!

this!

Page 121: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public class ListNode<T> {!! !T value;!! !ListNode<T> next;!

!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!

}!

121

131 132 240 241 this!

Compute the length of a list beginning at this ListNode!

4!End of Oyster

Page 122: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

122

131 132 240 241 head!

Sum the elements starting at head!

Oyster 5b

Page 123: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

123

131 132 240 241 head!

Sum the elements starting at head!

Page 124: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

124

131 132 240 241 head!

Sum the elements starting at head!

For this problem, we will use iteration

Page 125: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

125

131 132 240 241 head!

For this problem, we will use iteration and a static method

Sum the elements starting at head!

Page 126: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

126

131 132 240 241 head!

With no this, the method requires the first node as a parameter

Sum the elements starting at head!

Page 127: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

127

131 132 240 241 head!

Sum the elements starting at head!

Page 128: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

128

131 132 240 241 head!

Sum the elements starting at head!

Page 129: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

129

131 132 240 241 head!

Sum the elements starting at head!

p!

Page 130: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

130

131 132 240 241 head!

Sum the elements starting at head!

p!

Page 131: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

131

131 132 240 241 head!

Sum the elements starting at head!

p!

Page 132: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

132

131 132 240 241 head!

Sum the elements starting at head!

p!

Page 133: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!

}!

133

131 132 240 241 head!

Sum the elements starting at head!

p!

Page 134: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

134

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 0!

Page 135: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

135

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 131!

Page 136: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

136

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 131!

Page 137: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

137

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 263!

Page 138: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

138

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 263!

Page 139: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

139

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 503!

Page 140: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

140

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 503!

Page 141: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

141

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 744!

Page 142: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

142

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 744!

Page 143: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

143

131 132 240 241 head!

Sum the elements starting at head!

p!

ans! 744!

Page 144: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

144

131 132 240 241 head!

Sum the elements starting at head!

ans! 744!

Page 145: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.5 ListNode methods

public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!

}!

145

131 132 240 241 head!

Sum the elements starting at head!

Thus, sum(head) returns 744!

End of Oyster

Page 146: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.6 Roundtable

•  We will complete the following method –  size(), static and recursively

•  Different base case!

146

Roundtable 6

End of Roundtable

Page 147: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.7 Exercises

•  Write each of the following methods –  size(), static and iteratively –  size(), static and recursively

•  Write the following method –  sum(), static but recursively

•  Use a "helper" method if necessary –  Why can't we write a nonstatic version of the sum()

method?

147

BLT 7a,b,c,d

End of BLT

Page 148: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT: –  T is a type parameter

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

148

Oyster 8a

Page 149: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT: –  T is a type parameter

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

149

We can use T in here to declare the type of the values contained in the list

Page 150: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

150

Page 151: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

151

•  Add the item x of type T to the beginning of the list.

Page 152: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

152

•  Add the item x of type T to the end of the list.

Page 153: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

153

•  Find and remove the first occurrence of x in the list, returning true if successful

Page 154: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

154

•  Return the ith element in the list •  First element is at index 0 •  Return null if no such element exists

Page 155: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

155

•  Return the size of the list •  The number of elements in the list!

Page 156: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

156

•  Is the list empty? •  Can use the principle of reduction

Page 157: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List!

•  Consider a List ADT:

public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!

157

•  Is the list empty? •  Can use the principle of reduction •  Same as asking if size() == 0!

Page 158: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  We next implement the List<T> interface –  A SinglyLinkedList<T> implementation

•  Two kinds of objects involved –  ListNode<T> has-a

• T value!• ListNode<T> next!

158

value!

next!

Page 159: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  We next implement the List<T> interface –  A SinglyLinkedList<T> implementation

•  Two kinds of objects involved –  ListNode<T> has-a

• T value!• ListNode<T> next!•  We will use many of these per list

159

value!

next!

Page 160: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  We next implement the List<T> interface –  A SinglyLinkedList<T> implementation

•  Two kinds of objects involved –  ListNode<T> has-a

• T value!• ListNode<T> next!•  We will use many of these per list

–  SinglyLinkedList<T> has-a • ListNode<T> head!

160

value!

next!

head!

Page 161: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  We next implement the List<T> interface –  A SinglyLinkedList<T> implementation

•  Two kinds of objects involved –  ListNode<T> has-a

• T value!• ListNode<T> next!•  We will use many of these per list

–  SinglyLinkedList<T> has-a • ListNode<T> head!•  Only one of these used per list

161

value!

next!

head!

Page 162: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  To simply things –  Let's assume the parametric type T is Integer!–  Our implementation will work for any type T!

162

131 132 240 241 head!

Page 163: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  In our singly-linked list implementation –  Each ListNode points to the next element in the list

163

131 132 240 241 head!

Page 164: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  In our singly-linked list implementation –  Each ListNode points to the next element in the list –  The head pointer in SinglyLinkedList points to

the beginning of the list

164

131 132 240 241 head!

Page 165: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Note –  The SinglyLinkedList object implements the

methods of interest

165

131 132 240 241 head!

Page 166: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Note –  The SinglyLinkedList object implements the

methods of interest

166

131 132 240 241 head!

addFirst(…)!

Page 167: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Note –  The SinglyLinkedList object implements the

methods of interest

167

131 132 240 241 head!

size()!

Page 168: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Note –  The SinglyLinkedList object implements the

methods of interest •  It does its work by following and processing the ListNode objects

168

131 132 240 241 head!

Page 169: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Note –  The SinglyLinkedList object implements the

methods of interest –  The ListNode objects are just containers

•  They offer no special methods at all

169

131 132 240 241 head!

Page 170: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  We will use the following program and implement methods as needed

170

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

Page 171: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Construction

171

head!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!

Page 172: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Construction •  The only instance variable is head!

–  Set to null!–  Represents the empty list

172

head!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!

End of Oyster

Page 173: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

173

head!

•  Method addFirst(Integer i) –  Special case: adding the element to an empty list

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

Oyster 8b

Page 174: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

174

0 head!

•  Method addFirst(Integer i) –  Special case: adding the element to an empty list

–  Create a new ListNode (referenced by p)!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

p!

Page 175: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

175

240 head!

•  Method addFirst(Integer i) –  Special case: adding the element to an empty list

–  Create a new ListNode (referenced by p)!–  Set its value to i (its next is null)

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

p!

Page 176: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

176

240 head!

•  Method addFirst(Integer i) –  Special case: adding the element to an empty list

–  Create a new ListNode (referenced by p)!–  Set its value to i (its next is null) –  Set head to reference p's ListNode!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

p!

Page 177: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

177

240 head!

•  Method addFirst(Integer i) –  Result shown below

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

Page 178: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

178

240 head!

•  Method addFirst(Integer i) –  The list is no longer empty!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!

Page 179: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

179

240 head!

•  Method addFirst(Integer i) –  The list is no longer empty!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!

Page 180: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

180

240 head!

132

•  Method addFirst(Integer i) –  Adding to a non-empty list

–  Create a new ListNode and set its value to 132!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!

p!

Page 181: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

181

240 head!

132

•  Method addFirst(Integer i) –  Adding to a non-empty list

–  Create a new ListNode and set its value to 132 –  Its next references head's ListNode!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!

p!

Page 182: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

182

240 head!

132

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!

•  Method addFirst(Integer i) –  Adding to a non-empty list

–  Create a new ListNode and set its value to 132 –  Its next references head's ListNode!–  head references p's ListNode!

p!

Page 183: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

183

240 head!

132

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!

•  Method addFirst(Integer i) –  Result shown below

Page 184: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

9.8 A LinkedList implementation of List

184

240 head!

132

•  Method addFirst(Integer i) –  Result shown below

Page 185: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

9.8 A LinkedList implementation of List

185

240 head!

132

•  Method addFirst(Integer i) –  Same process as last time

Page 186: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

9.8 A LinkedList implementation of List

186

240 head!

132

•  Method addFirst(Integer i) –  Same process as last time

131

p!

Page 187: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

9.8 A LinkedList implementation of List

187

240 head!

132

•  Method addFirst(Integer i) –  Same process as last time

131

p!

Page 188: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

9.8 A LinkedList implementation of List

188

240 head!

132

•  Method addFirst(Integer i) –  Same process as last time

131

p!

End of Oyster

Page 189: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

189

240 head!

132 131

•  Method addFirst(Integer i) –  Result shown below

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!

Page 190: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

190

240 head!

132 131

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  This could happen to an empty list

Oyster 8c

Page 191: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

191

240 head!

132 131

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  This could happen to an empty list –  Note: adding to the beginning or end of an empty list

produces the same result –  Use the principle of reduction

Page 192: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

192

240 head!

132 131

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  This could happen to an empty list –  If so, call addFirst(i)!

Page 193: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

193

240 head!

132 131

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise!

Page 194: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

194

240 head!

132 131 0

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode (call it q)

q!

Page 195: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

195

240 head!

132 131 241

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!

q!

Page 196: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

196

240 head!

132 131 241

q!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  We now search for the end of the list

Page 197: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

197

240 head!

132 131 241

p! q!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  We now search for the end of the list (using p)

Page 198: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

198

240 head!

132 131 241

p! q!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  We now search for the end of the list (using p)

Page 199: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

199

240 head!

132 131 241

p! q!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  We now search for the end of the list (using p)

Page 200: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

200

240 head!

132 131 241

p! q!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  When p references the end

Page 201: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

201

240 head!

132 131 241

p!

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Otherwise

–  Make the new ListNode and set its value!–  When p references the end –  Set p.next to q!

q!

Page 202: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

202

240 head!

132 131 241

SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!

•  Method addLast(Integer i) –  Result shown below!

End of Oyster

Page 203: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

203

240 head!

132 131 241

•  Method remove(Integer i) –  Finds and removes the first node that has i as its value!

list.remove(132);!

Oyster 8d

Page 204: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

204

•  Method remove(Integer i) –  Special case: empty list—nothing to do!

list.remove(132);!

head!

Page 205: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

205

•  Method remove(Integer i) –  Another special case: item is found first in the list!

list.remove(132);!

240 head!

132 241

Page 206: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

206

•  Method remove(Integer i) –  Another special case: item is found first in the list –  Remove by setting head = head.next!

list.remove(132);!

240 head!

132 241

Page 207: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

207

•  Method remove(Integer i) –  Another special case: item is found first in the list –  Remove by setting head = head.next!

list.remove(132);!

240 head!

132 241

Page 208: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

208

•  Method remove(Integer i) –  Another special case: item is found first in the list –  Remove by setting head = head.next!–  The deleted node is no longer part of this list

list.remove(132);!

240 head!

132 241

Page 209: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

209

•  Method remove(Integer i) –  Another special case: item is found first in the list –  Remove by setting head = head.next!–  The deleted node is no longer part of this list

list.remove(132);!

240 head!

241

Page 210: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

210

240 head!

132 131 241

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list

list.remove(132);!

Page 211: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

211

240 head!

132 131 241

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list –  Suppose the item is found and referenced by p

list.remove(132);!

Page 212: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

212

240 head!

132 131 241

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list –  Suppose the item is found and referenced by p

list.remove(132);!

p!

Page 213: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

213

240 head!

132 131 241

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list –  Suppose the item is found and referenced by p –  We have no reference to the previous node

list.remove(132);!

p!

Page 214: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

214

240 head!

132 131 241

p!

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list –  Trick: look at the node just to the right of p!

Page 215: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

215

240 head!

132 131 241

p!

•  Method remove(Integer i) –  Otherwise we have to look for the item in the middle

of the list –  Trick: look at the node just to the right of p!

Page 216: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Recall our List ADT: •  And recall our singly-linked list implementation •  The method remove(int)!

–  Technique: look at value one to the right of p!

216

240 head!

132 131 241

p!

Page 217: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

•  Recall our List ADT: •  And recall our singly-linked list implementation •  The method remove(int)!

–  Technique: look at value one to the right of p!

217

240 head!

132 131 241

p!

Page 218: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

218

240 head!

132 131 241

p!

•  Method remove(Integer i) –  Trick: look at the node just to the right of p!–  When found, set p.next = p.next.next!

list.remove(132);!

Page 219: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

219

240 head!

132 131 241

p!

•  Method remove(Integer i) –  Trick: look at the node just to the right of p!–  When found, set p.next = p.next.next!

list.remove(132);!

Page 220: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

9.8 A LinkedList implementation of List

220

240 head!

131 241

•  Method remove(Integer i) –  Result is shown below!

list.remove(132);!

End of Oyster

Page 221: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

Roundtable

•  Develop the SinglyLinkedList implementation –  addFirst, addLast, remove!–  If time allows: get, size, and isEmpty!

221

Roundtable 9a

End of Roundtable

Page 222: Module 9: List Structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 Introduction • Recall methods – One method can call another – A method can even call itself – this

Roundtable

•  How is our code changed by adding a sentinel to the list? –  A dummy node at the start of the list –  It's there even just after the list is constructed –  It never goes away

•  Modify the methods to take account of this –  And see how things get simpler –  Each method – turn to student: how would you do

this? –  addFirst, addLast, remove!–  If time allows: get, size, and isEmpty!

222

Roundtable 9b

End of Roundtable