Skip to content

Instantly share code, notes, and snippets.

@ostojics
Forked from bradtraversy/js_linked_list.js
Last active July 9, 2021 19:12
Show Gist options
  • Select an option

  • Save ostojics/ffa5f308606b9d79c1f45a7a6598b2f3 to your computer and use it in GitHub Desktop.

Select an option

Save ostojics/ffa5f308606b9d79c1f45a7a6598b2f3 to your computer and use it in GitHub Desktop.

Revisions

  1. ostojics revised this gist Jul 9, 2021. 4 changed files with 16 additions and 145 deletions.
    1 change: 1 addition & 0 deletions cloudSettings
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {"lastUpload":"2021-07-09T19:12:39.409Z","extensionVersion":"v3.4.3"}
    12 changes: 12 additions & 0 deletions extensions.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    [
    {
    "metadata": {
    "id": "e337c67b-55c2-4fef-8949-eb260e7fb7fd",
    "publisherId": "Shan.code-settings-sync",
    "publisherDisplayName": "Shan"
    },
    "name": "code-settings-sync",
    "publisher": "Shan",
    "version": "3.4.3"
    }
    ]
    145 changes: 0 additions & 145 deletions js_linked_list.js
    Original file line number Diff line number Diff line change
    @@ -1,145 +0,0 @@
    // Construct Single Node
    class Node {
    constructor(data, next = null) {
    this.data = data;
    this.next = next;
    }
    }

    // Create/Get/Remove Nodes From Linked List
    class LinkedList {
    constructor() {
    this.head = null;
    this.size = 0;
    }

    // Insert first node
    insertFirst(data) {
    this.head = new Node(data, this.head);
    this.size++;
    }

    // Insert last node
    insertLast(data) {
    let node = new Node(data);
    let current;

    // If empty, make head
    if (!this.head) {
    this.head = node;
    } else {
    current = this.head;

    while (current.next) {
    current = current.next;
    }

    current.next = node;
    }

    this.size++;
    }

    // Insert at index
    insertAt(data, index) {
    // If index is out of range
    if (index > 0 && index > this.size) {
    return;
    }

    // If first index
    if (index === 0) {
    this.insertFirst(data);
    return;
    }

    const node = new Node(data);
    let current, previous;

    // Set current to first
    current = this.head;
    let count = 0;

    while (count < index) {
    previous = current; // Node before index
    count++;
    current = current.next; // Node after index
    }

    node.next = current;
    previous.next = node;

    this.size++;
    }

    // Get at index
    getAt(index) {
    let current = this.head;
    let count = 0;

    while (current) {
    if (count == index) {
    console.log(current.data);
    }
    count++;
    current = current.next;
    }

    return null;
    }

    // Remove at index
    removeAt(index) {
    if (index > 0 && index > this.size) {
    return;
    }

    let current = this.head;
    let previous;
    let count = 0;

    // Remove first
    if (index === 0) {
    this.head = current.next;
    } else {
    while (count < index) {
    count++;
    previous = current;
    current = current.next;
    }

    previous.next = current.next;
    }

    this.size--;
    }

    // Clear list
    clearList() {
    this.head = null;
    this.size = 0;
    }

    // Print list data
    printListData() {
    let current = this.head;

    while (current) {
    console.log(current.data);
    current = current.next;
    }
    }
    }

    const ll = new LinkedList();

    ll.insertFirst(100);
    ll.insertFirst(200);
    ll.insertFirst(300);
    ll.insertLast(400);
    ll.insertAt(500, 3);

    // ll.clearList();
    // ll.getAt(2);

    ll.printListData();
    3 changes: 3 additions & 0 deletions settings.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    {
    "sync.gist": "ffa5f308606b9d79c1f45a7a6598b2f3"
    }
  2. @bradtraversy bradtraversy revised this gist Jul 3, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion js_linked_list.js
    Original file line number Diff line number Diff line change
    @@ -139,5 +139,7 @@ ll.insertFirst(300);
    ll.insertLast(400);
    ll.insertAt(500, 3);

    // ll.clearList();
    // ll.getAt(2);

    ll.printListData();
    // ll.getAt(10);
  3. @bradtraversy bradtraversy created this gist Jul 3, 2019.
    143 changes: 143 additions & 0 deletions js_linked_list.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    // Construct Single Node
    class Node {
    constructor(data, next = null) {
    this.data = data;
    this.next = next;
    }
    }

    // Create/Get/Remove Nodes From Linked List
    class LinkedList {
    constructor() {
    this.head = null;
    this.size = 0;
    }

    // Insert first node
    insertFirst(data) {
    this.head = new Node(data, this.head);
    this.size++;
    }

    // Insert last node
    insertLast(data) {
    let node = new Node(data);
    let current;

    // If empty, make head
    if (!this.head) {
    this.head = node;
    } else {
    current = this.head;

    while (current.next) {
    current = current.next;
    }

    current.next = node;
    }

    this.size++;
    }

    // Insert at index
    insertAt(data, index) {
    // If index is out of range
    if (index > 0 && index > this.size) {
    return;
    }

    // If first index
    if (index === 0) {
    this.insertFirst(data);
    return;
    }

    const node = new Node(data);
    let current, previous;

    // Set current to first
    current = this.head;
    let count = 0;

    while (count < index) {
    previous = current; // Node before index
    count++;
    current = current.next; // Node after index
    }

    node.next = current;
    previous.next = node;

    this.size++;
    }

    // Get at index
    getAt(index) {
    let current = this.head;
    let count = 0;

    while (current) {
    if (count == index) {
    console.log(current.data);
    }
    count++;
    current = current.next;
    }

    return null;
    }

    // Remove at index
    removeAt(index) {
    if (index > 0 && index > this.size) {
    return;
    }

    let current = this.head;
    let previous;
    let count = 0;

    // Remove first
    if (index === 0) {
    this.head = current.next;
    } else {
    while (count < index) {
    count++;
    previous = current;
    current = current.next;
    }

    previous.next = current.next;
    }

    this.size--;
    }

    // Clear list
    clearList() {
    this.head = null;
    this.size = 0;
    }

    // Print list data
    printListData() {
    let current = this.head;

    while (current) {
    console.log(current.data);
    current = current.next;
    }
    }
    }

    const ll = new LinkedList();

    ll.insertFirst(100);
    ll.insertFirst(200);
    ll.insertFirst(300);
    ll.insertLast(400);
    ll.insertAt(500, 3);

    ll.printListData();
    // ll.getAt(10);