Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.

Similar presentations


Presentation on theme: "COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping."— Presentation transcript:

1 COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut. Albert Einstein, Observer, Jan. 15, 1950

2 COSC 1P03 Data Structures and Abstraction 10.2 List most general of list-oriented collections an ordered collection (initially empty) of items (of some type) to which items may be added and removed. cursored-list cursor operations relative to cursor off list errors off list list overflow

3 COSC 1P03 Data Structures and Abstraction 10.3 List Operations insertion? before or after cursor? at front at end? access at cursor off list? deletion at cursor off list? cursor after deletion inverse of addition

4 COSC 1P03 Data Structures and Abstraction 10.4 traversal moving to front advancing end of list search key from where exhaustive search list as stack insert without advance list as queue insert with traverse to end sequential order insert with advance sorted traverse to find insertion point

5 COSC 1P03 Data Structures and Abstraction 10.5 List Interface generic E getKey Comparable from java.lang operations insertion deletion access length traversal search off end? iterator exceptions NoSpaceException NoItemException

6 COSC 1P03 Data Structures and Abstraction 10.6 Iterators a device that allows traversal through a collection ADT ADT extends Iterable (from java.util ) iterator returns an Iterator over the ADT interface Iterator in java.util methods UnsupportedOperationException extended for implementation must have intimate knowledge of ADT representation place in same package & use package visibility in ADT

7 COSC 1P03 Data Structures and Abstraction 10.7 List ADT Contiguous Representation variable-sized array contiguity reorganization ( O(n) ) cursor is an index instance variables constructors empty list operations insertion create opening shift items R L cursor

8 COSC 1P03 Data Structures and Abstraction 10.8 deletion fill gap shift items L R cursor removal of reference at length-1 find goal: move cursor check each item in turn until found or off list short circuit operator negation using de Morgans law iterator create an iterator on this ADT remaining operations

9 COSC 1P03 Data Structures and Abstraction 10.9 ConListIterator generic in item type (same as ConList ) iteration involves sequencing through index positions from 0 to length-1 instance variables list it is iterating has its own cursor must keep track of current position constructor package visibility only classes in package can create initialize cursor methods access ConList instance variables directly (package visibility)

10 COSC 1P03 Data Structures and Abstraction List ADT Linked Implementation sequentially-linked structure sequential processing natural cursor? cursor is Node reference off list insertion in front of cursor precursor cursor pairs header node at end?

11 COSC 1P03 Data Structures and Abstraction representation sequentially linked structure with header and two cursors empty list off list? length keep count comparison with contiguous insert/remove O(1) vs O(n)

12

13 Cursor must be moved to the front of the list, First item. Try to match the "key' with an item in the list which has the same key. If find did not locate an item with "key" then the cursor will be off the list. Thus we break out of the search routine. Must of found something, so process the item. Before we search for another item with the same key, the cursor must be advanced 1 unit. The search will now start from the current cursor position. Exhaustive Search

14

15 Some data fields may be missing for some students Where is the data???? Grade Report Data File

16 COSC 1P03 Data Structures and Abstraction Keyed package Collections; /**This interface defines the requirements for items that **are to be placed on a list. The items must have a key **which can be compared. Key comparison is used to determine **a match in List.find. In this case comparing strings. ** public interface Keyed { /**This method returns the key of the item. ** key of the item.*/ public String getKey ( ); }// E Must be implemented within the implementation class.

17 COSC 1P03 Data Structures and Abstraction List Interface package Collections; import java.util.*; public interface List extends Iterable { public void add ( E item ); public E remove ( ); public E get ( ); public boolean empty ( ); public int length ( ); public void toFront ( ); public void advance( ); public void find ( String key ); public boolean offEnd ( ); public Iterator iterator ( ); }// List Lists interface is now restricted to Parametric subtypes of Keyed Most keys are Strings or can have String equivalents, key is of type String. Extend the Iterable interface so an implementation supports the same type E as does the ADT Include the interator method which comes from java.util.

