CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked 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.
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)
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
CSC 212 – Data Structures Lecture 22: PositionList.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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 
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
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.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Lists. The Position Abstract Data Type A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes.
Stacks, Queues, and Deques
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
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.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
CSC 212 – Data Structures Lecture 37: Course Review.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
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.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
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 212 Trees & Recursion. Announcements Midterm grades were generally good  Wide distributions of scores, however  Will hand back at end of class 
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.
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.
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.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
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.
CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
LINKED LISTS.
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,
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
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,
Stacks.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Vectors, Lists and Sequences
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Presentation transcript:

CSC 212 Vectors, Lists, & Sequences

Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted, if necessary Next homework assignment due Thursday Midterm in a little over one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed computer)  Thursday’s lecture includes a review

Rank of Element Describes the position of element in a list  Item at front of list has rank of 0  2 nd item has rank of 1; 3 rd item has rank of 2  Generally, n th item has rank of n – 1

Ranking Details Ranks do not depend on arrays  They specify how to order lists of elements independent of implementation details Arrays can maintain ranks, but so can:  Linked lists  Doubly linked lists  Specially trained monkeys…

Vector ADT Vector is first type which uses ranks Similar to previous ADTs in that it defines  int size()  boolean isEmpty() Differs from stacks, queues, and deques  Can insert and remove any element  Can also replace a specific element

Item Retrieval Any element in a Vector can be retrieved using Object elemAtRank(int r) Note: this is different than using an array Object[] arr; Vector vect;... Object o = arr[4]; // legal o = vect[4]; // No! o = vect.elemAtRank(4) // legal o = arr.elemAtRank(4) // No go!

Insertion into Vectors Insertion can occur anywhere  void insertAtRank(int r, Object e) Inserts object e at rank r Increases rank of objects now after it  What exceptions do we need to include?

Removal from Vectors Can also remove any element  Object removeAtRank(int r) Removes object at rank r Decreases rank of elements after object being removed  Could this throw any exceptions beyond those defined by insertAtRank() ?

New Option Excitement! Vectors can also replace elements Object replaceAtRank(int r, Object e) Throws same exceptions as previous methods Returns element previously at rank r Ranks of remaining elements are unchanged

Vector Implementation What could we use to implement vectors and how?

Array-based Vector Arrays make implementing Vectors simple, except… … inserting an element. What is big-Oh? Vector 012 r 012 r 012 e r

Array-based Vector Removing from a Vector is not easy either… Vector 012 r 012 e r 012 r

Array-based Vector How would you implement size() ? Vector 012 r

Linked Lists Not a Panacea What is complexity of insertAtRank, removeAtRank, and replaceAtRank ?

Vector Growth Using linked lists removes size limit  But we could get unbounded size using arrays Whenever array fills, allocate larger array Object[] temp = new Object[...]; for (int i = 0; i < arr.size; i++) temp[i] = arr[i]; arr = temp;

Growth Strategies Two different strategies for growth:  Grow by constant, c, each time  Double size each time Both have same big-Oh complexity.  What is it?  When does it occur? Instead compare amortized (average) time  Consider inserting n elements

Constant Growth Need to grow k = n/c times  Each time we grow we must copy entire array  Total copies is: 1 + (c+1) + (2c+1) + + ((k-1)*c)+1 + k*c+1 = k*c+2 + k*c k*c+2 = k/2 * ((k*c)+2) = ½ck = O(n 2 )  For each of n inserts, avg complexity is O(n)

Doubling Growth Array grows k = log n times  Each time we grow we must copy entire array  Total copies: k k = 2 k + 2 k = 2 k+1 = 2 * 2 log n = 2 * n  For each insert, average complexity only O(1)

Adapter Pattern Implementation using other class instance  Example: Implementing Stack with Deque

Adapter Pattern public class Stack { protected Deque deque; public Stack() {deque = new Deque();} public int size() { return deque.size(); } public boolean isEmpty() { return deque.isEmpty(); } public void push(Object e) { deque.insertFirst(e); }

Positions Sometimes rankings may not make sense  Elements may not have absolute positions  May not want to pay time overhead Instead use relative rankings  Retrieve elements before or after current one  Add new element before or after current one  Replace/remove current element  Get first/last elements

Position ADT ADT implementing these relative rankings public interface Position { public Object element(); }

List ADT Simplest collection of Position s is List Defines usual int size() & boolean isEmpty() methods

List ADT Also defines accessors: Position first() Position last() Position next(Position p) throws InvalidPositionException, BoundaryViolationException Position prev(Position p) throws InvalidPositionException, BoundaryViolationException

List ADT Defines update methods:  insertFirst, insertLast, insertBefore, insertAfter Takes an object and returns the Position  remove Removes the position passed in as parameter  replace Substitutes object for element currently at position

Implementation of List How could we implement List & Position?

Sequences Sequences combine List and Vector ADTs Also enables conversion between the two  atRank – returns the position object for a given rank  rankOf – given a position, returns its rank public interface Sequence extends List, Vector { public Position atRank(int r) throws public int rankOf(Position p) throws }

Daily Quiz Use adapter pattern to write a Deque implementation  Adapt using an implementation of Vector  A vector class, LLVector, the exceptions LLVector needs, and the Deque & Vector interfaces available on quiz page. Useful for student looking to practice Java skills by compiling/testing solutions