‘Bout Dem Data Structurez

Part Uno

One of the best parts of not having to implement your own programming language is letting all the genius who created the language implement the difficult computer science concepts. One of the most primary building blocks of any programming language is data structures (we’ll define them here as any type of object that holds data values).

These concepts are also super helpful to understand any coding algorithm based technical problem that you might run into — I’m talking about those ridiculously hard tech puzzles like on Codewars, Leetcode, Hackerrank, Codefights, etc.

One of the best parts of Hack Reactor vs. other bootcamps is that make you dive through some computer science fundamentals. But yeah… I struggled through a lot of these concepts initially. Man — so much appreciation for basic Javascript objects like arrays now…

Stacks

Think about stacks in real life. You stack money, dishes, or laundry one by one. Therefore, if you have a pile of dirty dishes, you can only get to the bottom one by going top to bottom. This concept is LIFO (last in first out, different from financial accounting principles).

We ended up re-implementing an entire stack from scratch that was eerily similar to some parts of Javascript array object (like pop and push functionality), without indices of course. Here’s some of the code in pseudoclassical form (we went this route for the rest of our data structure objects):

var Stack = function() {
this._storage = {};
this._size = 0;
};
Stack.prototype.push = function(value) {
this._storage[this._size] = value;
this._size++;
};
Stack.prototype.pop = function() {
this._size && this._size--;
var result = this._storage[this._size];
delete this._storage[this._size];
return result;
};
Stack.prototype.size = function() {
return this._size;
};

Queues

People have heard of queues before because they’re just a synonym to understand what a line is. We queue up for a million things ie. the DMV, going to the bathroom. The easiest way to understand is this is if a stack is vertical, then a queue is horizontal. Therefore, it is a FIFO system (first in first out) aka early bird gets the worm. You get to the DMV first, you get served first. You can enqueue by adding to the end of a line and then dequeue by removing from the back. The hardest part of this object was remembering that you’re constrained by the start and end of the queue.

var Queue = function() {
this._storage = {};
this._start = 0;
this._end = 0;
};
Queue.prototype.enqueue = function(value) {
this._storage[this._end++] = value;
};
Queue.prototype.dequeue = function() {
var result = this._storage[this._start];
delete this._storage[this._start];
this.size() && this._start++;
return result;
};
Queue.prototype.size = function() {
return this._end - this._start;
};

I kept it easy peasy in this one. In my next blog post, I’ll cover some of the way we reimplemented some more advanced data structures like Linked Lists, Binary Search Trees, and Graphs. Let me just say — thank God I’m not writing my own functional programming language from scratch.

Can’t get enough? Some super helpful resources: