08-Lists - 2/17/2011 List Overview CS/ENGRD 2110...

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

View Full Document Right Arrow Icon
2/17/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 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 DocumentRight Arrow Icon
2/17/2011 2 Class ListCell class ListCell<T> { private T datum; private ListCell<T> next; public ListCell(T datum, ListCell<T> next){ this.datum = datum; this.next = next; } public T getDatum() { return datum; } public ListCell<T> getNext() { return next; } public void setDatum(T obj) { datum = obj; } public void setNext(ListCell<T> c) { next = c; }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

08-Lists - 2/17/2011 List Overview CS/ENGRD 2110...

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

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