开发者

Getting an illegal constructor error when trying to create a new Node

开发者 https://www.devze.com 2022-12-07 20:34 出处:网络
I am getting illegal constructor when creating a new Node. I have tried doing it with array and it works but whenever I use Node it just gives me an error saying illegal instructor.

I am getting illegal constructor when creating a new Node.

I have tried doing it with array and it works but whenever I use Node it just gives me an error saying illegal instructor.

Here is my code:

class SinglyLinkedList {
    constructor(){
        this.head =  null;
        this.tail = null;
        this.length = 0;
    }
    
    push(value)
        {
         const newNode = new Node(value)
            if(this.head===null){
            this.head = newNode
            this.tail = this.head
        }
        else{
            
            this.tail.next = newNode
            this.tail  = this.tail.next
             }
             this.length+=1
             return this
  开发者_StackOverflow  }
}

const list =  new SinglyLinkedList()

list.push('Hi')
list.push('this')
list.push('is')
list.push('Aman')

console.log(list.tail)


Node() is undefined that's why it is an illegal constructor.

Add this to your code.

class Node {
    constructor(value) {
        this.value =  value;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消