Lecture 15: Linked Lists

Overview

  1. A zoo as an Array
  2. Linked Lists, Conceptually
  3. Implementing a Linked List

Previously

Stored zoo as an array of Animals:

Animal[] zoo = new Animal[ZOO_SIZE];

for (int i = 0; i < ZOO_SIZE; i++) {
    Animal[i] = new ...;
}

Problem:

  • We need to specify ZOO_SIZE in advance!
  • But we may want to add/remove Animals from our zoo

How can we make our zoo dynamic?

A zoo Array in Memory

How is Animal[] zoo actually stored in our computer’s memory?

Another Way to Store a Zoo

Think Locally!

  • Consider an actual zoo
  • Each animal is in some kind of enclosure
  • Each enclosure has:
    • a sign indicating what animal(s) are there
    • another sign pointing to next enclosure
  • This is enough information to navigate the zoo and see all animals

Representing a Zoo in Java

High level picture—a Linked List:

Thinking classes

What do we need?

  1. class Node representing enclosure
    • stores (reference to) an Animal
    • stores (reference to) next Node
  2. class Zoo
    • stores (reference to) first Node (if any)
    • stores (reference to) last Nodes (if any)
    • implements desired operations:
      • feed Animals
      • add an Animal
      • remove an Animal

Implementing Node

class Node {
    private Animal animal;
    private Node next;

    public Node (Animal animal) {
	this.animal = animal;
	next = null;
    }

    public void setNext (Node nd) {
	next = nd;
    }

    public Node getNext () {
	return Next;
    }

    public Animal getAnimal () {
	return animal;
    }
}

Making the Zoo Class

public class Zoo {
    private Node head = null;
    private Node tail = null;

    public void add (Animal a) {...}
	
    public void feedAnimals () {...}
	
    public void remove (Animal a) {...}

    public boolean contains (Animal a) {...}
}

How to Implement add?

add a Platypus named Doug

Step 1: Create a Node for Doug

Step 2: Update tail.next

Step 3: Update tail

Now Doug is in the List

What if List Was Empty?

Make New Node head and tail

add in Code

  1. Create a Node for doug:
     Node nd = new Node(doug);
    
  2. If tail != null
    • Update tail.next:
        tail.setNext(nd);
      
    • Update tail:
        tail = nd;
      
  3. Else
    • Set head, tail:
        head = nd;
        tail = nd;
      

Activity

Think about how to implement other Zoo operations!

  • Feed all the animals
  • Remove an animal
  • Determine if the zoo contains a given animal

How to Implement feedAnimals?

How to Implement remove?

How to Implement contains?

Testing Our Code

Another Question

With the array Animal[] zoo we could access all of the Animals easily:

Animal[] zoo;
...
for (int i = 0; i < zoo.length; i++) {
   // do something with zoo[i]
}

How could we do something similar with the Zoo class?

Coming Up

  • Generics
  • Interfaces
  • Iterators