Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A proper iteration of all LinkedList nodes in Javascript [closed]

Tags:

javascript

In my code snippet I am iterating of LinkedList nodes using a while loop, I am console.logging each node value. My while loop exists and my last value has to be console.logged next line after the while loop, anyway to make a more elegant iterator for my LinkedList?

function LinkedList() {
    this.head = null;
};

LinkedList.prototype = (function () {
    function reverseAll(current, prev) {
        if (!current.next) { //we have the head
            this.head = current;
            this.head.next = prev;
        }

        var next = current.next;
        current.next = prev;

        reverseAll(next, current);
    };

    return {
        constructor: LinkedList,

        reverse: function () {
            reverseAll(this.head, null);
        },

        head: function() {
            return this.head;
        }
    }
})();

LinkedList.prototype.add = function(value) {
    var node = {
        value: value,
        next: null
    };

    var current;

    if (this.head === null) {
        this.head = node;
    } else {
        current = this.head;
        while (current.next) {
            current = current.next;
        }
        current.next = node;
    }

    return node;
}

LinkedList.prototype.remove = function(node) {
    var current, value = node.value;

    if (this.head !== null) {
        if (this.head === node) {
            this.head = this.head.next;
            node.next = null;
            return value;
        }
        //find node if node not head
        current = this.head;
        while (current.next) {
            if (current.next === node) {
                current.next = node.next;
                return value;
            }

            current = current.next;
        }
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function() {
    var obj = new LinkedList();
    
    for (var i = 1; i <= 10; i++) {
        obj.add(i);
    }

    var current = JSON.parse(JSON.stringify(obj.head));

    while (current.next) {
        console.log(current.value);

        current = current.next;

    }
    //not so hot iteration, printing last value that would be great if printed in the while loop
    console.log(current.value);
});
</script>
like image 420
Brian Ogden Avatar asked Oct 25 '15 19:10

Brian Ogden


People also ask

How do you iterate through a node in a linked list?

To iterate the LinkedList using the iterator we first create an iterator to the current list and keep on printing the next element using the next() method until the next element exists inside the LinkedList. We check if the LinkedList contains the next element using the hasNext() method.

Is there a LinkedList in JavaScript?

In this article, we will be implementing the LinkedList data structure in Javascript. LinkedList is the dynamic data structure, as we can add or remove elements at ease, and it can even grow as needed. Just like arrays, linked lists store elements sequentially, but don't store the elements contiguously like an array.

How do you pass a linked list in JavaScript?

To traverse the linked list, we use a while statement to go from head to the last node (fourth). The statement will continue to traverse the linked list until it reaches a value that is null. We also print the result of the data from each element using a function called printListData().

What is ListNode in JavaScript?

As stated earlier, a list node contains two items: the data and the pointer to the next node. We can implement a list node in JavaScript as follows: class ListNode { constructor(data) { this. data = data this.


1 Answers

Just test for the object, not if it is linked to the next object.

while (current) { // while not null
    console.log(current.value);
    current = current.next; 
}
like image 149
Blindman67 Avatar answered Oct 26 '22 22:10

Blindman67