Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
The List ADT Textbook Sections
CSC 212 – Data Structures Lecture 22: PositionList.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CSC 212 – Data Structures.  Fri., Dec. 17 th from 8AM – 10AM in OM 200  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP 
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Vectors, Lists, and Sequences - Ed. 2 and 3.: Chapter 5 - Ed. 4: Chapter 6.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
CSC 212 – Data Structures Lecture 13: Linked Lists.
Problem Of The Day  Decipher the following phrase: STANDS 0 _
CSC 213 – Large Scale Programming. Today’s Goal  Consider what will be important when searching  Why search in first place? What is its purpose?  What.
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
CSC 212 – Data Structures Lecture 37: Course Review.
Problem of the Day  The place where they signed the Declaration of Independence has the same name now as it did then. What is it?
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
CSC 212 – Data Structures.  Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start.
CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation.
Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?
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’
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
CS 367 Introduction to Data Structures Lecture 5.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
List Interface and Linked List Mrs. Furman March 25, 2010.
Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Ch7. List and Iterator ADTs
Sequences and Iterators
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
" A list is only as strong as its weakest link. " - Donald Knuth
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Presentation transcript:

Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse.  1 st two weeks, race is aimless mess, never to end  Missing their families, kids consult a wise man  Upon leaving the wise man, kids jump on horses  Race at top speed  Race at top speed to the city.  What did the wise man say?

Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse.  1 st two weeks, race is aimless mess, never to end  Missing their families, kids consult a wise man  Upon leaving the wise man, kids jump on horses  Race at top speed  Race at top speed to the city.  What did the wise man say? ride the other's horse!  He told them to ride the other's horse!

CSC 212 – Data Structures

 All elements available when stored in a List  Without removing a thing can access all elements  Add element anywhere in the List  For existing location, set changes element in-place  List s implement Iterable interface  iterator() returns Iterator over elements  Instance of 2 nd class returned by this method  Iterable makes it easy to access elements  Use for-each to handle any Iterable instance List ADT

 But List s still differ in how they are organized  To use ArrayList need absolute index (an int )  Positions (references) used by NodeList  Ideally have ADT with both sets of methods  Could use both absolute & relative ordering  Conversion methods enable jumping back-and-forth Identical Access w/ Iterable?

 But List s still differ in how they are organized  To use ArrayList need absolute index (an int )  Positions (references) used by NodeList  Ideally have ADT with both sets of methods Identical Access w/ Iterable?

 But List s still differ in how they are organized  To use ArrayList need absolute index (an int )  Positions (references) used by NodeList  Ideally have ADT with both sets of methods Identical Access w/ Iterable?

 But List s still differ in how they are organized  To use ArrayList need absolute index (an int )  Positions (references) used by NodeList  Ideally have ADT with both sets of methods Identical Access w/ Iterable?

Sequence ADT  Combines D EQUE, I NDEX L IST, & P OSITION L IST  Includes all methods defined by these interfaces  Adds 2 methods to convert between systems  Get Position at index using atIndex(i)  indexOf(pos) returns index of a Position

Sequence ADT  Combines D EQUE, I NDEX L IST, & P OSITION L IST  Includes all methods defined by these interfaces  Adds 2 methods to convert between systems  Get Position at index using atIndex(i)  indexOf(pos) returns index of a Position

Sequence ADT  Combines D EQUE, I NDEX L IST, & P OSITION L IST  Includes all methods defined by these interfaces  Adds 2 methods to convert between systems  Get Position at index using atIndex(i)  indexOf(pos) returns index of a Position

Sequence ADT  Combines D EQUE, I NDEX L IST, & P OSITION L IST  Includes all methods defined by these interfaces  Adds 2 methods to convert between systems  Get Position at index using atIndex(i)  indexOf(pos) returns index of a Position

interface Sequence extends IndexList, PositionList, Deque { public Position atIndex(int r) throws BoundaryViolationException; public int indexOf(Position p) throws InvalidPositionException; }  As ADT, must be implementation independent  Specify Position okay, but not it's implementation array linked list  S EQUENCE class free to use array or linked list S EQUENCE Interface

 Actually used by all of Java’s List classes  Combines everything so far into one simple idea  Can serve as basis of S TACK, Q UEUE, or D EQUE  Small, simple databases built from Sequence s  Basic building block for many other structures  Many use S EQUENCE somewhere in implementation  Need to know how Position & index works  Use big-Oh costs to select which implementation Why Use a Sequence?

 Actually used by all of Java’s List classes  Combines everything so far into one simple idea  Can serve as basis of S TACK, Q UEUE, or D EQUE  Small, simple databases built from Sequence s  Basic building block for many other structures  Many use S EQUENCE somewhere in implementation  Need to know how Position & index works  Use big-Oh costs select which implementation  Use big-Oh costs to select which implementation Why Use a Sequence?

 Easier & faster if nodes doubly-linked  Position -based methods come from N ODE L IST  To find indices, need to traverse Node s  Could use Node s via getNext() & getPrev()  next() & prev() if want Position traversal Linked list-based Sequence Node s List

Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

 Suppose call was: atIndex(3) i  3 trav Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

 Suppose call was: atIndex(3) i  3 trav Implementing atIndex(i) Node s List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

Node s  Suppose call was: atIndex(3) i  3 2 trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

Node s  Suppose call was: atIndex(3) i  trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

Node s  Suppose call was: atIndex(3) i  trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

Position  Suppose call was: atIndex(3) i  trav Implementing atIndex(i) List Algorithm atIndex(i) if (i < 0) OR (i ≥ size()) then throw BoundaryViolationException fi

Node s  Suppose call was: atIndex(3) i  trav Implementing atIndex(i) List Algorithm atIndex(i) if (i 0) do trav  next(trav) i-- endwhile return trav

Array-based Sequence  Based on IndexList  Use and resize array  Easy to implement  Store elements in array S 0123 N-1 List

Array-based Sequence S 0123 N-1 List

Array-based Sequence  Array of Position s  Can be used in methods  Position contains:  Element  Index in array  No next or prev links  Instead use the index  Update index when Position shifted 0123 S 0123 N-1 List Position

Array-based Sequence Algorithm next(pos) if (pos == S[size()-1]) then throw BoundaryViolation… else idx  pos.getIndex() return S[idx+1] fi end 0123 S 0123 N-1 List Position

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 012 S 0123 N-1 List Position

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 013 S 0123 N-1 List Position

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 023 S 0123 N-1 List Position

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ 123 S 0123 N-1 List Position 0

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ for i  size() downto 1 do S[i]  S[i-1] S[i].setIndex(i) endfor S[0]  new Node(0, e) size++ end 123 S 0123 N-1 List Position 0

Array-based Sequence Algorithm addFirst(e) /* Resize array if needed */ for i  size() downto 1 do S[i]  S[i-1] S[i].setIndex(i) endfor S[0]  new Node(0, e) size++ end 123 S 0123 N-1 List Position 0

Your Turn  Get into your groups and complete activity

For Next Lecture