8 Lists - 9/16/2010 List Overview 2 Purpose Maintain an...

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

View Full Document Right Arrow Icon
9/16/2010 1 Lecture 8 CS2110 – Fall 2008 List Overview 2 ± 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 lis 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 : ) A Simple List Interface 3 public interface List<T> { public void insert(T element); public void delete(T element); public boolean contains(T element); public int size(); } List Data Structures ± Array ± Must specify array size at creation ± Insert, delete require moving elements 4 Linked list ² uses a sequence of linked cells ² we will define a class ListCell from which we build lists ± Must copy array to a larger array when it gets full 24 -7 87 78 empty 24 -7 87 78 List Terminology ± Head = first element of the list ± Tail = rest of the list 5 10 84 -7 1 33 7 tail head Class ListCell 6 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 1

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

View Full DocumentRight Arrow Icon
9/16/2010 2 Building a Linked List ± ListCell<Integer> c ± = new ListCell<Integer>(new Integer(24), null); 7 24 c ListCell: 24 –7 87 Integer t = new Integer(24); Integer s = new Integer(-7); Integer e = new Integer(87); ListCell<Integer> p = new ListCell<Integer>(t, new ListCell<Integer>(s, new ListCell<Integer>(e, null))); p ListCell: Building a Linked List (cont’d) 8 24 -7 Integer t = new Integer(24); Integer s = new Integer(-7); Integer e = new Integer(87); //Can also use "autoboxing" p ListCell: Another way:
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.

This note was uploaded on 09/30/2011 for the course CS 2110 taught by Professor Francis during the Fall '07 term at Cornell University (Engineering School).

Page1 / 4

8 Lists - 9/16/2010 List Overview 2 Purpose Maintain an...

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