mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add queue data structure lab (#66269)
This commit is contained in:
@@ -3328,6 +3328,12 @@
|
||||
"Learn about common data structures and how to work with them in JavaScript."
|
||||
]
|
||||
},
|
||||
"lab-implement-a-queue": {
|
||||
"title": "Implement a Queue",
|
||||
"intro": [
|
||||
"In this lab, you will implement a queue data structure using functions."
|
||||
]
|
||||
},
|
||||
"lecture-introduction-to-common-searching-and-sorting-algorithms": {
|
||||
"title": "Introduction to Common Searching and Sorting Algorithms",
|
||||
"intro": [
|
||||
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
---
|
||||
id: 69aaafeaa4037197c75587cd
|
||||
title: Implement a Queue
|
||||
challengeType: 26
|
||||
dashedName: implement-a-queue
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you will implement a queue data structure using functions. A queue is a First-In-First-Out (FIFO) data structure where elements are added to the back of the queue and removed from the front.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should have a `enqueue` function that adds an element to the back of the queue.
|
||||
1. You should have a `dequeue` function that removes and returns the front element of the queue.
|
||||
1. You should have a `front` function that returns the front element of the queue without removing it.
|
||||
1. You should have a `size` function that returns the number of elements in the queue.
|
||||
1. You should have an `isEmpty` function that returns `true` if the queue is empty and `false` otherwise.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have an `enqueue` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(enqueue);
|
||||
```
|
||||
|
||||
`enqueue` function should add an element to the back of the queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'first');
|
||||
enqueue(queue, 'second');
|
||||
assert.strictEqual(front(queue), 'first');
|
||||
assert.strictEqual(size(queue), 2);
|
||||
```
|
||||
|
||||
You should have a `dequeue` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(dequeue);
|
||||
```
|
||||
|
||||
`dequeue` function should remove and return the front element of the queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'first');
|
||||
enqueue(queue, 'second');
|
||||
enqueue(queue, 'third');
|
||||
assert.strictEqual(dequeue(queue), 'first');
|
||||
assert.strictEqual(dequeue(queue), 'second');
|
||||
assert.strictEqual(size(queue), 1);
|
||||
```
|
||||
|
||||
`dequeue` should return `undefined` when called on an empty queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
assert.isUndefined(dequeue(queue));
|
||||
```
|
||||
|
||||
`enqueue` should continue to add elements to the back of the queue after dequeues.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'first');
|
||||
enqueue(queue, 'second');
|
||||
assert.strictEqual(dequeue(queue), 'first');
|
||||
enqueue(queue, 'third');
|
||||
assert.strictEqual(front(queue), 'second');
|
||||
assert.strictEqual(dequeue(queue), 'second');
|
||||
assert.strictEqual(dequeue(queue), 'third');
|
||||
```
|
||||
|
||||
Different queue instances should not share state.
|
||||
|
||||
```js
|
||||
const queueOne = initQueue();
|
||||
const queueTwo = initQueue();
|
||||
enqueue(queueOne, 'first');
|
||||
enqueue(queueTwo, 'second');
|
||||
assert.strictEqual(front(queueOne), 'first');
|
||||
assert.strictEqual(front(queueTwo), 'second');
|
||||
assert.strictEqual(size(queueOne), 1);
|
||||
assert.strictEqual(size(queueTwo), 1);
|
||||
```
|
||||
|
||||
You should have a `front` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(front);
|
||||
```
|
||||
|
||||
`front` function should return the front element of the queue without removing it.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'first');
|
||||
enqueue(queue, 'second');
|
||||
assert.strictEqual(front(queue), 'first');
|
||||
assert.strictEqual(front(queue), 'first');
|
||||
assert.strictEqual(size(queue), 2);
|
||||
```
|
||||
|
||||
`front` should return `undefined` when called on an empty queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
assert.isUndefined(front(queue));
|
||||
```
|
||||
|
||||
You should have a `size` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(size);
|
||||
```
|
||||
|
||||
`size` function should return the correct number of elements in the queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
assert.strictEqual(size(queue), 0);
|
||||
enqueue(queue, 'first');
|
||||
assert.strictEqual(size(queue), 1);
|
||||
enqueue(queue, 'second');
|
||||
enqueue(queue, 'third');
|
||||
assert.strictEqual(size(queue), 3);
|
||||
dequeue(queue);
|
||||
assert.strictEqual(size(queue), 2);
|
||||
```
|
||||
|
||||
You should have an `isEmpty` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(isEmpty);
|
||||
```
|
||||
|
||||
`isEmpty` function should return `true` for an empty queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
assert.isTrue(isEmpty(queue));
|
||||
```
|
||||
|
||||
`isEmpty` function should return `false` for a non-empty queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'element');
|
||||
assert.isFalse(isEmpty(queue));
|
||||
```
|
||||
|
||||
`isEmpty` function should return `true` after all elements are removed from the queue.
|
||||
|
||||
```js
|
||||
const queue = initQueue();
|
||||
enqueue(queue, 'first');
|
||||
enqueue(queue, 'second');
|
||||
dequeue(queue);
|
||||
dequeue(queue);
|
||||
assert.isTrue(isEmpty(queue));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function initQueue() {
|
||||
return {
|
||||
collection: []
|
||||
};
|
||||
}
|
||||
|
||||
function print(queue) {
|
||||
console.log(queue.collection);
|
||||
}
|
||||
|
||||
function enqueue(queue, element) {
|
||||
|
||||
}
|
||||
|
||||
function dequeue(queue) {
|
||||
|
||||
}
|
||||
|
||||
function front(queue) {
|
||||
|
||||
}
|
||||
|
||||
function size(queue) {
|
||||
|
||||
}
|
||||
|
||||
function isEmpty(queue) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function initQueue() {
|
||||
return {
|
||||
collection: []
|
||||
};
|
||||
}
|
||||
|
||||
function print(queue) {
|
||||
console.log(queue.collection);
|
||||
}
|
||||
|
||||
function enqueue(queue, element) {
|
||||
queue.collection.push(element);
|
||||
}
|
||||
|
||||
function dequeue(queue) {
|
||||
return queue.collection.shift();
|
||||
}
|
||||
|
||||
function front(queue) {
|
||||
return queue.collection[0];
|
||||
}
|
||||
|
||||
function size(queue) {
|
||||
return queue.collection.length;
|
||||
}
|
||||
|
||||
function isEmpty(queue) {
|
||||
return queue.collection.length === 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Implement a Queue",
|
||||
"isUpcomingChange": true,
|
||||
"dashedName": "lab-implement-a-queue",
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "link",
|
||||
"blockLabel": "lab",
|
||||
"usesMultifileEditor": true,
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "69aaafeaa4037197c75587cd",
|
||||
"title": "Implement a Queue"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -300,6 +300,7 @@
|
||||
"comingSoon": true,
|
||||
"blocks": [
|
||||
"lecture-working-with-common-data-structures-js",
|
||||
"lab-implement-a-queue",
|
||||
"quiz-data-structures-js"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user