Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: add a method for converting a linked list to a string #39

Open
erelsgl opened this issue Jul 9, 2014 · 0 comments
Open

Request: add a method for converting a linked list to a string #39

erelsgl opened this issue Jul 9, 2014 · 0 comments

Comments

@erelsgl
Copy link

erelsgl commented Jul 9, 2014

While debugging linked lists, it could be useful to have a function that converts the list to a string representing the elements. Here is a function I use now:

LinkedList.Doubly.Circular.prototype.toString  = function() {
    var s = "";
    this.forEach(function(node, i) {
        var sfield = "";
        for (var field in node) {
            if (node.hasOwnProperty(field) && field!='prev' && field !='next' && field !='list') {
                if (sfield) sfield+=",";
                sfield += field+":"+node[field];
            }
        }

        if (s) s+=",\n ";
        s +="{"+sfield+"}"
    });
    return "["+s+"]";
}

Example:

var list = new LinkedList.Doubly.Circular();
var foo = {f:1}, bar = {b:1}, baz = {z:1};
list.push(foo);
list.push(bar);
list.push(baz);
console.log(list.toString());

Result:

[{f:1}, {b:1}, {z:1}]

I believe it can be useful to others too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant