array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are...
Transcript of array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are...
![Page 1: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/1.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
List abstract data type andan “array list” implementation
Comp Sci 1575 Data Structures
![Page 2: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/2.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Data Structures...
![Page 3: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/3.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Reminder
• Welcome to the first day of Data Structures.
• Now is when it will start to be very helpful to have readthe book chapter. The slides, book, and code will paralleleach other.
![Page 4: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/4.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 5: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/5.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 6: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/6.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Interfaces
• List
• Unsorted set (USet)
• Sorted set (SSet)
• Priority queue /
• Graph
![Page 7: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/7.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
List implementations
![Page 8: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/8.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Unsorted set implementations
![Page 9: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/9.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Sorted set implementations
![Page 10: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/10.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Priority queue implementations
![Page 11: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/11.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Graph implementations
• Adjacency matrix
• Adjacency list (array and linked)
• Incidence list
![Page 12: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/12.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Big picture on ADTs
![Page 13: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/13.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Chapter dependencies in ODS
![Page 14: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/14.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 15: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/15.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
List ADT: Why do we care?
• A list is the most universal user interface for computers:e.g., Bash’s $ls, your inbox, your google results, your filebrowser, etc.
• MANY programs use lists in at least some part of theirback-end
• What is the common-language definition of a list?
• What is our formal definition of a list ADT?
![Page 16: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/16.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 17: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/17.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Definitions: List ADT
• A list is a finite, ordered sequence of data items.
• List elements have a position or order.
• Ordered is not the same as sorted, which relates order tovalue; lists can be sorted or unsorted, but are still ordered
• Notation: < a0, a1, . . . , an−1 >
• Length/Size of the list is n − 1
• Position of element ai in the list is i
• First element of the list is a0, the head
• Last element is an−1, the tail
• ai follows (or succeeds) ai−1 where (i < n)
• ai−1 precedes ai where (i > 0).
• Each list item has a data type
• Operations??
![Page 18: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/18.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 19: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/19.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Which list ADT operations do we want?
• Should be able to be ordered, and have a position
• Should grow and shrink as we add/insert or remove items
• Should be able to insert and remove elements fromanywhere in the list.
• Should be able to gain access to any element’s value,either to read it or to change it.
• Should be able to create and clear (or reinitialize) lists.
• Convenient to access next or previous element from“current” one.
• Should be able to locate and/or read items by value orposition
• Can have many more arbitrary actions if desired
Check out the pure virtual abstract class, list.h
![Page 20: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/20.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 21: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/21.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 22: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/22.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
How can we accomplish operation implementation?
What about normal arrays? Aren’t they already complete lists?
i n t x [ 4 ] = {1 , 5 , 8 , 2} ;x [ 0 ] = 3 ;x [ 1 ] = 3 ;x [ 2 ] = 3 ;x [ 3 ] = 3 ;
• Ordered?
• What about search, insert, remove, grow, shrink, next,previous, length, print whole list, append, pop?
To increase our degree of modularity in operation betweenfunctions, which construct is very helpful to keep track of tocomplete many of the above tasks?
![Page 23: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/23.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 24: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/24.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Current position construct is helpful to implement
• Most operations can be made to act relative to a currentposition or a specified position (which could be set tocurrent to complete a task)
• The in-class implementation will support a currentposition, illustrated here using notation | ,e.g.,< 20, 23, |12, 15 >
• For example, listObject.insert(x) could produce< 20, 23, x , |12, 15 >
• This is not true for std:: implementations we’ll cover
![Page 25: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/25.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 26: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/26.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Iterate through a list
One very common task is to iterate through a list:
f o r ( L . mvToStart ( ) ; L . c u r r P o s ( ) < L . l e n g t h ( ) ; L . n e x t ( ) ){
e l e m v a l u e = L . g e t V a l u e ( ) ;doSomething ( e l e m v a l u e ) ;
}
Note the use of member functions to move through the lits andcheck termintation; note that this function does not use privatemembers.
![Page 27: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/27.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
List find function
// r e t u r n True i f k i s i n l i s t L , f a l s e o t h e rw i s e
bool f i n d ( L i s t <i n t> &L , i n t K){
i n t i t ;f o r ( L . mvToStart ( ) ; L . c u r r P o s ( ) < L . l e n g t h ( ) ; L . n e x t ( ) ){
i t = L . g e t V a l u e ( ) ;i f (K == i t )
re tu rn t rue ;}re tu rn f a l s e ; // K not found
}
What would a better find function return?Can we provide more information?
![Page 28: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/28.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Array list insert
• Requires moving elements after insert toward the tail
• How many steps does this take for a list of 4?How about 5? 6? 7?
![Page 29: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/29.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
Outline
1 IntroductionAbstract data types: interfaces
ListUnsorted setSorted setPriority queueGraphs
2 Lists are fundamentalDefinitionsList operations
3 List implementation demandsArrays as lists?Current positionExample tasks
IterateSearchInsert
4 An actual implementation
![Page 30: array list implementation · List Unsorted set Sorted set Priority queue Graphs Lists are fundamental De nitions List operations List implemen-tation demands Arrays as lists? Current](https://reader030.fdocuments.net/reader030/viewer/2022041001/5ea117017521645d6972bbde/html5/thumbnails/30.jpg)
Introduction
Abstract data types:interfaces
List
Unsorted set
Sorted set
Priority queue
Graphs
Lists arefundamental
Definitions
List operations
List implemen-tationdemands
Arrays as lists?
Current position
Example tasks
Iterate
Search
Insert
An actual im-plementation
“Array list” implementation
List based on arrays:
• Check out abstract class file, list.h
• Go over array list class file, list A.h
• Check out find and print in ListTest.h
• Which is all called in main AList easy .cpp