Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All...
-
Upload
oscar-eaton -
Category
Documents
-
view
217 -
download
0
Transcript of Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All...
Lists and Iterators
H C P T E R 21
A
Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.
Java MethodsJava MethodsObject-Oriented Programming
and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
21-2
Objectives:
• Learn to work with ListNode objects and do-it-yourself linked lists
• Understand singly-linked list, linked list with a tail, circular list, and doubly-linked list
• Learn to implement iterators
21-3
Singly-Linked List
• Each node holds a reference to the next node
• In the last node, next is null
• A linked list is defined by a reference to its first node (often named head or front)
value 0 value 1 value 2
value ... value n-1
head
21-4
Singly-Linked List (cont’d)public class ListNode{ private Object value; private ListNode next;
public ListNode (Object v) { value = v; next = null; }
public ListNode (Object v, ListNode nx) { value = v; next = nx; }
public Object getValue ( ) { return value; } public ListNode getNext ( ) { return next; } public void setValue (Object v) { value = v; } public void setNext (ListNode nx) { next = nx; }}
A reference to the next node
Represents a node of a singly-linked list
21-5
Singly-Linked List — Example 1• Append x at the head of a linked list and
return the head of the new list.
public ListNode append (ListNode head, Object x) { return new ListNode (value, head); }
value 0 value 1 value 2
value ... value n-1
x
head
21-6
Singly-Linked List — Example 2 • Assuming the list has at least two nodes,
remove the last node.
public void removeLast (ListNode head) { ListNode node = head; while (node.getNext ().getNext () != null) node = node.getNext ( ); node.setNext (null); }
null
nullA B C D
node
head
21-7
Singly-Linked List — Traversal
public void printList (ListNode head) { for (ListNode node = head; node != null; node = node.getNext ( )) System.out.println (node.getValue ()); }
21-8
Do-it-Yourself Iterator
public class SinglyLinkedList implements Iterable<Object>{ private ListNode head; ... public Iterator<Object> iterator () { return new SinglyLinkedListIterator (head); } ...}
21-9
Do-it-Yourself Iterator (cont’d)public class SinglyLinkedListIterator implements Iterator<Object>{ private ListNode nextNode;
public SinglyLinkedListIterator (ListNode head) { nextNode = head; }
public boolean hasNext ( ) { return nextNode != null; }
public Object next ( ) { if (nextNode == null) throw new NoSuchElementException ( );
Object obj = nextNode.getValue ( ); nextNode = nextNode.getNext ( ); return obj; } ...}
public void remove ( ) { throw new UnsupportedOperationException( ); }
21-10
Singly-Linked List with a Tail
• Keeps a reference to the last node
• Suitable for implementing a queue
value 0 value 1 value 2
value ... value n-1
head tail
21-11
Doubly-Linked List
• Each node has references to the next and previous nodes
• In the last node, next is null; in the first node, previous is null.
• Can be traversed backwards
a0 a1 a2 an-1...
head tail
21-12
Doubly-Linked Circular List
• next in the last node points to the first node
• previous in the first node points to the last node
a0 a1 a2 an-1...
head
21-13
Doubly-Linked Circular List with a Header Node
• That’s how java.util.LinkedList is implemented
a0 a1 a2 an-1...
private ListNode2 header;
a field in the DoublyLinkedList class
21-14
Review:
• What does an object of the ListNode class represent?
• Which fields and methods have to be added to ListNode to make it suitable for doubly-linked lists?
• What is a circular list?
• In an empty circular list with a header node, what are the values of next and previous in header?