Build a Queue data structure in TypeScript

Paul McBride
InstructorPaul McBride
Share this video with your friends

Social Share Links

Send Tweet
Published 5 months ago
Updated a month ago

A queue is FIFO (First In First Out) list. When you remove elements from the queue, oldest item is removed first. In this lesson we'll cover how to implement a queue using TypeScript. Queue based questions are common in technical interviews, so it useful to be familiar with how they work.

[00:00] We'll get started by creating a class named Queue. The Queue will hold a collection of data. Let's create a property Tasks to store them. Here, we initialize Tasks to an empty array of type Any. The first method we're going to define is In [00:23] Queue. In Queue takes one argument, Task of type Any. Then using Arrays' built in push method, we push that task to our tasks array. I've written some tests for this so let's jump over and see how we did. In our test, we create a new queue, we enqueue 2 items to it, then we [00:43] check that the internal state of the queue is what we expect it to be. Our tests are running on watch mode, and as you can see, that first test is passing. The next method we'll define is dequeue. A queue is a first in, first out data structure. [01:03] What 'dqueue' does is takes the oldest element in our array, removes it from the array, then returns it to us. Let's hop back over to the tests and make sure it works. We start by creating a new queue, then enqueuing 2 elements to it. Next, we call dequeue and check we get the results we expect. Here, we're also checking the [01:23] internal state of the queue to ensure we've removed the element we expect. You can see here that the test is passing. Next up, let's define the 'peak' method. Peak is pretty similar to dequeue. It returns the oldest element in our queue, but it doesn't remove [01:43] that element from the queue. We'll head back over to the tests and make sure it works. Just like before, we create a new queue and add 2 elements to it. Then we call 'peak' and check if we get the results we expect. Finally, we check the internal state of the queue and ensure we haven't removed any elements from it. And as you can see, that test is passing as [02:03] well. Next up, let's define the size method. Size is pretty straightforward. We just return the number of tasks in our queue. Let's hop back over to the tests and make sure it works. Here, we're setting up the queue, and then we're checking to see that the size is what we [02:23] expect it to be. And as you can see, that test is passing as well. Let's create the last method isEmpty. This one's pretty straightforward too. We just call this dot size and then we return true if it is 0. Once [02:42] again, we'll hop back over to the tests. We start here by creating a queue and checking that it's empty. We then enqueue an element, and we check that it's no longer empty. And as you can see, that test is passing.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today