Linked Lists & Iterators

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
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.
© 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 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.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Iterators and Sequences
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sequences and Iterators
COMP9024: Data Structures and Algorithms
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Data Structures Linked list.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Sequences and Iterators
Array Lists, Node Lists & Sequences
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Copyright © Aiman Hanna All rights reserved
" A list is only as strong as its weakest link. " - Donald Knuth
Ordered Maps & Dictionaries
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
LAB#4 Linked List.
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Problem Understanding
Presentation transcript:

Linked Lists & Iterators Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Linked Lists & Iterators Coverage Singly Linked Lists Doubly Linked Lists Circularly Linked Lists Iterators Linked Lists & Iterators

Linked Lists & Iterators Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem  A B C D Linked Lists & Iterators

The Node Class for List Nodes public class Node { // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node() { this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; // Accessor methods: public Object getElement() { return element; public Node getNext() { return next; // Modifier methods: public void setElement(Object newElem) { element = newElem; public void setNext(Node newNext) { next = newNext; Linked Lists & Iterators

Linked Lists & Iterators Inserting at the Head Allocate a new node Insert new element Have new node point to old head Update head to point to new node Linked Lists & Iterators

Linked Lists & Iterators Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node Linked Lists & Iterators

Linked Lists & Iterators Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Linked Lists & Iterators

Linked Lists & Iterators Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Linked Lists & Iterators

Stack as a Linked List (Review Section 5.1.3) We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t  elements Linked Lists & Iterators

Queue as a Linked List (Review Section 5.2.3) We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements Linked Lists & Iterators

Linked Lists & Iterators Doubly Linked List A doubly linked list provides a natural implementation of the Node List ADT Nodes implement Position and store: element link to the previous node link to the next node Special sentinel/dummy trailer and header nodes (simplify implementation) prev next elem node trailer header nodes/positions elements Linked Lists & Iterators

Linked Lists & Iterators Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C Linked Lists & Iterators

Linked Lists & Iterators Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Linked Lists & Iterators

Linked Lists & Iterators Deletion We visualize remove(p), where p = last() A B C D p A B C p D A B C Linked Lists & Iterators

Linked Lists & Iterators Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t Linked Lists & Iterators

Linked Lists & Iterators Performance In the implementation of the List ADT by means of a doubly linked list The space used by a list with n elements is O(n) The space used by each position of the list is O(1) Operations of the List ADT run in O(1) time (as will be discussed later) Operation element() of the Position ADT also runs in O(1) time (as will be discussed later) Linked Lists & Iterators

Linked Lists & Iterators An iterator abstracts the process of scanning through a collection of elements It maintains a cursor that sits between elements in the list, or before the first or after the last element Methods of the Iterator ADT: hasNext(): returns true so long as the list is not empty and the cursor is not after the last element next(): returns the next element Extends the concept of position by adding a traversal capability Linked Lists & Iterators

Linked Lists & Iterators Iterable Classes An iterator is typically associated with an another data structure, which can implement the Iterable ADT We can augment the different ADTs (i.e. Stack, Queue, List, etc. ) with method: Iterator<E> iterator(): returns an iterator over the elements In Java, classes with this method extend Iterable<E> Two notions of iterator: snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure In Java: an iterator will fail (and throw an exception) if the underlying collection changes unexpectedly Linked Lists & Iterators

Linked Lists & Iterators The For-Each Loop Java provides a simple way of looping through the elements of an Iterable class: for (type name: expression) loop_body For example: List<Car> ls1; for (Car c : ls1) System.out.println(c); // Car is assumed to have a toString method defined. Linked Lists & Iterators

Linked Lists & Iterators List Iterators in Java Java uses a the ListIterator ADT for node-based lists. This iterator includes the following methods: add(e): add e at the current cursor position hasNext(): true if there is an element after the cursor hasPrevious: true if there is an element before the cursor previous(): return the element e before the cursor and move cursor to before e next(): return the element e after the cursor and move cursor to after e set(e): replace the element returned by last, next or previous operation with e remove(): remove the element returned by the last, next or previous method Linked Lists & Iterators