TCSS 143, Autumn 2004 Lecture Notes

Slides:



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

Linked Lists Linear collections.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
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.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
(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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
EKT472: Object Oriented Programming
Chapter 5: The List Abstract Data Type
Elementary Data Structures
The List ADT.
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Marcus Biel, Software Craftsman
Linked Lists Chapter 5 (continued)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Announcements & Review
Programming Abstractions
Implementing ArrayList Part 1
Top Ten Words that Almost Rhyme with “Peas”
Topic 11 Linked Lists -Joel Spolsky
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
ArrayLists 22-Feb-19.
slides created by Marty Stepp
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Header and Trailer Sentinels
Linked Lists Chapter 5 (continued)
CSC 143 Java Linked Lists.
ArrayLists.
ArrayLists 27-Apr-19.
Building Java Programs
slides created by Marty Stepp
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
CSE 373 Set implementation; intro to hashing
Java Generics & Iterators
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

TCSS 143, Autumn 2004 Lecture Notes Implementation of Array Lists and Linked Lists

Java's List interface: review Java has an interface java.util.List to represent a list of objects. It adds the following methods to those in Collection: public void add(int index, Object o) Inserts the specified element at the specified position in this list. public Object get(int index) Returns the element at the specified position in this list. public int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain it.

List interface, cont'd. More java.util.List methods: public int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain it. public Object remove(int index) Removes the object at the specified position in this list. public Object set(int index, Object o) Replaces the element at the specified position in this list with the specified element.

ArrayList implementation Let's examine instructor's provided IntArrayList class, to get an idea of how ArrayList works An IntArrayList stores a list of ints and mimics many of the methods of java.util.ArrayList simpler to examine than a real ArrayList, but limited (cannot be used for other data types)

reminder: ArrayList problems an insertion into a full list causes a large reallocation an insertion to the front forces us to "slide" down the subsequent items a removal also forces us to "slide" down subsequent items still need to use indexes/subscripts a lot the syntax for using it is somewhat ugly

The underlying issue the elements of an ArrayList are too tightly attached; can't easily rearrange them can we break the element storage apart into a more dynamic and flexible structure?

Idea: a list of linked nodes create a special Node object that represents one storage slot to hold one element of the list each node will keep a reference to the node after it (the "next" node) the last node will have next == null (drawn as / ), signifying the end of the list

A linked list linked list: a collection that provides the list operations named previously, implemented using a linked sequence of nodes the list only needs to keep a reference to the first node (we might name it myFront)

LinkedList Class java.util.LinkedList implements List using linked nodes as the internal implementation

Some list states of interest empty list (myFront == null) list with one element list with many elements

Let's draw them together... an add operation at the front, back, and middle a remove operation a get operation a set operation an index of (searching) operation

Node implementation (int) /* Stores one element of a linked list. */ public class IntNode { public int data; public Node next; public Node(int data) { this(data, null); } public Node(int data, Node next) { this.data = data; this.next = next;

Linked list implementation /* Models an entire linked list. */ public class IntLinkedList { private Node myFront; public IntLinkedList() { myFront = null; } /* Methods go here */ fill unimplemented methods with thrown UnsupportedOperationException

Linked list methods // inserts given value at front public void addFirst(int value) // returns true if no nodes are in list public boolean isEmpty() // returns number of elements public int size()

Linked list methods, cont'd. // returns string representation of list public String toString() // appends given val at end public void addLast(int value) // inserts given value at given index public void add(int index, int value)

Linked list methods, cont'd. // returns value at given index // (exception when index is OOB) public int get(int index) // sets element at given index to // have the given value, and returns it public int set(int index, int value)

Linked list methods, cont'd. / returns index of value in list; // -1 if value is not in the list public boolean indexOf(int value) // returns true if value is in list public boolean contains(int value) // removes all values from list public void clear()

Linked list methods, cont'd. // removes and returns front value // (exception when list is empty) public int removeFirst() // removes and returns rear value public int removeLast() // removes,returns value at index // (exception when index is OOB) public void remove(int index)

Algorithm efficiency, in brief operations that always execute a fixed number of statements are called "constant time" operations often called O(1) : "big-Oh of 1" or "order of 1" operations that take longer to perform if our list is longer are called "linear time" operations often called O(n) : "big-Oh of n" or "order of n", where n means the size of the list O(n) operations are slower and we want to avoid them when we can

Which methods are O(n)? METHOD addFirst(int) isEmpty() size() toString() addLast(int) add(int, int) get(int) set(int, int) RUNTIME (Big-Oh) = O(_____)

Which methods are O(n)? Can we categorize which operations are slow? indexOf(int) contains(int) clear() removeFirst() removeLast() remove(int) RUNTIME (Big-Oh) = O(_____) note which of them were slow because we had to get to the end of the list... (candidates for improvement by myBack reference) Can we categorize which operations are slow? What does this say about how to use our list?

A particularly slow idiom // print every element of the list for (int i = 0; i < list.size(); i++) { int element = list.get(i); System.out.println(i + ": " + element); } This code executes an O(n) operation (get) every time through a loop that runs n times! Its runtime is O(n2), which is much worse than O(n) this code will take prohibitively long to run for large data sizes

The problem of position The code on the previous slide is wasteful because it throws away the position each time it would be much better if we could somehow keep the list in place at each index as we looped through it Java uses special objects to represent a position of a list as it's being examined... these objects are called "Iterators"

Iterators in Java interface java.util.Iterator public boolean hasNext() Returns true if there are more elements to see. public Object next() Returns the next object in this collection. Throws an exception if no more elements remain. public Object remove() Deletes the element that was last returned by next.

Iterator usage example ArrayList names = new ArrayList(); //... // print every name in the list, in upper case Iterator itr = myList.iterator(); while (itr.hasNext()) { String nextElement = (String)itr.next(); System.out.println(nextElement.toUpperCase()); }

Benefits of iterators speed up iteration through linked lists provide a unified way to examine all elements of a collection every collection in Java has an iterator method don't need to look up that collection's method names to see how to use it removes the hassle of managing indexes and worrying about out-of-bounds problems when examining a list's elements in order

An optimization: myBack add a myBack pointer to the last node what benefits does this provide? which methods' Big-Oh runtime improves to O(1)? what complications does this add to the implementation of the methods of the list?

A variation: dummy header dummy header: a front node intentionally left blank myFront always refers to dummy header (myFront will never be null) requires minor modification to many methods surprisingly, makes implementation much easier

References Koffman/Wolfgang Ch. 4, pp. 193-248