List Interface and Linked List Mrs. Furman March 25, 2010.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Introduction to Computation and Problem
Lists CS 3358.
Linked Lists.
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Concrete collections in Java library
Double-Linked Lists and Circular Lists
John Hurley Cal State LA
The List ADT Textbook Sections
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
M180: Data Structures & Algorithms in Java
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
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 the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
1 Collections Working with More than One Number or Data Type or Object.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
LinkedList Many slides from Horstmann modified by Dr V.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(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.
Linked Structures, LinkedStack
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Unit – I Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Ch7. List and Iterator ADTs
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Announcements & Review
Programming Abstractions
Linked Lists Chapter 6.5, 17 CSCI 3333 Data Structures.
Introduction to Data Structures
Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Collections Framework
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

List Interface and Linked List Mrs. Furman March 25, 2010

List Interface  A class that implements List is a sequence of elements.  Duplicates are allowed.  Indexes start at 0 (first index) … length -1  A list allows you to:  Access an element at any position in the list using its integer index.  Insert an element anywhere in the list  Iterate over all elements using ListIterator or Iterator.

List Interface Methods of List: boolean add(Object obj) – adds element at end void add (int index, Object obj) – inserts at position int size() – returns number of elements Object get (int index) – returns the Object at the index Object set(int index, Object obj) – sets the element at the index to obj. Iterator iterator() ListIterator <> listIterator()

Some reminders… ArrayList is an array implementation of List interface. ArrayLists can change size at run-time, while an array has a fixed size. Shifting elements caused by insertion and deletion is handled automatically with ArrayLists.

ArrayList Methods for ArrayList: same as methods for List, since List is an interface, we have to implement each one of their methods.

Side Notes on ArrayList add, get, remove and set all take indexes, that can be out of bounds, and therefore throw exceptions. get, remove, and set are out of bounds if.. index = size() index = size() add can use the index that is equal to size… so index size() is out of bounds.

Why have a List interface?  We use interfaces when we have multiple abstract commonalities between classes.  There are multiple ways we can represent a List  There are other types of lists that implement the List interface.

Linked Lists  Differ from Arraylist because storage isn’t necessarily all together in memory.  Each element (node) stores two things:  it value  a reference to where the next element is located  The last element stores a null pointer as the location of the next element.  Dynamic Storage: grows and shrinks during run- time! When an element is no longer being referred to  Java has automatic garbage collection.

Several Types of Linked Lists  Linear – links in one direction. From the first node  the last node.  Doubly Linked – link in both directions, so that you can go forward and backward through the list  Circular Linked – The last node refers back to the first.

LinkedList methods  LinkedList() – constructor  void addFirst(Object obj) – inserts obj at the front of the list.  void addLast (Object obj) – appends obj to the end of the list.  Object getFirst() – returns first element  Object getLast() – returns the last element  Object removeFirst() – Removes and returns the first element in the list  Object removeLast() – Removes and returns the last element in the list.

ArrayList vs. LinkedList Insert at front add(0, obj) O(n) shifts all the element addFirst (obj) O(1). Insert at End add(obj)O(1) O(n) – adding n elements add(obj)O(1) Delete at front remove(0) O(n) – shifting n elements down removeFirst()O(1) Delete at End remove (size() -1 ) O(1)removeLast()O(1) Insert in middle add(Index, obj) O(1) – find the spot O(n) – insert the element. itr.add() O(n) – find the spot O(1) – insert.

Delete in middle remove (index) O(1) – access O(n) – shift the elements into the void itr.remove() O(n) – access to insertion point O(1) – delete Change value in middle set (index, obj) O(1). Fast access set (index, obj) O(n) traversal to location element For most applications ArrayList is faster! ArrayList has fast access to any element, while LinkedList has to traverse the list to get to anything but 1 st and last. LinkedList needs to allocate a node for each element in the list whereas ArrayList does not. Use LinkedLists for: Programs that require frequent additions to front of list. If you need to iterate through the entire list deleting elements as you go

Exercises Which of the following will compile without error? 1. List list = new List(); 2. List list = new ArrayList(); 3. List list = new LinkedList(); 4. ArrayList list = new List(); 5. ArrayList list = new ArrayList(); 6. LinkedList list = new List(); 7. LinkedList list = new ArrayList();

Using an Iterator  It is often helpful to use a ListIterator rather than a loop

Iterators ListIterator interface methods: next – gets the next element of the collection hasNext – returns if there is a next element remove – removes the current element from the collection.

Iterators Cont.  Each collection class defines it own implementation of the Iterator methods in a class that’s invisible to the user of the collection  An Iterator object is created using the iterator method.  Iterator itr = c.iterator();  This point itr at the start of the collection. All elements are returned in proper order.

Iterators Cont.  Traversal through a list with an iterator: for (itr = c.iterator(); itr.hasNext();) { /*code with a call to itr.next() which advances itr, so we do not need a step expression in our for loop*/ } List, ArrayList and LinkedList have a expanded Iterator… ListIterator, which allows us to move in both directions, as well as add() and set()

Divide into 2 groups  You will each be given the first node of a list. Please record the value of this node, and iterate through the list, finding each of the other nodes in the list. When you reach the null pointer, you can return to the room, and output your list to the board. Have Fun!!