DEV Community

Will Sheppard
Will Sheppard

Posted on

Creating a Linked List in JavaScript using Mockedin-Client Data

Introduction

In this tutorial, we will learn how to create a simple linked list in JavaScript. Specifically, we will populate this linked list with data from a project called "Mockedin-Client." This client-side project simulates key LinkedIn features and is available on GitHub.

If you're looking to hire or connect with me, you can find my LinkedIn profile here and my GitHub profile here.

What is a Linked List?

A linked list is a data structure used for storing a sequence of elements. Each element contains a reference (or link) to the next element in the sequence.

Setting up the Node and LinkedList Classes

First, let's set up our Node and LinkedList classes in JavaScript:

// Node Class: Represents each element in the linked list
class Node {
  constructor(user) {
    this.user = user;  // Store user data
    this.next = null;  // Initialize next to null
  }
}

// LinkedList Class: Manages the linked list
class LinkedList {
  constructor() {
    this.head = null;  // Initialize head to null
  }

  // Method to append a user to the list
  append(user) {
    const newNode = new Node(user);  // Create new node with user data

    if (!this.head) {
      this.head = newNode;  // If list is empty, set head to new node
      return;
    }

    // Traverse to the last node in the list
    let current = this.head;
    while (current.next) {
      current = current.next;
    }

    // Attach new node to the last node's next pointer
    current.next = newNode;
  }
}
Enter fullscreen mode Exit fullscreen mode

Populating the Linked List with Mockedin-Client Data

Assuming you have your data stored in a db.json file, here is a sample:

{
  "linkedinUsers": [
    { "id": 1, "name": "John Doe", "position": "Developer" },
    { "id": 2, "name": "Jane Smith", "position": "Recruiter" },
    { "id": 3, "name": "Emily Johnson", "position": "CEO" },
    { "id": 4, "name": "Mike Brown", "position": "Sales" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

We can populate our linked list with this data as follows:

// Sample data from Mockedin-Client
const db = {
  "linkedinUsers": [
    { "id": 1, "name": "John Doe", "position": "Developer" },
    { "id": 2, "name": "Jane Smith", "position": "Recruiter" },
    { "id": 3, "name": "Emily Johnson", "position": "CEO" },
    { "id": 4, "name": "Mike Brown", "position": "Sales" }
  ]
};

// Create a new linked list instance
const userList = new LinkedList();

// Populate linked list with Mockedin-Client data
db.linkedinUsers.forEach((user) => {
  userList.append(user);
});

// Now, userList is a linked list containing Mockedin-Client users.
Enter fullscreen mode Exit fullscreen mode

Conclusion

And there you have it! We've created a simple linked list in JavaScript and populated it with data from the Mockedin-Client project. This is a basic example, but you can extend it to suit your specific needs.

Feel free to reach out to me on LinkedIn or GitHub if you have any questions or if you're interested in discussing algorithms, coding, or potential opportunities. Happy coding!

Connect with me:

Top comments (0)