lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where...

18
CS206 Lec05 CS206 Linked Lists 1

Transcript of lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where...

Page 1: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

CS206

Linked Lists

1

Page 2: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Linked List

• A linked list is a lists of objects. • The objects form a linear sequence. • The sequence is unbounded in length.

2

A B C D

head tail

Page 3: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Linked List versus Array

• An array is a single consecutive piece of memory, a linked list is made of many disjoint pieces (the linked objects).

3

Array Linked List

Page 4: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Linked List versus Array

• Array ▫ quick access to any element ▫ slow insertion, deletion and reordering

(shifting required in general)

• Linked list ▫ quick insertion, deletion and reordering of

the elements ▫ slow access (must traverse list)

4

Page 5: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Linked List Core• the essential part of a linked list is a “self-referential”

structure • That is, a class with an instance variable that holds a

“reference” to another member of that same class • For linked lists, this structure is usually referred to as

a Node

5

public class Node {

private Object data;

private Node next;

}

Page 6: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

References in Java

• A reference variable holds a memory address to where the referenced object is stored (not the object itself)

• Reference types ▫ Anything that inherits from Object (including String, Integer, Double, etc)

▫ “primitive” types: int, float, etc are NOT reference types

• A reference is null when it doesn’t refer/point to any object

6

Page 7: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

References and equality

7

public class ReferenceCheck { public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("abc"); String s3 = s2; String s4 = "abc";

System.out.println(“s1.equals(s2) " + s1.equals(s2)); System.out.println("s1==s2 " + (s1 == s2)); System.out.println("s1==s3 " + (s1 == s3)); System.out.println("s1==s4 " + (s1 == s4)); System.out.println("s2==s3 " + (s2 == s3)); System.out.println("s2==s3 " + (s2 == s4)); System.out.println("s3==s4 " + (s3 == s4)); } }

Equals compares content

== compares memory location

Page 8: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206

Rabbits

8

You want to store data about a herd of rabbits. Each rabbit has a breed and birthdate (stored as double) and ID. Your herd has three breeds, french lop, dwarf dutch, angorapublic enum BreedEnum { DwarfDutch, Angora, FrenchLop }

public class Rabbit { private final BreedEnum breed; private final String iD; public Rabbit(BreedEnum breed, String id) { this.breed = breed; this.iD = id; } private Rabbit() { this.breed=BreedEnum.Angora; this.iD=null; } public boolean equals(Rabbit otherRabbit) { return otherRabbit.getId().equals(this.iD); } public String getId() { return iD; } }

Final

Private constructor!

Enumerated type

Override equals

Page 9: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Node for Rabbitsprivate class Node { public Rabbit data; public Node next; public Node(Rabbit data, Node next) { this.data = data; this.next = next; } }

9

Page 10: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

A Rabbity Linked List interface

10

public interface LinkedListInterface { int size(); boolean isEmpty(); Rabbit first(); Rabbit last(); void addLast(Rabbit c); void addFirst(Rabbit c); Rabbit removeFirst(); Rabbit removeLast(); Rabbit remove(Rabbit r); Rabbit find(String iD); }

No mention of nodes!! handout

Page 11: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206

Starting Point

11

public class LinkedListOfRabbits implements LinkedListInterface { private class Node { public Rabbit data;

public Node next; public Node(Rabbit data, Node next) { this.data = data; this.next = next; } } private Node head = null; private Node tail = null; private int size = 0; }

Page 12: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Print a Linked List

12

public String toString() { StringBuffer s = new StringBuffer(); for (Node n=head; n!=null; n=n.getNext()) { s.append( n.data.toString()); if (n != tail) { s.append("\n"); } } return s.toString(); }

Page 13: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Inserting at the Tail1. create a

new node 2. Have new

node point to null

3. have old last node point to new node

4. update tail to point to new node

13

R13

R1R13

R7R1R13

R7

R7R1

R22

R22

Page 14: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Insertion

14Why not take a Node?

public void addLast(Rabbit c) { Node newest = new Node(c, null); if (isEmpty()) { head = newest;} else { tail.next=newest; } tail = newest; size++; }

Page 15: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Inserting at the Head1. create a

new node 2. have new

node point to old head

3. update head to point to new node

15

R15R13R42

R73R15R13

R73R15R13

R73

R42

write addFirst at chalkboard

Page 16: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Removing at the Head1. update

head to point to next node in the list

2. allow “garbage collector” to reclaim the former first node

16

R2R4R74

R2

R2R4R74

R4R74R42

R42

Page 17: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec05

Deletion

17

public Rabbit removeFirst() {

if (isEmpty()) {return null;} Rabbit target = head.data; head = head.next; size--; if (isEmpty()) {tail = null;} return target;

}

Page 18: lec05 · CS206 Lec05 References in Java • A reference variable holds a memory address to where the referenced object is stored (not the object itself) • Reference types Anything

CS206 Lec06

Find

18