100_lecture26

100_lecture26 - Introduction to Computation and Problem...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 26: Class 26: Linked Lists Linked Lists 2 The Java Collection Classes The java.util package contains implementations of many data structures that we are also going to discuss and implement in a simpler way in class. You are welcome to use the standard Java implementations in problem sets. They are more elaborate and abstract than the implementations we will develop. Learning how to implement a few data structures makes you more sophisticated in how you use all data structures. We will cover the Java implementations in a later lecture. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Goals data structure (a Linked List) using linking rather than arrays. data structure is as much as its interface (set of methods). We will examine and build a new kind of The most important theme, however, is that implementation affects how useful a 4 A list is a collection of elements that has a order an element at a time. Lists as an Abstract Data Type particular order. It can have arbitrary length. You should be able to insert or delete an element anywhere. You should be able to go through the list in 2
Background image of page 2
5 A List Interface public interface List<E> { public boolean isEmpty(); public void addFirst( E e ); public void addLast( E e ); public boolean contains( E e ); public boolean remove( E e ); public E removeFirst() throws NoSuchElementException; public void clear(); public int size(); } 6 There are certain obvious things we would like to do with lists that we can't do using end? One approach is to number or index the positions in the list. Lists and Ordinal Position only this interface. Two examples: How would you access the elements of the list if you did not know them in advance? How would you insert an item into the list at any other position than the beginning and the 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Indexed Lists We could then add three methods public E get( int n ) throws IndexOutOfBoundsException; public E remove( int n ) throws IndexOutOfBoundsException; public void add( E e, int n ) throws IndexOutOfBoundsException; for ( int i = 0; i < myList.size(); i++ ) { E e = myList.get( i ); . . . } The following code block will traverse an indexed list, myList: 8 Indexed Lists, 2 ArrayList ) Lists implemented using arrays (e.g., Java’s often provide such methods because they are easy to implement. The idea of using an index to access list members can lead to problems, however. Since the index depends on ordinal position, it can change every time an item is added to or deleted from the list. If the list is not implemented on top of an indexed data structure like an array, accessing an indexed element is slow. When we use large lists like a telephone directory in
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

100_lecture26 - Introduction to Computation and Problem...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online