How To
Think
Like a
Programmer
Part 2: Data Structures
By @ErikRalston
data
The quantities, characters, or symbols on which operations are performed by a computer, being stored and transmitted in the form of electrical signals and recorded on magnetic, optical, or mechanical recording media.
Types of data
the stack
Fast memory tied to the current instructions
the heap
Long-lived memory tied to the program
Memory Management
How does a program get memory from the computer?
It Asks the Operating System
How does it give it back?
It depends
Manual
Code allocates and deallocates with commands
automatic reference counting
References to objects increment and decrement automatically
Objects with no live references clean themselves up
Automatic garbage collection
The program periodically scan for orphaned data
Releasing it automatically using a "Garbage Collector"
typing
Static
Buckets can only hold one type of thing
dynamic
Buckets can hold anything
Mutable vs immutable
Mutable = Can Change
Immutable = Cannot Change
Example
The String type is immutable in some languages
All operations returns a new copy of the string
variable
A bucket for holding a single value
Whole Number (Integer, Int)
Decimal Number (Float, Double)
Character
Boolean (Bool, True/False)
shelves?
Arrays
A contiguous collection of the same kind of variable
Allocated and deallocated all at once
Each item addressable by number starting at zero
struct
A contiguous collection of different types of variables
Allocated and deallocated all at once
Each item addressable based on some representation
Not all languages implement structs
multi-dimensional arrays
An Array of Arrays
AKA Jagged Arrays
stack
A resizable array
Push items in, Pop items out
"Last In, First Out"
Pros: Resizable and random access
Cons: Cannot randomly remove
linked list
Distinct items that point to the following item in the list
Pros: Easy memory management for insert or remove
Cons: No random access
AKA Queues
hands-on
Be a Linked List
Set
A collection of unique items
Trying to add a repeat item does nothing
Enables set operations like "Union", "Intersection"
Pros: Preserves Uniqueness
Cons: Special Purpose, Usually Indeterminate Order
dictionary
A collection that maps a Key to a Value (Key-Value Pair)
Items are added with key and value
Items are read out by key
AKA Associative Array, Map, HashMap
Pros: Fast Access, Easily Resizable
Cons: Big Memory Footprint, Indeterminate Order
tree
Items linked into "Parent-Child" relationships
A "Root" node connecting down to "Leaf" nodes
Pros: Enables relative position to be very meaningful
Fast insertion, deletion, and search
Cons: Special Purpose
tables
A collection of rows, each conforming to a set of columns
"Clustered Index" ordering and allocation of the data
"Unclustered Index" tree or hash pointing into the data
relational database
A collection of tables
Related by Constraints
Hands-On
Building a Video Store Database
Next time on dragonball z...
thinking
with your
brain