prg-titech / Kanon

A live programming environment specialized for data structure programming.
https://prg-titech.github.io/Kanon/
MIT License
68 stars 4 forks source link

Disappearing nodes after mutation #12

Closed masuhar closed 7 years ago

masuhar commented 7 years ago

With the following code, the display only shows nodes 888, 456, and 789 (i.e., 123 and 999 are disappeared).

class Node {
    constructor(val, next) {
        this.val = val;
        this.next = next;
        if (next != null) {
            this.next.prev = this;
        }
    }
    insertAfter(pos, value) {
        if (pos == 0) {
            var n = new Node(value,this.next);
            this.next = n;

        } else {
            this.next.insertAfter(pos-1, value);
        }
    }
}

var lst = new Node(123, new Node(456, new Node(789, null)));
lst.insertAfter(0, 999);
lst.insertAfter(1, 888);

image

(edit) Commenting out the lines 7-9 gives the hidden nodes back. So the assignment this.next.prev = this; might be the cause of the problem. image