CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.

Slides:



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

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
CSC 212 – Data Structures Lecture 22: PositionList.
Stacks, Queues, and Deques. 2 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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
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.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
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.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
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
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
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.
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  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.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
LinkedList Many slides from Horstmann modified by Dr V.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
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.
© 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 – 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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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 
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Week 4 - Monday CS221.
Marcus Biel, Software Craftsman
" A list is only as strong as its weakest link. " - Donald Knuth
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
CS210- Lecture 6 Jun 13, 2005 Announcements
TCSS 143, Autumn 2004 Lecture Notes
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList

Question of the Day What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side. A A B B

Deque ADT Another type of Collection class Stands for Double Ended QUEue  Combines ideas from Stack and Queue ADT Manipulate elements at front & rear  addFront(), addLast()  removeFront(), removeLast()  getFront(), getLast() Can be implemented with array or linked list  If using linked list, should be doubly-linked list

List ADT Sometime want use of entire Collection  Add new element before existing one  Get the 3 rd element in the Collection  Loop over all elements without removing them  Cannot be done with Stack, Queue & Deque Lists can access all of its elements  But provide different means for this access  Will discuss over next several lectures

IndexList ADT Also called a Vector  Names are used interchangeably First example of a List Extends idea of an array  Stores arbitrary sequence of elements  Elements may appear multiple times  Access elements using integer rank  Like array indexes, does not imply ordering

Ranks ArrayList organizes collection using rank  Item at front of list has rank of 0  2 nd item has rank of 1  3 rd item has rank of 2  n th item has rank of n – 1 Ranks increase sequentially  Merely discusses position of element  Cannot skip over a rank  Cannot repeat a rank

IndexList Interface public interface IndexList extends Collection { public void add(int rank, E e) throws IndexOutOfBoundsException; public E get(int rank) throws IndexOutOfBoundsException; public E remove(int rank) throws IndexOutOfBoundsException; public E set(int rank, E newValue) throws IndexOutOfBoundsException; }

IndexList != array Extends the idea of an array, but…  IndexList does not have constant size  Elements’ ranks may change over time Can implement using:  Array,  Singly-linked list,  Doubly-linked list,  Specially trained monkeys,  College students

Insertion add( r, e ) “shifts” existing elements down to make room for e  For linked-list, insert node at proper location  For array, must shift elements down Can take O(n) time S 012n r S 012n r S 012n e r

Deletion remove( r ) “shifts” remaining elements up to fill hole created by removal  For linked-list, happens automatically  For array, must shift elements up Can also take O(n) time S 012n r S 012n r S 012n r

IndexList’s Operations add, get, & remove similar to past operations  add ≈ addFront, addLast, enqueue, push  get ≈ getFront, getLast, front, top  remove ≈ removeFront,removeLast,dequeue,pop But, set is a brand new operation  Stores the new element at the given rank  Removes (& returns) element already there  Does not change rank of other elements

IndexList’s Exceptions Any of these methods may throw IndexOutOfBoundsException Thrown when rank is illegal for operation add accepts ranks 0 - size()  add(0, e)  e added to front of List  add(size(), e)  e added at end of List get, set, & remove use ranks 0 - size()-1  Ranks start at 0, so no element at size() rank

Implement IndexList with array Can allocating & copy into larger array  Increase array length by constant, c -or-  Double length of the array each time Both approaches have O( n ) complexity  Cost of copying entire array But have different amortized complexities  Consider cost of growth due to n calls to add()

Constant Growth Need to grow k = n / c times  Copy entire array with each growth, so total copies is: 1 + ( c +1) + (2 c +1) + + ((( k -1) *c )+1) + ( k*c +1) = (( k * c ) + 2) + (( k * c ) + 2) + + (( k * c ) + 2) = k / 2 * (( k * c ) + 2) = O( c*k 2 )= O( c * ( n / c ) 2 ) = O( n 2 * 1 / c ) = O( n 2 )  Average cost: O( n 2 ) / n = O( n )

Doubling Growth Array grows k = log n times  Still copy entire array, for this many copies: k k = (2 k - 1) + 2 k = 2 k = (2 * 2 k ) - 1 = (2 * 2 log n ) - 1 = O(2 n - 1) = O( n )  Average cost: O( n ) / n = O(1) But, moving elements in add() still costs O( n )!

Your Turn Get back into groups and do activity

Before Next Lecture… Keep up with your reading! Continue Week #9 Assignment Work on Programming Assignment #2