Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.

Slides:



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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Linked Lists Linear collections.
Double-Linked Lists and Circular Lists
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.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Linked Lists Dr. Tim Margush University of Akron © 2009.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
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.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Tirgul OOP No.3 Iterators. What is an Iterator? An object that provides a way to access elements of an aggregate object sequentially, without exposing.
List Interface CS java.util.List Interface and its Implementers CS340 2.
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.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Java Methods Lists and Iterators Object-Oriented Programming
Single-Linked Lists.
Linked Lists Chapter 5 (continued)
Announcements & Review
Algorithm for deleting a node from a singly linked list
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Java collections library
Introduction to Data Structures
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists, Stacks and Queues Textbook Sections
Lecture 26: Advanced List Implementation
Chapter 15 – An Introduction to Data Structures
Linked Lists [AJ 15].
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Example: LinkedSet<T>
Recursive Objects Singly Linked Lists.
ITI Introduction to Computing II Lab-12
Linked Lists Chapter 5 (continued)
Presentation transcript:

Recursive Objects Singly Linked List (Part 2) 1

Operations at the head of the list  operations at the head of the list require special handling because there is no node before the head node 2

Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 3 'a' 'x' LinkedList head 'z'

Adding to the front of the list  must connect to the rest of the list 4 'a' 'x' LinkedList head 'z'

Adding to the front of the list  then re-assign head of linked list 5 'a' 'x' LinkedList head 'z'

6 /** * Inserts the specified element at the beginning of this list. * c the character to add to the beginning of this list. */ public void addFirst(char c) { Node newNode = new Node(c); newNode.next = this.head; this.head = newNode; this.size++; }

Adding to the middle of the list  adding to the middle of the list  t.add(2, 'z') 7 'a' 'x''r''a' 's' 'z'

Adding to the middle of the list  must connect to the rest of the list 8 'a' 'x''r''a' 's' 'z'

Adding to the middle of the list  then re-assign the link from the previous node  notice that we to know the node previous to the inserted node 9 'a' 'x''r''a' 's' 'z'

