2

I am writing linkedlist datastructure using javascript. I have issue with inserting new node in a particular position. I have following code.

var node3 = {
    data: 4,
    next: null
},
node2 = {
    data: 3,
    next: node3
},
node1 = {
    data: 1,
    next: node2
},
newNode = {
    data: 2,
    next: null
},
head = node1, current = head, pos = 3, i = 1, prev;

while (current.next) {
    if (pos === i) {
        prev.next = newNode;
        newNode.next = current;
        break;
    }
    prev = current;
    i++;
    current = current.next;
}

I am creating 3 nodes initially and finally a newNode. I want to insert this newNode at position 3 i.e. pos = 3 which means the end of the list. My above code inserts nodes to other places but it does not at the end. Why and how can I solve this?

4 Answers 4

1

When you iterate over node3, current.next == null, so the loop doesn't execute, so you'll need to come up with another condition for your loop.

Also you should use break instead of return since you're not in a function.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you add sample example of another condition?
You could try change it to current.next || prev.next instead. The difference it will make is when current.next fails and previous.next is true which would be at an end node.
Where could I add current.next || prev.next?
0

You can try dynamic creation of linked list like this.

var Linkedlist = function() {
this.head = null;
this.tail = null;

};

function createNode( val ) {
var node = {};
node[ "key" ] = val;
node[ "prev" ] = null;
node[ "next" ] = null;
return node;

}

Linkedlist.prototype.insert = function( val ) {
if ( this.head === null ) {
    this.head = createNode( val );
    this.tail = createNode( val );
} else {
    var newNode = createNode( val );
    newNode[ "next" ] = this.head;
    newNode[ "prev" ] = null;
    this.head[ "prev" ] = newNode;
    this.head = newNode;
}
console.log(this);

};

Linkedlist.prototype.search = function( val ) {
var node = this.head;
while ( node[ "key"] != val ) {
    node = node[ "next" ];
    if ( node === null ) {
        break;
    }
}
return node;

};

Linkedlist.prototype.remove =  function( val ) {
var node = this.search(val);
if ( node != null ) {
    if ( node[ "next" ] === null && node[ "prev" ] === null ) {
        this.head = null;
        this.tail = null;
    } else {
        if ( node[ "key"] === this.head[ "key" ] ) {
            this.head = node[ "next" ];
            this.head[ "prev" ] = null;
        } else if ( node[ "key"] === this.tail[ "key" ]) {
            this.tail = node[ "prev" ];
            this.tail[ "next" ] = null;
        } else {
            node[ "prev" ][ "next" ] = node[ "next" ];
            node[ "next" ][ "prev" ] = node[ "prev" ];
        }
    }
    console.log( this );
} else {
    console.log("key doesnt exist");
}

};

Linkedlist.prototype.largest = function() {
var node = this.head;
var largest = this.head;
while ( node[ "next"] !== null ) {
    if ( node[ "key"] > largest[ "key" ] ) {
        largest = node;
    }
    node = node[ "next" ];
}
return largest;

};

Instantiation of this can be done as shown below.

linkedlist = new Linkedlist();
linkedlist.insert(1);
linkedlist.insert(2);
linkedlist.insert(3);
linkedlist.insert(4);
linkedlist.remove(2);
linkedlist.largest();

Comments

0

With LinkedList, insert at position is not a common practice as LinkedLists don't maintain position for each node, rather it maintains links between nodes. Usually we want to insert before/after a node. More commonly, we want to insert at the end of the list. Below is code to insertAfter a sourceNode

this.insertAfter = function(sourceNode, node) {
    var curNode = head;
    while (curNode != null) {
        if (curNode === sourceNode) {
            curNode.setNext(node.setNext(curNode.getNext()));
            return;
        }
        else curNode = curNode.getNext(); 
    }
};

Comments

0

With your structure ideally insertAt function would look like:

var insertAt = function(head, item, index) {
    var curr = head;
    var count = 0;
    while (curr !== null) {
        if (count === index) {
            var newNode = { data: item, next: curr.next };
            curr.next = newNode;
            break;
        }
        count++;
        curr = curr.next;
    }
};

insertAt(head, 2, 3);  

Let me know if this works.
Also look at this LinkedList class I have created, insertAt function is currently missing, but from this stack question, I am planning to add it in my class as well.
Class- GitHub
NPM package - @dsinjs/linked-list
Complete documentation

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.