A journey into data structures
A Linked List is a data structure we create contains a list of nodes that are linked with code we write to link them
node: A node is an object that holds at most 2 pieces of information.
Vocabulary
//What does a node look like
//A node that does NOT link to another node
{
data: "Turkey Sandwich",
next: null
}
//A node that DOES link to another node
{
data: "Turkey Sandwich",
next: {
data: "Meatballs",
next: null
} // this is really a reference to an object in memory
}
In code a single node can "look" 2 ways:
In memory an un-linked list looks like this:
They are simply JUST objects in memory
Note: There is no such thing as an un-linked list
In memory a linked list looks like this:
One object holds a reference to another object
Or: One node holds a pointer to the next node in order
We use linked lists in situations where we don't know the exact size but anticipate that the list could potentially grow to large sizes
We still need to preserve order AND anticipate that order will change over time
Arrays and Objects take their own physical "chunk" of memory and grow that space as they get larger
Linked Lists are just small simple objects in memory that can be stored anywhere and simply "point" to other objects in other places/addresses in memory
Changing the structure of a large linked list is much faster than changing the structure of a large array
Array or Nested Object:
myArray.push(1);
Linked List:
myLinkedList.add(obj);
Adding something to a Linked List is very fast since we just create an object and point to it.
Finding something in a Linked List can be slow since we always have to search from the beginning.
Finding something in a Single Linked List can only search from the beginning of the list.
A Linked List is a rather simple implementation
It is generally comprised of:
In a Single Linked List, each node knows about the next node but not the previous
Double Linked List - same as a Single Linked List with the exception that each node also points to the previous node as well as the next node
Circular Linked List - same as a Double Linked List with the exception that there is no concept of a head or tail. All nodes point to the next/previous in a circular fashion. No true start to the list.