Đang tải... (xem toàn văn)
Chapter 21 - Lists and iterators. In this and the following chapter we start looking at the implementation details of data structures. This chapter’s objectives are to: 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.
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin A C H P T E R 21 Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved 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 212 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) head value value value value value n-1 213 Singly-Linked List (cont’d) public class ListNode { private Object value; private ListNode next; Represents a node of a singly-linked list A reference to the next node 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; } } 214 Singly-Linked List — Example • 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); } x head value value value value value n-1 215 Singly-Linked List — Example • 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); } node head A B C D null null 216 Singly-Linked List — Traversal public void printList (ListNode head) { for (ListNode node = head; node != null; node = node.getNext ( )) System.out.println (node.getValue ()); } 217 Do-it-Yourself Iterator public class SinglyLinkedList implements Iterable { private ListNode head; public Iterator iterator () { return new SinglyLinkedListIterator (head); } } 218 Do-it-Yourself Iterator (cont’d) public class SinglyLinkedListIterator implements Iterator { 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( ); } 219 Singly-Linked List with a Tail • Keeps a reference to the last node • Suitable for implementing a queue head tail value value value value value n-1 2110 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 head tail a0 a1 a2 an1 2111 Doubly-Linked Circular List • next in the last node points to the first node • previous in the first node points to the last node head a0 a1 a2 an1 2112 Doubly-Linked Circular List with a Header Node • That’s how java.util.LinkedList is implemented private ListNode2 header; a field in the DoublyLinkedList class a0 a1 a2 an1 2113 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 doublylinked 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? 2114 ... 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? ?2 Singly-Linked List... setNext (ListNode nx) { next = nx; } } 21? ?4 Singly-Linked List — Example • Append x at the head of a linked list and return the head of the new list public ListNode append (ListNode head, Object x)... UnsupportedOperationException( ); } 21? ?9 Singly-Linked List with a Tail • Keeps a reference to the last node • Suitable for implementing a queue head tail value value value value value n-1 21? ?10 Doubly-Linked List