1

I have some FIFO calculations to be followed so I am using singly-linked-list When I am trying to insert data at specific location using insertAt(index,data) method it just does not work.

 var LinkedList = require('singly-linked-list')
 var testlist = new LinkedList();

 testlist.insertAt(1,'abc');

Output

{ 
    head: null,
    tail: null,
    size: 0,
    iterator: 
    { 
        list: [Circular], 
        currentNode: null 
    }
}

Please suggest me any fix or alternative for this.

0

1 Answer 1

0

The module you are using will not insert a new node at an index that is out of bounds based on the current size of the list.

You may have thought that 0 would never be out of bounds but it will be according to the condition in the source linked to above when the list is empty.

You could use the insert method instead, perhaps surrounded by a check to see if the list is empty or not.

3
  • What if I want to insert records at first 3 positions and my list is not empty Mar 15, 2016 at 11:57
  • @PriyankaPawar - If your list is not empty the insertAt method should work fine. See this example. Mar 15, 2016 at 11:59
  • ohh yes. Thank you so much Mar 15, 2016 at 12:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.