Before starting Hack Reactor, the concept of a data structure was pretty foreign to me. While I understood the usefulness of having different types of data structures to store and represent information, I had no idea what they looked like, how to implement them, or how to traverse them at all.
I'd like to talk about a couple of simpler data structures to help ease others into them.
First things first: Data structures exist to store information and grant access to it. Not all data structures are created equally and they come with strengths, weaknesses, and varying levels of complexity and usefulness.
They're also not as intimidating as they sound. Let's look into a few:
We interact with queues in everyday life. Think of the line at the grocery store or that thirty-minute wait time to speak to a live person over the phone. A queue handles data in two different ways:
- It accepts data by adding it to the end of the line. This is where the queue always begins.
- It removes data by accessing the front of the line.
That's it! Just like the line you go through at your local coffee shop.
Queues are best represented as an array:
var queue = [1, 2, 3, 4, 5]
When we add something to the queue, we put it at the back of the line:
queue.push(6) => [1, 2, 3, 4, 5, 6]
When we want to remove data from the queue, we return the first item:
queue.shift() => returns 1
Stacks are just queues with one distinct difference: Stacks are returned in a last-in, first-out order. Think of your web browser's back button: The last page you visited is the first one you see when you press that button, not the first page you browsed to in the morning.
var stack = [1, 2, 3, 4, 5]
To add something:
stack.push(6) => [1, 2, 3, 4, 5, 6]
stack.pop() => returns 6
And that's it! Stacks and queues, easily explained. Let's look at something a bit more challenging.
A Linked List is a data structure made up of many nodes (each representing one piece of data) that has a couple of important properties:
- A 'Head': The start of the Linked List
- A 'Tail': The end of a Linked List
- A method to add a value to the end of the Linked List
- A method to remove the front of the Linked List
- A method to see if a passed in value belongs in the Linked List.
Nodes are traditionally added to a Linked List the same way as they are added to a Queue. That is, the newest item is placed at the end of the list. Because of this, the newest item in a Linked List is always going to be designated as its Tail (until it gets replaced by another, newer item).
One thing to understand about a Linked List is that the contents of this Linked List are not actually stored into any individual object. Instead, a Linked List exclusively relies on its 'Next' property to formulate a certain path.
That's basically it! You've now received a high level look at two simple data structures followed by one slightly more complicated one.
Next time, we're going to take a look at some more fun data structures including Trees and Hash Tables. Exciting.