18 COSC 1P03 Data Structures and Abstraction Contiguous List package Collections; import java.io.*; public class ConList implements List, Serializable { private E[]items;// the items in the list private intcursor;// the list cursor private intlength;// the length of the list /**This constructor creates a new empty list capable of holding **100 items.*/ public ConList ( ) { this(100); };// constructor public ConList ( int size ) { items = (E[]) new Keyed[size]; cursor = 0; length = 0; };// Constructor Array of items of type E Cursor indexes the array. Number of items in the list Create an items array of E object of SIZE size. Cursor and length are initialized of valid starting values. Create a ConList implementation with the same subtype Keyed as the interface.

19 COSC 1P03 Data Structures and Abstraction Contiguous List. public void add ( E item ) { intj; if ( length >= items.length ) { throw new NoSpaceException(); } else { for ( j = length-1 ; j>=cursor ; j-- ) { items[j+1] = items[j]; }; items[cursor] = item; length = length + 1; }; };// add public E remove ( ) { Ei; intj; if ( cursor >= length ) { throw new NoItemException(); } else { i = items[cursor]; for ( j=cursor+1 ; j

20 COSC 1P03 Data Structures and Abstraction Contiguous List.. public E get ( ) { if ( cursor >= length ) { throw new NoItemException(); } else { return items[cursor]; }; };// get public boolean empty ( ) { return length == 0; };// empty public int length ( ) { return length; };// length If cursor is not indexing a valid item, then raise an exception. Otherwise, return the item but leave it in the list. Empty when there are no item in the list. Return the number of items in the list.

21 COSC 1P03 Data Structures and Abstraction Contiguous List... public void toFront ( ) { cursor = 0; };// toFront public void advance( ) { if ( cursor < length ) { cursor = cursor + 1; }; };// advance public void find ( String key ) { while ( cursor < length && key.compareTo(items[cursor].getKey()) != 0 ) { cursor = cursor + 1; }; };// find public boolean offEnd ( ) { return cursor >= length; };// offEnd Set the cursor to the beginning of the list. Increment the cursor by 1 as long as the cursor is still in the valid range of the list. i.e. array bounds. The item object must implement getKey to return a comparable type. Returns a String compareTo is then used to check for equality. The search will stop when the key is found or the list is exhausted. If the cursor is referencing a valid index or not.

22 COSC 1P03 Data Structures and Abstraction Contiguous List.... public Iterator iterator ( ) { return new ConListIterator (this); }; // iterator Interface defines that List extends iterator. Implementation defines that the iterator method must be implemented. Method will return an iterator of over type E Create the iterator object from an implementation of the iterator based on the ConList ADT.

23 COSC 1P03 Data Structures and Abstraction KeyedChar class KeyedChar implements Keyed { char theChar; KeyedChar ( char c ) { theChar = c; }; // constructor public String getKey( ) { // for Keyed return String.valueOf(theChar); }; // getKey } // KeyedChar

24 COSC 1P03 Data Structures and Abstraction Test Lists public TestLists ( ) { List l; // a list //Code which puts KeyedChar items onto the list out.writeString("Traversal using List methods"); out.writeEOL(); l.toFront(); while ( ! l.offEnd() ) { out.writeChar(l.get().theChar); l.advance(); }; out.writeEOL(); out.writeString("Traversal using iterator"); out.writeEOL(); java.util.Iterator j = l.iterator(); while(j.hasNext()){ out.writeString(j.next().getKey()); } out.writeEOL(); for (KeyedChar aKeyChar:l){ out.writeString(aKeyChar.getKey()); } out.writeEOL(); } Create the iterator j from the list implementation While loop runs while the iterator has more items. Next returns an item defined by the parametric value E which in this case is a KeyedChar The getKey method returns a string representation of the char. Java expands this expression to return an iterator over the list l. aKeyChar can then be referenced directly.

25 COSC 1P03 Data Structures and Abstraction ConListIterator class ConListIterator implements Iterator { int cursor; ConList list; ConListIterator ( ConList l ) { list = l; cursor = 0; }; // constructor public boolean hasNext ( ) { // from Iterator return cursor < list.length; }; // hasNext public E next ( ) { // from Iterator E i; if ( cursor >= list.length ) { throw new NoSuchElementException(); } else { i = list.items[cursor]; cursor = cursor + 1; return i; } }; // next public void remove ( ) { // from Iterator throw new UnsupportedOperationException(); }; // remove Implements the iterator interface over the Object E which is a Keyed subtype. Local Pointer to the list and a local cursor index. Constructor assigns the list to a local variable and initializes cursor to the first element in the sequence. If local cursor is within the bounds of the list. Standard over indexing procaution. Save current list object to local variable. Advance the cursor Return the object. Implementation is Optional, if we dont throw this exception. Must implement the type E ConList which is a Keyed subtype

26 COSC 1P03 Data Structures and Abstraction Extended For

27 COSC 1P03 Data Structures and Abstraction The End


Download ppt "COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping."

Similar presentations


Ads by Google