Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.

Similar presentations


Presentation on theme: "Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming."— Presentation transcript:

1 Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 2nd AP edition  with GridWorld

2 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

3 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 0value 1value 2 value...value n-1 head

4 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

5 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 0value 1value 2 value...value n-1 x head

6 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 ABCD node head

7 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 ()); }

8 21-8 Do-it-Yourself Iterator public class SinglyLinkedList implements Iterable { private ListNode head;... public Iterator iterator () { return new SinglyLinkedListIterator (head); }... }

9 21-9 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( ); }

10 21-10 Singly-Linked List with a Tail Keeps a reference to the last node Suitable for implementing a queue value 0value 1value 2 value...value n-1 headtail

11 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 a0a0 a1a1 a2a2 a n-1... head tail

12 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 a0a0 a1a1 a2a2 a n-1... head

13 21-13 Doubly-Linked Circular List with a Header Node That’s how java.util.LinkedList is implemented a0a0 a1a1 a2a2 a n-1... private ListNode2 header; a field in the DoublyLinkedList class

14 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?


Download ppt "Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming."

Similar presentations


Ads by Google