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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Chapter 4.
Queues and Linked Lists
Linked Lists Linear collections.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
COMP 103 Linked Stack and Linked Queue.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
DataStructures1 Barb Ericson Georgia Tech July 2008.
Stacks and Queues Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
LinkedList Many slides from Horstmann modified by Dr V.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Chapter 5 Linked Lists II
CS2006- Data Structures I Chapter 5 Linked Lists III.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
CS 367 Introduction to Data Structures Lecture 5.
List Interface and Linked List Mrs. Furman March 25, 2010.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Java Methods Lists and Iterators Object-Oriented Programming
CSE 373 Implementing a Stack/Queue as a Linked List
8-1.
8-1.
Java Methods Stacks and Queues A & AB Object-Oriented Programming
A Data Structure Bestiary
Computer Science and Engineering
Linked Lists & Iterators
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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

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 0value 1value 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 0value 1value 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 ABCD 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 { private ListNode head;... public Iterator iterator () { return new SinglyLinkedListIterator (head); }... }

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

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

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

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

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

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?