DSA-Class 04-ADTs_ArrayList
DSA-Class 04-ADTs_ArrayList
ARRAY LIST
Data Structures and Algorithm
2
Outline
• Abstract Data Type (ADT)
• Common ADTs
• Array List
3
Operation1(…)
Operation2(…)
Operation3(…)
…
…
…
5
Why Abstract?
• Specify the operations of the data structure
and leave implementation details to later
• many, many different ADTs
• picking the right one for the job is an important step
in design
• "Get your data structures correct first, and the rest
of the program will write itself."
- Davids S. Johnson
• High level languages often provide built in
ADTs,
• the C++ STL, the Java standard library
6
Implementing ADTs
• When implementing an ADT, the operations
and behaviors are already specified
• Implementer’s first choice is what to use as
the internal storage container for the
concrete data type
• the internal storage container is used to hold the
items in the collection
• often an implementation of an ADT
• initially slim pickings for choice of storage
containers: arrays anyone?
8
Lists
• Items have a position in this Collection
• Random access or not?
• Array Lists
• internal storage container is native array
• Linked Lists
last
first
11
Stacks
• Collection with access only to the last element
inserted
• Last in first out Data4 Top
• insert/push Data3
• remove/pop
Data2
• top
Data1
• make empty
12
Queues
• Collection with access only to the item that
has been present the longest
• Last in last out or first in first out
• enqueue, dequeue, front
• priority queues and dequeues
Front
Back
Trees
• Similar to a linked list:
Root
14
HashTables
• Take a key, apply function
• f(key) = hash value
• store data or object based on hash value
• Sorting O(N), access O(1) if a perfect hash
function and enough memory for table
• how deal with collisions?
16
Other ADTs
• Maps
• a.k.a. Dictionary
• Collection of items with a key and associated values
• similar to hash tables, and hash tables often used to
implement Maps
• Graphs
• Nodes with unlimited connections between other
nodes
• Sparse vectors and sparse matrices
17
List ADT
• What is a list?
• An ordered sequence of elements A1, A2, …, AN
• Elements may be of arbitrary, but the same type (i.e., all
ints, all doubles etc.)
• Common List operations are:
• Insert(ElementType E, Position P)
• Delete(Position P)
• Find(ElementType E)
• IsEmpty() – Returns true if the list is empty
• First() – Returns the first element
• Last() – Returns the last element
• Kth(Position K) – Returns the Kth element
• Length() – Returns the length of the list
18
Lists: Implementation
• Two types of implementation:
• Array-Based - Today
• Linked List – Next Week
public:
ArrayList (); // Constructor
~ArrayList ();// Destructor
Application of Lists
• Polynomial ADT: store and manipulate single
variable polynomials with non-negative exponents
• 10X^3 + 4X^2 + 7 = 10X^3 + 4X^2 + 0X^1 + 7X^0
• Data structure: stores coefficients Ci and exponents i