Node.js examples for Data Structure:List
linked list using class syntax
//linked list// ww w. java 2 s . c om class Node { constructor(data) { this.data = data this.next = null this.length = 1 } } class LinkedList { constructor(node) { this.head = node this.tail = node this.length = 1 } insert(node) { this.tail.next = node this.tail = node this.length++ } insertInPlace(node, i) { if(i > this.length) { return } let current = this.head for(let index = 1; index < i; index++) { current = current.next } node.next = current.next current.next = node } } //create a new linked list called 'food' const food = new LinkedList(new Node('burrito')) //instert foods into food linked list food.insert(new Node('taco')) food.insert(new Node('cheese')) food.insert(new Node('bagel')) food.insertInPlace(new Node('fruit'), 4) console.log(JSON.stringify(food))