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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Linked Lists Dr. Tim Margush University of Akron © 2009.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
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.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
LinkedList Many slides from Horstmann modified by Dr V.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Information and Computer Sciences University of Hawaii, Manoa
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.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
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.
Chapter 5 Linked Lists II
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
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.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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.
Linked Lists and Generics Written by J.J. Shepherd.
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.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Advanced Programming 7/10/20161 Ananda Gunawardena.
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.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Single-Linked Lists.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
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
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Linked Lists [AJ 15].
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Recursive Objects Singly Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
Presentation transcript:

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 head 'z'

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

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

5 /** * 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') 6 'a' 'x''r''a' 's' 'z'

Adding to the middle of the list  must connect to the rest of the list 7 '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 8 'a' 'x''r''a' 's' 'z'

9 /** * 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); }

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++; }

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 11 '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; 12 'a' 'x' LinkedList head 'z' curr

Removing from the front of the list  re-assign the head node this.head = curr.next; 13 '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; 14 'a' 'x' LinkedList head 'z' curr

15 /** * 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) 16 '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 17 '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; 18 '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; 19 'a' 'x''r''a' 's' 'z' curr prev

20 /** * 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); }

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

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 } 22

Iterable Interface public interface Iterable Implementing this interface allows an object to be the target of the "foreach" statement. 23 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. 24 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).