开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