Follow me on Twitter!

🤪

Quicksand

Huey Lewis and the Queues

 10/05/2019

In an effort to dig deeper into core computer science principles and concepts, I've been reading a ton about Data Structures and examples of their real life applications. This week I have been reading about Queues; ****what they are, how they work, how to implement them and IRL applications in the wild.

What is a queue, you may ask? Yo Wikipedia, give me a beat!

In computer science, a queue is a collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue.

If you were looking for the literal definition, then I guess you can literally go somewhere else and implement your own queue service and make bajillions of doll hairs; but if you're anything like me, I tend to understand things when they are broken down into small, digestible, and sometimes adorable, pieces.

WTQ?


In an effort to better understand queues and their place in the world, I sought out to create my own tiny implementation of one. Granted, this is going to he the most basic Queue complete with the most basic of features. But in my research I have found that a queue must have 4 basic requirements to be considered among one of the greats:

  1. A Queue must be an ordered list of elements of similar data types

  2. Enqueuing - the ability to place an item onto the queue

  3. Dequeuing - the ability to remove an item from the queue

  4. FIFO Structure - (First In First Out) items must be retrieved in the same order they were put onto the queue, much like the same way a line or queue of people at a bank works

With these 4 requirements in mind, I set out to create a queue that would exceed the expectations of every queue that came before it. But, this wouldn't be a good blog post if I did not ask the question, why? What problems do queues solve, and when would I see one in the real world?

I was able to find a few examples of queues in the real world:

  • Serving requests on a single shared resource, like a printer

  • CPU task scheduling

  • Call center phone systems

  • Serving large amounts of files to high volume traffic.

So now that I had most information I need in order to implement my own queue, I have decided to unveil a little friend that I have created just for the occasion:

Meet Huey 🐙


Huey the Queue, Huey for short, is a tiny JavaScript implementation of a queue that meets all of the above requirements. Now there are benefits and drawbacks to the language I've chosen and I believe I can clear up the reasoning behind my choice:

I knew I wanted to create a multitude of simple implementations of common data structures in a short amount of time. Since JavaScript is the most accessible (you have a REPL in your browser) language I could think of to implement most things in the shortest amount of time, I knew it would make for a decent choice in these circumstances:

class HueyTheQueue { constructor() { this.q = [] } enQ(...msgs) { return this.q.push(...msgs) } deQ() { if (this.q.length) { return this.q.shift() } throw Error('Huey has no news for you.') } peek() { return this.q[0] } length() { return this.q.length } }

Huey can handle any basic queue feature, aside from really fancy stuff like visibility timeouts, proper error handling, etc:

const huey = new HueyTheQueue() huey.enQ("Here's a job."); huey.enQ("Here's another one."); huey.deQ() // "Here's a job." huey.length() // 1 huey.peek() // "Here's another one."

Huey will take things that we give him, and hold onto them in the order they were received until we ask for them back, just like a trusty queue should do.

There were obviously advantages as well as disadvantages to choosing this particular language. In retrospect, I could have benefitted from using a more strongly typed language with private variables, otherwise we could technically do this:

const huey = new HueyTheQueue()

// We can easily manipulate the queue, bypassing the intended channels huey.q = [1, 2, 3, 4]

This post is apart of a broader series of learning about data structures. Tune in next time where we'll be building a Stack and a Linked List!