Cracking the Coding Interview - 2 Linked Lists

Implementation of Slingly Linked List


In [ ]:
class Circle {
    constructor (radius) {
        this.radius = radius;
    }
    get area () {
        return Math.PI * this.radius * this.radius;
    }
    set area (n) {
        this.radius = Math.sqrt(n / Math.PI);
    }
}

In [1]:
class SLLNode {
    constructor(data) {
        this.data = data
        this.next = null
    }
    get end () {
        
    }
}


Out[1]:
[Function: SLLNode]

In [ ]:
var traverse = sllNode => {
    
}

In [2]:
var myNode = new SLLNode(3)


evalmachine.<anonymous>:6
    set data (newData) {
             ^

RangeError: Maximum call stack size exceeded
    at SLLNode.set data [as data] (evalmachine.<anonymous>:6:14)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)
    at SLLNode.set data [as data] (evalmachine.<anonymous>:7:19)

In [ ]:
myNode.next = new SLLNode(5)

In [ ]:
myNode.next

In [ ]:


In [ ]:
var SLLNode = val => {
    let value = val
    const getValue = () => value
    const setValue = newValue => value = newValue
    
    let next = null
    const getNext = () => next
    const setNext = Node => next = Node
    const traverse = () => {
        if (value) console.log(value)
        if (next) next.traverse()
    }
    return {getValue, setValue, getNext, setNext, traverse}
}

In [ ]:
var myNode = SLLNode(1)

In [ ]:
myNode.getValue()

In [ ]:
myNode.setValue(5)

In [ ]:
myNode.getValue()

In [ ]:


In [ ]:
myNode.setNext(SLLNode(2))

In [ ]:
myNode.traverse()

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
myC = new Circle(3)

In [ ]:
myC.area = 3

In [ ]:
myC.radius

In [ ]: