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.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Introduction to Computation and Problem
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Lecture 15 Linked Lists part 2
Chapter 17 Linked Lists.
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
List Implementations That Use Arrays
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
ADTs unsorted List and Sorted List
Data Structures Using C++
Chapter 11 Array-Based Lists.
Double-Linked Lists and Circular Lists
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
CSE Lecture 12 – Linked Lists …
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
Review Pseudo Code Basic elements of Pseudo code
Linked Lists.
The List ADT Textbook Sections
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Stack & Queues COP 3502.
Java Review Interface, Casting, Generics, Iterator.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Stacks, Queues, and Deques
Marc Smith and Jim Ten Eyck
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS 367 Introduction to Data Structures Lecture 5.
List Interface and Linked List Mrs. Furman March 25, 2010.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
LINKED LISTS.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 16 Linked Structures
Presentation transcript:

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

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

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

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

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

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

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

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

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)

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?

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)

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

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

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.

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.

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.

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<length ; j++ ) { items[j-1] = items[j]; }; length = length-1; items[length] = null; return i; }; };// remove Determine if there is any more space available in the array. If not then raise an exception. For all items above the cursor, move them up in the array 1 location to make space for the new item. Cursor now indexes an empty cell in the array, so assign the item to that cell. We have added an item to the list, so length must be increased by 1. If the cursor is off of the list, it is not referencing any item. Hence raise exception. Extract the item from the array Move all items above the cursor down 1 cell. Reduce the count of items now in the list. Items have been moved down 1 cell, the last item now invalid and can be removed to allow garbage collection. Return the removed item

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.

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.

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.

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

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.

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

COSC 1P03 Data Structures and Abstraction Extended For

COSC 1P03 Data Structures and Abstraction The End