Source: What’s a Linked List, Anyway? Part 1
Source: What’s a Linked List, Anyway? Part 2
A sequence of Nodes
that are connected/linked to each other. Each Node
references the next. Linear data structure.
Singly
- Each Node
has one reference. Points to Next
Doubly
- Each Node
has two references. Point to Next
and Previous
Circular
- Tail
reference node points to Head
instead of NullNode
- Items/links in a linked listNext
- Property on a Node
containing reference to next nodeHead
- Reference type of node to the first node in a linked listTail
- Reference type of node to the last node in a circular linked listCurrent
- Reference type of node to the node currently being looked at. Helpful when traversing. Reset to head when done.ALGORTIHM Includes (value)
// INPUT <-- integer value
// OUTPUT <-- boolean
Current <-- Head
WHILE Current is not NULL
IF Current.Value is equal to value
return TRUE
Current <-- Current.Next
return FALSE
Set current to head to start @ beginning
While loop, until null (end)
Check values
Move to next if no match
If we hit end, not in list
ALGORITHM Add(newNode)
// INPUT <-- Node to add
// OUTPUT<-- No output
Current <-- Head
newNode.Next <-- Head
Head <-- newNode
Current <-- Head
ALGORITHM AddBefore(newNode, existingNode)
// INPUT <-- New Node, Existing Node
// OUTPUT <-- No Output
Current <-- Head
while Current.Next is not equal to NULL
if Current.Next.Value is equal to existingNode.Value
newNode.Next <-- existingNode
Current.Next <-- newNode
Current <-- Current.Next
Traverse, but…
ALGORITHM Print()
// INPUT <-- None
// OUTPUT <-- string to console
Current <-- Head
while Current.Next is not equal to NULL
OUTPUT <-- "Current.Value --> "
Current <-- Current.Next
OUTPUT <-- "Current.Value --> NULL"
In a dynamically typed language (JS, Python) we don’t think about memory use because types are abstracted from us. It’s happening, but we don’t see it.
Good
Memory efficient when adding/removingBad
Memory iniefficent when searching