10 /** * Insert an element at the specified index in the list. * index the index to insert at c the character to insert */ public void add(int index, char c) { if (index this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } if (index == 0) { this.addFirst(c); } else { LinkedList.add(index - 1, c, this.head); this.size++; } recursive method

11 /** * Insert an element at the specified index after the * specified node. * index the index after prev to insert at c the character to insert prev the node to insert after */ private static void add(int index, char c, Node prev) { if (index == 0) { Node newNode = new Node(c); newNode.next = prev.next; prev.next = newNode; return; } LinkedList.add(index - 1, c, prev.next); }

Removing from the front of the list  removing from the front of the list  t.removeFirst() or t.remove(0)  also returns the element removed 12 'a' 'x' LinkedList head 'z'

Removing from the front of the list  create a reference to the node we want to remove Node curr = this.head; 13 'a' 'x' LinkedList head 'z' curr

Removing from the front of the list  re-assign the head node this.head = curr.next; 14 'a' 'x' LinkedList head 'z' curr

Removing from the front of the list  then remove the link from the old head node curr.next = null; 15 'a' 'x' LinkedList head 'z' curr

16 /** * Removes and returns the first element from this list. * the first element from this list */ public char removeFirst() { if (this.size == 0) { throw new NoSuchElementException(); } Node curr = this.head; this.head = curr.next; curr.next = null; this.size--; return curr.data; }

Removing from the middle of the list  removing from the middle of the list  t.remove(2) 17 'a' 'x''r''a' 's' 'z'

Removing from the middle of the list  assume that we have references to the node we want to remove and its previous node 18 'a' 'x''r''a' 's' 'z' curr prev

Removing from the middle of the list  re-assign the link from the previous node prev.next = curr.next; 19 'a' 'x''r''a' 's' 'z' curr prev

Removing from the middle of the list  then remove the link from the current node curr.next = null; 20 'a' 'x''r''a' 's' 'z' curr prev

21 /** * Removes the element at the specified position in this list * index the index of the element to be removed the element previously at the specified position */ public char remove(int index) { if (index = this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } if (index == 0) { return this.removeFirst(); } else { char result = LinkedList.remove(index - 1, this.head, this.head.next); this.size--; return result; } recursive method

22 /** * Removes the element at the specified position relative to the * current node. * index * the index relative to the current node of the * element to be removed prev * the node previous to the current node curr * the current node the element previously at the specified position */ private static char remove(int index, Node prev, Node curr) { if (index == 0) { prev.next = curr.next; curr.next = null; return curr.data; } return LinkedList.remove(index - 1, curr, curr.next); }

Implementing Iterable  having our linked list implement Iterable would be very convenient for clients // for some LinkedList t for (Character c : t) { // do something with c } 23

Iterable Interface public interface Iterable Implementing this interface allows an object to be the target of the "foreach" statement. 24 Iterator iterator() Returns an iterator over a set of elements of type T.

Iterator  to implement Iterable we need to provide an iterator object that can iterate over the elements in the list public interface Iterator An iterator over a collection. 25 booleanhasNext() Returns true if the iteration has more elements. E next() Returns the next element in the iteration. voidremove() Removes from the underlying collection the last element returned by this iterator (optional operation).

Recursive Objects (Part 3) 26

LinkedList Iterator  think of the iterator as lying between elements in the list (like a cursor) 27 'a' 'x' LinkedList head 'r' i iterator between 'x' and 'r'

LinkedList Iterator  think of the iterator as lying between elements in the list (like a cursor) 28 'a' 'x' LinkedList head 'r' i iterator at the start of the iteration (between nothing and 'a')

LinkedList Iterator  because the iterator is between elements, there is a current element and next element of the iteration 29 'a' 'x' LinkedList head 'r' i iterator between 'x' and 'r' current element next element

LinkedList Iterator  the current element is null at the start of the iteration 30 'a' 'x' LinkedList head 'r' i iterator at the start of the iteration (between nothing and 'a') next element null

LinkedList Iterator  the next element is null at the end of the iteration 31 'a' 'x' LinkedList head 'r' i iterator between 'x' and nothing current element null

LinkedList Iterator  both the current and next elements are null if the list is empty 32 LinkedList head i iterator at the start of the iteration null

LinkedList Iterator: hasNext  hasNext() returns true if there is at least one more element in the iteration 33 'a' 'x' LinkedList head 'r' i i.hasNext() is true next element

LinkedList Iterator: hasNext  hasNext() returns false at the end of the iteration 34 'a' 'x' LinkedList head 'r' i i.hasNext() is false current element null

LinkedList Iterator: next  invoking next() returns the next element… 35 'a' 'x' LinkedList head 'r' i i.next() == 'a' is true next element

LinkedList Iterator: next  and causes the iterator to move to its next position in the iteration 36 'a' 'x' LinkedList head 'r' i current element next element

LinkedList Iterator: next  invoking next() at the end of the iteration causes a NoSuchElementException to be thrown 37 'a' 'x' LinkedList head 'r' i i.next() causes a NoSuchElementException current element null

LinkedList Iterator: remove  remove() causes the element most recently returned by next() to be removed from the linked list 38 'a' 'x' LinkedList head 'r' i i.remove() causes 'x' to be removed current element next element

LinkedList Iterator: remove  notice that the iterator needs to know what was the previous element of the iteration 39 'a' LinkedList head 'r' i next element previous element

LinkedList Iterator: remove  after removing the element the current element and previous element are the same 40 'a' LinkedList head 'r' i next element previous element current element

LinkedList Iterator: remove  invoking remove() a second time causes an IllegalStateException to be thrown 41 'a' LinkedList head 'r' i next element i.remove() causes an IllegalStateException previous element current element

LinkedList Iterator: remove  invoking remove() before calling next() also causes and IllegalStateException to be thrown 42 'a' 'x' LinkedList head 'r' i next element i.remove() causes an IllegalStateException null no current or previous element

LinkedList Iterator: remove  note that using an iterator and remove() is the safest way to iterate over a collection and selectively remove elements from the collection  called filtering 43

LinkedList Iterator: remove // removes vowels from our LinkedList t for (Iterator i = t.iterator(); i.hasNext(); ) { char c = i.next(); if (String.valueOf(c).matches("[aeiou]")) { System.out.println("removing " + c); i.remove(); } 44

Implementation  currNode  reference to the node most recently returned by next()  this means that currNode is null at the start of the iteration  requires special treatment in methods  prevNode  reference to the node previous to currNode  needed for remove() 45

Implementation: Attributes and Ctor private class LinkedListIterator implements Iterator { private Node currNode; private Node prevNode; public LinkedListIterator() { this.currNode = null; this.prevNode = null; } 46

Implementation: public boolean hasNext() { if (this.currNode == null) { return head != null; } return this.currNode.next != null; } 47

Implementation: public Character next() { if (!this.hasNext()) { throw new NoSuchElementException(); } this.prevNode = this.currNode; if (this.currNode == null) { this.currNode = head; } else { this.currNode = this.currNode.next; } return this.currNode.data; } 48

Implementation: public void remove() { if (this.prevNode == this.currNode) { throw new IllegalStateException(); } if (this.currNode == head) { head = this.currNode.next; } else { this.prevNode.next = this.currNode.next; } this.currNode = this.prevNode; size--; } 49

LinkedList Summary  each node can be thought of as the head of a smaller list 50 'a' 'x' LinkedList head 'r''a' head of ['a', 'x', 'r', 'a']

LinkedList Summary  each node can be thought of as the head of a smaller list 51 'a' 'x' LinkedList head 'r''a' head of ['x', 'r', 'a']

LinkedList Summary  each node can be thought of as the head of a smaller list 52 'a' 'x' LinkedList head 'r''a' head of ['r', 'a']

LinkedList Summary  each node can be thought of as the head of a smaller list 53 'a' 'x' LinkedList head 'r''a' head of ['a']

LinkedList Summary  the recursive structure of the linked list leads to recursive algorithms that operate on the list private static boolean contains(char c, Node node) { if (node.data == c) { return true; } if (node.next == null) { return false; } return LinkedList.contains(c, node.next); } 54

LinkedList Summary  nodes are an implementation detail  the client only cares about the elements (characters) in the list  Node is implemented as a private static inner class  private so that only LinkedList can use it  static because Node does not need access to any non-static attribute of LinkedList 55

LinkedList Summary  by implementing the Iterable interface we give clients the ability to iterate over the elements of the list  clients expect to be able to do this for most collections // for some LinkedList t for (Character c : t) { // do something with c } 56

LinkedList Summary  to implement Iterable we need to provide an iterator object that can iterate over the elements in the list public interface Iterator An iterator over a collection. 57 booleanhasNext() Returns true if the iteration has more elements. E next() Returns the next element in the iteration. voidremove() Removes from the underlying collection the last element returned by this iterator (optional operation).