Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 21 - Maria Litvin, Gary Litvin

14 22 0
Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 21 - Maria Litvin, Gary Litvin

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 21­2 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 21­3 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; } } 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) { return new ListNode (value, head); } x head value value value value value n-1 21­5 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 21­6 Singly-Linked List — Traversal public void printList (ListNode head) { for (ListNode node = head; node != null; node = node.getNext ( )) System.out.println (node.getValue ()); } 21­7 Do-it-Yourself Iterator public class SinglyLinkedList implements Iterable { private ListNode head; public Iterator iterator () { return new SinglyLinkedListIterator (head); } } 21­8 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( ); } 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 • 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 an­1 21­11 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 an­1 21­12 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 an­1 21­13 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? 21­14 ... 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

Ngày đăng: 04/11/2020, 23:18

Từ khóa liên quan

Mục lục

  • Slide 1

  • Objectives:

  • Singly-Linked List

  • Singly-Linked List (cont’d)

  • Singly-Linked List — Example 1

  • Singly-Linked List — Example 2

  • Singly-Linked List — Traversal

  • Do-it-Yourself Iterator

  • Do-it-Yourself Iterator (cont’d)

  • Singly-Linked List with a Tail

  • Doubly-Linked List

  • Doubly-Linked Circular List

  • Doubly-Linked Circular List with a Header Node

  • Review:

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan