Skip to content

Add Linked list workshop to main #1100

@jdwilkin4

Description

@jdwilkin4

We are adding DSA modules to the JS cert.

We are going to repurpose this linked list workshop for the JS cert

https://www.freecodecamp.org/learn/python-v9/workshop-linked-list-class/step-24

Instead of using classes, we are going to use regular functions since the majority of the cert uses that.

Here revised JS code for the workshop

function initList() {
  return {
    head: null,
    length: 0
  };
}


function isEmpty(list) {
  return list.length === 0;
}


function add(list, element) {
  const node = {
    element: element,
    next: null
  };

  if (isEmpty(list)) {
    list.head = node;
  } else {
    let current = list.head;
    while (current.next !== null) {
      current = current.next;
    }
    current.next = node;
  }

  list.length++;
}


function remove(list, element) {
  let previous = null;
  let current = list.head;

  while (current !== null && current.element !== element) {
    previous = current;
    current = current.next;
  }

  if (current === null) return;

  if (previous !== null) {
    previous.next = current.next;
  } else {
    list.head = current.next;
  }

  list.length--;
}

const myList = initList();

console.log(isEmpty(myList)); // true

add(myList, 1);
add(myList, 2);

console.log(isEmpty(myList)); // false
console.log(myList.length);   // 2

You can look to the Python workshop on ways to break down this project into steps and probably reuse a lot of that approach.

Here is where the new workshop will go in the order

Image

NOTE: this is blocked until the PR to add the DSA modules has been merged into main.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions