Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Methods Lists and Iterators Object-Oriented Programming

Similar presentations


Presentation on theme: "Java Methods Lists and Iterators Object-Oriented Programming"— Presentation transcript:

1 Java Methods Lists and Iterators Object-Oriented Programming
and Data Structures 3rd AP edition Maria Litvin ● Gary Litvin In this and the following chapter we start looking at the implementation details of data structures. Lists and Iterators Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.

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 The ListNode class used to be tested in now defunct AP Computer Science AB exams.

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) head head is a variable that holds a reference to the first node of the linked list. Its type is ListNode. value 0 value 1 value 2 value ... value n-1

4 Singly-Linked List (cont’d)
Represents a node of a singly-linked list 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 Adapted from the class that used to be tested in now defunct AP Computer Science AB exams.

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); } This code is terse. Basically it is the same as public ListNode append(ListNode head, Object x) { ListNode newNode = new ListNode(x, null); newNode.setNext(head); head = newNode; return head; } x head value 0 value 1 value 2 value ... value n-1

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); } To find the last node in a singly-linked list, you need to traverse the list. node head A B C D null null

7 Singly-Linked List — Traversal
public void printList (ListNode head) { for (ListNode node = head; node != null; node = node.getNext ( )) System.out.println (node.getValue ()); } This idiom with a for loop is a convenient way to traverse a singly-linked list.

8 Do-it-Yourself Iterator
public class SinglyLinkedList implements Iterable<Object> { private ListNode head; ... public Iterator<Object> iterator () return new SinglyLinkedListIterator (head); } Iterable is a Java library interface, which specifies one method, iterator.

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; } ... In general, SinglyLinkedListIterator should be an inner class in the SinglyLinkedList class. public void remove ( ) { throw new UnsupportedOperationException( ); }

10 Singly-Linked List with a Tail
Keeps a reference to the last node Suitable for implementing a queue head tail value 0 value 1 value 2 The add method would be: public void (add Object x) { ... tail.setNext(new ListNode(x, null)); } and the remove method would be public Object remove() Object temp = head.getValue(); head = head.getNext(); return temp; (with a few other details related to handling an empty list). value ... value n-1

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 In a doubly-linked list, both removeFirst and removeLast methods work in O(1) time. head tail ... a0 a1 a2 an-1

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 head ... Circular lists are not used very often. a0 a1 a2 an-1

13 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 The Teletext lab in this chapter provides an opportunity to practice with such a list. a0 a1 a2 ... an-1

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? What does an object of the ListNode class represent? A node of a singly-linked list. Which fields and methods have to be added to ListNode to make it suitable for doubly-linked lists? A field previous and methods getPrevious and setPrevious. What is a circular list? A list where the next reference in the last node is not null but refers back to the first node. In an empty circular list with a header node, what are the values of next and previous in header? Both refer to header.


Download ppt "Java Methods Lists and Iterators Object-Oriented Programming"

Similar presentations


Ads by Google