{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-Lists - List Overview CS/ENGRD 2110 Object-Oriented...

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

View Full Document Right Arrow Icon
6/15/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2011 Thorsten Joachims Lecture 8: Lists List Overview Purpose Maintain an ordered set of elements (with possible duplication) Common operations Create a list Access elements of a list sequentially Insert elements into a list Delete elements from a list Arrays Random access : ) Fixed size: cannot grow or shrink after creation : ( Linked Lists No random access : ( Can grow and shrink dynamically : ) 2 A Simple List Interface Often also: Insert at last position, insert at position i Get first element, get last element Reverse Etc. 3 public interface List<T> { public void insert(T element); // add to front public void delete(T element); public boolean contains(T element); public int size(); public String toString(); } Generic Types …in a Nutshell “List<E>” is read as “List of E”. To use the interface List<E> , supply an actual type argument, e.g., List<Integer>: List<Integer> list = new LinkedList<Integer>(); All occurrences of the formal type parameter ( E in this case) are replaced by the actual type argument ( Integer in this case) 4 public interface List<E> { // E is a type variable void insert(E x); } public class LinkedList<E> implements List<E> { void insert(E x) { … } } List Data Structures Array Must specify array size at creation Insert, delete require moving elements Must copy array to a larger array when it gets full Linked list uses a sequence of linked cells we will define a class ListCell from which we build lists 5 24 -7 87 78 empty 24 -7 87 78 List Terminology Head = first element of the list Tail = rest of the list 6 10 84 -7 1 33 tail head 21
Background image of page 1

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

View Full Document Right Arrow Icon