View page as slide show

Click presentation screen icon to the right to see the slide show.

Chapter 5: Linked Lists

Using references to instances of a Node class to store information.

  • list interface
  • Node class
  • using current and previous references to manipulate a list
  • variants: dummy nodes, doubly linked
  • List ADT

5.1 Preliminaries

Java references

Resizable arrays

Reference based linked lists

Linked List Basics

Linked List Basics

Linked List Basics

class Node {
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}

Linked List Basics

How would you use Node to create the linked list?

class Node {
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}

Linked List Basics

How would you use Node to create the linked list?

class Node {
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
Node head = null;  // empty list
 
head = new Node(new String("Lou"));

Linked List Basics

How would you use Node to create the linked list?

class Node {
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
Node head = null;  // empty list
 
head = new Node(new String("Lou"));
head = new Node(new String("Mary", head));
head = new Node(new String("Julie", head));
head = new Node(new String("Chuck", head));

Linked List Basics

How would you delete “Chuck”?

Linked List Basics

How would you delete “Chuck”?

head = head.next;

Linked List Basics

How would you delete “Julie”?

Linked List Basics

How would you delete “Julie”?

Node previous = head;  // "Chuck"
 
Node current = previous.next; // "Julie", the one to delete
 
current = current.next; // "Mary"
 
... Now what? ...

Linked List Basics

How would you delete “Julie”?

Node previous = head;  // "Chuck"
 
Node current = previous.next; // "Julie", the one to delete
 
current = current.next; // "Mary"
 
previous.next = current; // "Chuck" next -> "Mary"

Stepping Through the Elements

// Using while loop
 
Node current = head;
while (current != null) {
    System.out.println(current.item);
    current = current.next;
}
 
// Using for loop
 
for (Node current = head; current != null; current = current.next) {
    System.out.println(current.item);
}

Linked List for Any Object

String items

class Node {
 
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node node = new Node(new String("Chuck"));

Object items



















Linked List for Any Object

String items

class Node {
 
  String item;
  Node next;
 
  Node (String newItem) {
    item = newItem;
    next = null;
  }
 
  Node(String newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node node = new Node(new String("Chuck"));

Object items

class Node {
 
  Object item;
  Node next;
 
  Node (Object newItem) {
    item = newItem;
    next = null;
  }
 
  Node(Object newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node node = new Node(new MyClass("stuff", 42));

Linked List for Any Object

Object items

class Node {
 
  Object item;
  Node next;
 
  Node (Object newItem) {
    item = newItem;
    next = null;
  }
 
  Node(Object newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node node = new Node(new MyClass("stuff", 42));

Any particular type Type



















Linked List for Any Object

Object items

class Node {
 
  Object item;
  Node next;
 
  Node (Object newItem) {
    item = newItem;
    next = null;
  }
 
  Node(Object newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node node = 
        new Node(new MyClass("stuff", 42));

Any particular type Type (Generic Types)

class Node<Type> {
 
  Type item;
  Node next;
 
  Node (Type newItem) {
    item = newItem;
    next = null;
  }
 
  Node(Type newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
  }
}
 
 
Node<MyClass> node = 
        new Node<MyClass>(new MyClass("Chuck",42));

Modify loops for the Generic Type Form

// Using while loop
 
Node current = head;
while (current != null) {
    System.out.println(current.item);
    current = current.next;
}
 
// Using for loop
 
for (Node current = head; current != null; current = current.next) {
    System.out.println(current.item);
}

How would you change this code?

Modify loops for the Generic Type Form

// Using while loop
 
Node current = head;
while (current != null) {
    System.out.println(current.item);
    current = current.next;
}
 
// Using for loop
 
for (Node current = head; current != null; current = current.next) {
    System.out.println(current.item);
}
 
 
 
// Using while loop
 
Node<MyClass> current = head;
while (current != null) {
    System.out.println(current.item);
    current = current.next;
}
 
// Using for loop
 
for (Node<MyClass> current = head; current != null; current = current.next) {
    System.out.println(current.item);
}

Array-based Versus Reference-Based List


Array Reference
create Must allocate initial size. Might be too much or too few Nothing to allocate
storage for each element Just the element. Location of next one is implicit Element plus reference
access element at position Constant time. Does not depend on . Time is linear in
insert element at position Must shift elements first. Must find element , and assign next. No shifting needed.
remove element at position Same as above Same as above.

Inserting and Removing Singly-Linked Lists

Inserting. There are two cases:

  • If at front of list
    newNode.next = head;
    head = newNode;
  • else, inserting anywhere else, just after node
    newNode.next = node.next;
    node.next = newNode;                

Inserting and Removing Singly-Linked Lists

Inserting. There are two cases:

  • If at front of list
    newNode.next = head;
    head = newNode;
  • else, inserting anywhere else, just after node
    newNode.next = node.next;
    node.next = newNode;                

Removing. There are two cases:

  • If at front of list
    head = head.next;
  • else, removing current node, with previous assigned
    previous.next = current.next;

Inserting and Removing with Dummy Node

Inserting and Removing with Dummy Node

Inserting:

  • Anywhere. No special cases.
  • previous will be dummy node if inserting at front.
    newNode.next = previous.next;  // 1
    previous.next = newNode;       // 2     

Inserting and Removing with Dummy Node

Removing:

  • From anywhere. No special cases.
  • Remove current node, with previous assigned.
  • previous will be dummy node if removing first node.
    previous.next = current.next;
  • March through list while updating previous and current.

Doubly-Linked Lists with Dummy Node

  • No longer need previous.

Doubly-Linked Lists with Dummy Node

  • Delete node current.
    current.previous.next = current.next; // 1
    current.next.previous = current.previous; // 2

Rewrite the multiple references.

    (current.previous).next = current.next; // 1
    ("Chuck").next = "Mary"
 
    (current.next).previous = current.previous; // 2
    ("Mary").previous = "Chuck";

Doubly-Linked Lists with Dummy Node

  • Insert Jenni as newNode node after current.
    newNode.next = current.next;     // 1
    newNode.previous = current;      // 2
    current.next = newNode;          // 3
    newNode.next.previous = newNode; // 4

or, think of it as inserting b between a and c.

    c = a.next;
    a.next = b;
    b.next = c;
    b.previous = a;
    c.previous = b;

Really? No special cases?

Try deleting last element, “Mary”.

Try deleting first element, “Chuck”.

Try inserting a new first element.

Try inserting a new last element.

Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Driven by DokuWiki