linked list javascript

class LinkedList {   constructor() {    this._head = null;    this._tail = null;    this._length = 0;  }    add(value) {    let node = new Node(value);         //we create our node    if(!this._head && !this._tail){     //If it's the first node      this._head = node;                //1st node is head & tail      this._tail = node;    }else{    this._tail.next = node;             //add node to the back    this._tail = this._tail.next;       //reset tail to last node    }    this._length++;  }    size() {    return this._length;  }}

5
1

                                    const list = {
    head: {
        value: 6
        next: {
            value: 10                                             
            next: {
                value: 12
                next: {
                    value: 3
                    next: null	
                    }
                }
            }
        }
    }
};

5 (1 Votes)
0
0
7
Cristian 80 points

                                    
/**
 * @constructor SLL
 *
 * @param head The head of the list.
 * @param klass The reference to the SLLNode class
 * @param size the size or length of the list.
 * @arguments <klass:SLLNode>
 * @description The SLL Factory
 *
 */
export function SLL(klass = Node) {
    this.head = new Node();
    this.klass = Node;
    this.size = 0;
}

/**
 * @function Add|Method
 *
 * @returns SLLNode
 * @param node The node you will append
 * @arguments <key:string>
 * @description A method that will insert a new node at the beginning of the
 * list.
 */
SLL.prototype.add = function insert(key) {
    const node = new this.klass(key);
    this.head = this.head.add(node);
    this.size++;
    return this;
}

/**
 * @function Find|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to find.
 * @arguments <key:string>
 * @description A method that will retireve a node associated with the
 * corrosponding key.
 */
SLL.prototype.find = function find(key) {
    return this.head && this.head.find(key);
}

/**
 * @function Insert|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to append a new node.
 * @param key1 The key of the node you wish to insert
 * @arguments <key:string>, <key1:string>
 * @description A method that will insert a new node into the middle of the
 * list
 */
SLL.prototype.insert = function insert(key, key1) {
    const node = new this.klass(key1);
    return this.head && this.head.insert(key, node);
}

/**
 * @function Del|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to delete.
 * @arguments <key:string>
 * @description A method that will delete a node associated with the
 * corrosponding key.
 */
SLL.prototype.del = function del(key) {
    return this.head.del(key);
}

/**
 * @returns SLLNode
 * @param key The key of the node you wish to create.
 * @arguments <key:null?string>
 * @description The SLLNode Interface
 *
 * @interface ISLLNode
 */
function ISSLNode(key=null) {
    this.key = key;
    this.next = null;
}

/**
 * @constructor SLLNode
 *
 * @param key The key of the node you wish to create.
 * @arguments <key:null?string>
 * @description The SLLNode Factory
 * 
 * @implements ISLLNode
 */
export function SLLNode(key=null) {
    ISSLNode.call(this, key=null);
}

/**
 * @function Add|Method
 *
 * @returns SLLNode
 * @param node The node you will append
 * @arguments <node:SLLNode>
 * @description A method that will insert a new node at the beginning of the
 * list.
 */
SLLNode.prototype.add = function add(node) {
    if(!this.key && !this.next) {
        this.key = node.key;
        this.next = node.next;
        return node;
    }
    node.next = this;
    return node;
}

/**
 * @function Find|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to find.
 * @arguments <key:string>
 * @description A method that will retireve a node associated with the 
 * corrosponding key.
 */
SLLNode.prototype.find = function find(key) {
    if (this.key === key) {
        this.next = null;
        return this;
    }
    if (this.key !== key) {
        if(!this.next) {
            return 0;
        }
        return this.next.find(key);
    }
}

/**
 * @function Insert|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to append a new node.
 * @param node The node you will append
 * @arguments <key:string>, <node:SLLNode>
 * @description A method that will insert a new node into the middle of the
 * list
 */
SLLNode.prototype.insert = function insert(key, node) {
    if (this.key === key) {
        let tmp = this.next;
        this.next = node;
        node.next = tmp;
        return node;
    }
    if (this.key !== key) {
        if (!this.next) {
            return 0;
        }
        return this.next.insert(key, node);
    }
}

/**
 * @function Del|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to delete.
 * @param pre 
 * @arguments <key:string>, <pre:null?SLLNode>
 * @description A method that will delete a node associated with the
 * corrosponding key.
 */
SLLNode.prototype.del = function del(key, pre=null) {
    if (this.key === key) {
       let tmp = this.next;
       pre.next = tmp;
        return this;
    }
    if (this.key !== key) {
        if (!this.next) {
            return 0;
        }
        return this.next.del(key, this);
    }
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
linkedinlist js code what is linked list in javascript is linked list in javascript linkedlist methods javascript linkedlist methods js javascript linked list method Linked list javasc0 how to do a linked list in js js linked list linked list in javascript es6 when to use linked list in javascript linkedinlist js how to understand linked list in javascript understanding linked list js is there built in linked list javascript are there linked lists in javascript linked list en js linked list example js javascrip linked list singly and doubly linked list in javascript node linkedlist javascript implement linkedlist in js implement linked list in js javascript use linked list js linkedlist library linked listin js linked list example in javascript how to create linked list in javascript HOW TO GET THE lINKED LIST IN JAVASCRIPT linked list operations in javascript js linked list function linked list methods in javascript where we use linked list in javascript where we linked list in javascript is there a built in linked list in javascript linkedlist implementation javascript linked list using js linked list using javascript how to implement linked list in javascript Singly-linked list in javascript what javascript linked list linked list implementation javascript implementing linked lists in javascript linked list code js functional linked lists in javascript linked list method implementation javascript working with linked lists in javascript js in built linked list how make a linked list in js javascript how to implement a singly linked list linked lists explained javascript javascript linkedlists linked list code using js create linked list with javascript how to implement in linkedlist in javascript when was javascript linked list created implement linked list javascript make linked list javascript linked list JS linked list c JS linked list js example javascript build singly linked list singly and doubly linked list using js javascript linked linked list what is a linked list in js link list in javascript linked list implementation js linked list create js Demonstrate singly and doubly linked list in javascript linkedlist and javascript linked list simple program js linked list library javascript how to use linked list in javascript linked list find javascript linked list methods js implement linked list on js javascript implement linked list javascript using linked list how to code linked list in javascript linked list javascript library linked list javascript w3schools linked lis in js linkedlist js create linked list in javascript javascript linked list library javascript linked list w3schools linked list example javascript linked list havascript singly linked list in js making linked list from function javascript javascript linked lists linked list n javascript javascript linked list implementation how to implement a linked list javascript javascript linked list methods linked list with javascript linked list how to create javascript linked list program in javascript linked list javascript linkedi list javascript linked list functions javascript linked list in java implementation in javascript js Linked List detail explanation of linked list in javascript understanding linked lists javascript link list implementation javascript javascript add linked list to existing linked list node js linked list how to build linked list javascript javascript new ListNode() is a javascript array a linked list singly linked list in javascript listnode description in js linked list code in javascript linked list javascriopt js linkedlist last Singly Linked Lists javascript code singly linked list javascript example get indexed of linked list in js js linked list using object linked list in node js print linked list javascript create a linked list in javascript from an array javascript linklist javascript how to create a link list javascript new node linked list linked list traversal in array javascript add a node to a linked list javascript how to implement linked list in js how to create a linked list in javascript implementing linked list in javascript linear sea ch in singly linked list in js isngly linked list js return linked list method javascript return linked list javascript check if a linked list contains data js how to add to the tail in js singly linked list js create linked list in node js what is a linked list in javascript linkedlist javascript singly linked list java script js Linked Lists Singly Linked .val linked list javascript javascript linked list singly linked list javascript build linked list javascript linked list js example and meaning javascript program for a Single Linked List node object linked list in js node object linked list in j s does javascript have linked lists does javascript have a linkedlist list node javascript linked list javascript example how to create linked list in js ListNode js how to access data from linked list javascript implementing linked list in js why implement linkedlist in objects in js javascript linked list in memory add a nodes in linked list javascript javasccript linked list new listnode keyword javascript new Listnode () js js linkedlist create a new node in a linked list in js linked list algorithm javascript javascript arrays implemented with linked lists? linklist example js linklist javascript how to make a linked list javascript javascript create linked list define linkedlist js example of linked list in javascript expample of linked list in javascript ListNode in js how to initialize linked list javascript linked list methods javascript linked list class javascript linked list data structure javascript add and remove element in linked list using javascript js efficient linked list javascript practical use for linked list go through linked list javascript javascript linked list to check for an element check for an element in linked list using javascript check for a value in linklist javascript is prototype in javascript a linked list ListNode() javascript built linkedlist object javascript javascript linked list example create a linked list javascript traverse a linked list javascript new listnode javascript get head linked list javascript javascript contains link list logic can linkedlists be used in for loop javascript what is linked list head and tail in javascript how to parse linked list es7 new list node javascript best js linked list examples of linked list in data structure javascript linked list add head javascript traverse linked list javascript insert a node at the head of a linked list javascript class for creating a linked list in js how to singly link list in javascript how make linkedlist in js listnode javascript manipulate linked in with js type ListNode js working with linked list js example linkedlist' object example js javascript linked list show all implementing a linked list node class javascript naming a linked list class javascript js linkedlist insert at first listnode javascript node.js name origin come from linked list how to check uif the type of the variabe is linked list in js create singly linked list in javascript linked list javascript addtohead linked list in js linked list with js linked list implementation in javascript how to make a node point to null in javascript linkedlist linked list javascript find operation how to traverse to last node in linked list js creating a linked list using javascript creating a link list using javascript javscript linked list linkedlist in js linked lists in js point to single list node javascript how to use list node javascript how to create a linkedlist in javascript js linked list as class Javascript linked list's is a linked list in java script 0 indexed cool examples of linked lists javascript insert linked list javascript next linked list javascript link list javascript how to work with linked list like structures in jquery generic linked list in javascript linker list js javascriip list link what is a linked list javascript linking nodes in javascript how to construct a linked list in javascipt create a linked list in javascript how to insert an item at the end of a linked list javascript linked list syntax in javascript linked list javascript implementation linked list example interview js How to write a linked list in JavaScirpt array linked list javascript js linked lists how to create a linked list in js connect one linked list to another in javascript javascript lnikedlist linked list add method js datastructures linked list methods js js listnode javascript listNode what are linked lists in javascript create linked list javascript javascript linkedlist js linked list example linked lists javascript insertat linked lists javascript appendingnode learn linked lists javascript implement list in node js delete a node in linked list javascript js LinkedList class linked list in javascript javascript linked list code example class Node javascript linked list linked lists in javascript linked list add at head javascript implement linked list in javascript are linked lists 0 indexed javascript linked list js linked lists js what is linked list in js linked list traversal javascript javascript linked list add at index how to traverse a linked list javascript linked list in javascript example do people ever use linked lists in javascript javascript linked list linkedlist in javascript this.head javascript js linked list linked lists javascript linked list javascript how to change linked list in js javascript use object as linked list Javascript singly linked list
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source