mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add stack data structure lab (#66251)
Co-authored-by: Ilenia M <nethleen@gmail.com> Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com>
This commit is contained in:
@@ -5606,6 +5606,12 @@
|
||||
"In this lab you will implement common linked list operations such as insertion, deletion, and traversal."
|
||||
]
|
||||
},
|
||||
"lab-implement-a-stack": {
|
||||
"title": "Implement a Stack",
|
||||
"intro": [
|
||||
"In this lab, you will implement a stack data structure using functions."
|
||||
]
|
||||
},
|
||||
"lab-implement-a-queue": {
|
||||
"title": "Implement a Queue",
|
||||
"intro": [
|
||||
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
---
|
||||
id: 69a92f0b9cc04bb0d5327bb5
|
||||
title: Implement a Stack
|
||||
challengeType: 26
|
||||
dashedName: implement-a-stack
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you will implement a stack data structure using functions. A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the top.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should have a `push` function that adds an element to the top of the stack.
|
||||
1. You should have a `pop` function that removes and returns the top element of the stack, or `undefined` if there isn't one.
|
||||
1. You should have a `peek` function that returns the top element of the stack without removing it, or `undefined` if there isn't one.
|
||||
1. You should have a `isEmpty` function that returns `true` if the stack contains no elements, and `false` otherwise.
|
||||
1. You should have a `clear` function that removes all elements from the stack.
|
||||
|
||||
**Note:** Most tests depend on the `push` function. Implement `push` first, as tests for `pop`, `peek`, `isEmpty`, and `clear` require adding elements to the stack.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `push` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(push);
|
||||
```
|
||||
|
||||
`push` function should add an element to the top of the stack.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 'first');
|
||||
push(stack, 'second');
|
||||
assert.deepEqual(stack.collection, ['first', 'second']);
|
||||
```
|
||||
|
||||
You should have a `pop` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(pop);
|
||||
```
|
||||
|
||||
`pop` function should remove and return the top element of the stack.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 'first');
|
||||
push(stack, 'second');
|
||||
push(stack, 'third');
|
||||
assert.strictEqual(pop(stack), 'third');
|
||||
assert.strictEqual(pop(stack), 'second');
|
||||
```
|
||||
|
||||
`pop` function should return `undefined` if the stack is empty.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
assert.isUndefined(pop(stack));
|
||||
```
|
||||
|
||||
`pop` function should return falsy values correctly.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, '');
|
||||
assert.strictEqual(pop(stack), '');
|
||||
assert.lengthOf(stack.collection, 0);
|
||||
```
|
||||
|
||||
You should have a `peek` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(peek);
|
||||
```
|
||||
|
||||
`peek` function should return the top element of the stack without removing it.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 'first');
|
||||
push(stack, 'second');
|
||||
assert.strictEqual(peek(stack), 'second');
|
||||
assert.strictEqual(peek(stack), 'second');
|
||||
```
|
||||
|
||||
`peek` function should return `undefined` if the stack is empty.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
assert.isUndefined(peek(stack));
|
||||
```
|
||||
|
||||
`peek` function should return falsy values correctly.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 0);
|
||||
assert.strictEqual(peek(stack), 0);
|
||||
```
|
||||
|
||||
You should have an `isEmpty` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(isEmpty);
|
||||
```
|
||||
|
||||
`isEmpty` function should return `true` for an empty stack.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
assert.isTrue(isEmpty(stack));
|
||||
```
|
||||
|
||||
`isEmpty` function should return `false` for a non-empty stack.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 'element');
|
||||
assert.isFalse(isEmpty(stack));
|
||||
```
|
||||
|
||||
`isEmpty` function should return `false` when the top element is falsy.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, false);
|
||||
assert.isFalse(isEmpty(stack));
|
||||
```
|
||||
|
||||
You should have a `clear` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(clear);
|
||||
```
|
||||
|
||||
`clear` function should remove all elements from the stack.
|
||||
|
||||
```js
|
||||
const stack = initStack();
|
||||
push(stack, 'first');
|
||||
push(stack, 'second');
|
||||
push(stack, 'third');
|
||||
clear(stack);
|
||||
assert.lengthOf(stack.collection, 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function initStack() {
|
||||
return {
|
||||
collection: []
|
||||
};
|
||||
}
|
||||
|
||||
function print(stack) {
|
||||
console.log(stack.collection);
|
||||
}
|
||||
|
||||
function push(stack, element) {
|
||||
stack.collection.push(element);
|
||||
}
|
||||
|
||||
function pop(stack) {
|
||||
return stack.collection.pop();
|
||||
}
|
||||
|
||||
function peek(stack) {
|
||||
return stack.collection[stack.collection.length - 1];
|
||||
}
|
||||
|
||||
function isEmpty(stack) {
|
||||
return stack.collection.length === 0;
|
||||
}
|
||||
|
||||
function clear(stack) {
|
||||
stack.collection = [];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"isUpcomingChange": true,
|
||||
"dashedName": "lab-implement-a-stack",
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "link",
|
||||
"blockLabel": "lab",
|
||||
"usesMultifileEditor": true,
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "69a92f0b9cc04bb0d5327bb5",
|
||||
"title": "Implement a Stack"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -304,6 +304,7 @@
|
||||
"blocks": [
|
||||
"lecture-working-with-common-data-structures-js",
|
||||
"lab-linked-list-operations",
|
||||
"lab-implement-a-stack",
|
||||
"lab-implement-a-queue",
|
||||
"review-data-structures-js",
|
||||
"quiz-data-structures-js"
|
||||
|
||||
Reference in New Issue
Block a user