0
let playlist_data = ["Reset","I believe","Whatever it takes","Never Enough For me","Do you Remember"];

function createNode(data="",next=null,prev=null){
  this.data = data;
  this.next = next;
}

function createPlayList(playlist_data,len){
    var arr = playlist_data;
    var playList = null;
    for(let i=0;i<len;i++){
        if(playList === null){
            playList = new createNode(arr[i]);
        }
        else {
           var current = playList;
           while(current.next !== null){
               current = current.next;
           }
           current.next = new createNode(arr[i]);           
        }
    }
    return playList;
}

console.log(createPlayList(playlist_data,playlist_data.length));


function createPlayList2(playlist_data,len){
    var arr = playlist_data;
    var playList = null;
    for(let i=0;i<len;i++){
        if(playList === null){
            playList = new createNode(arr[i]);
        }
        else {
           //var current = playList;
           while(playList.next !== null){
               playList = playList.next;
           }
           playList.next = new createNode(arr[i]);  
        }
    }
    return playList;
}
  • createPlayList2() doesn't work I know exactly why !
  • createPlayList() works because I used the copy i.e var current = playList; It has the same reference. Still it is working fine. I am confused how ? Which part of code or concept am I missing ?
  • Also If I want to create doubly linked list using one more key 'prev' , using vanilla javascript ! Can I ?

Edited---------- Why createPlayList2() doesn't work ?

function createPlayList(playlist_data,len){
    var arr = playlist_data;
    var playList = null;
    for(let i=0;i<len;i++){
        if(playList === null){
            console.log("Null PlayList");
            playList = new createNode(arr[i]);
        }
        else {
           //var current = playList;
           console.log("Before Traversing");
           console.log(playList);
           while(playList.next !== null){
               playList = playList.next;
               console.log("Traversing");
               console.log(playList);
           }
           playList.next = new createNode(arr[i]);
           console.log("After Traversing");
           console.log(playList); 
        }
    }
    return playList;
}
console.log("Called Funtion");
console.log(createPlayList(playlist_data,playlist_data.length))
console.log("Ends");

Output -

Called Function
Null PlayList // when playList is empty at i=0
Before Traversing // when playlist is not empty at i=1
{data: "Reset", next: null} // current playList value

After Traversing // after while loop
{data: "Reset", next:{data: "I believe",next: null}} 

Before Traversing // at i=2
{data: "Reset", next:{data: "I believe",next: null}} // current PlayList

Traversing // inside while loop
 {data: "I believe", next: null} // current playList changes to this !

========================.............
Rest the loop continues, I have changed the original value of playList while traversing. The reason it is not working !!.
3
  • "createPlayList2() doesn't work I know exactly why !" doesn't work how? What's happening? What do you expect? Do you have a minimal reproducible example? Commented Nov 13, 2020 at 10:27
  • Edited the post, "createPlayList2() doesn't work I know exactly why ! " Commented Nov 13, 2020 at 10:59
  • You are asking multiple questions. Focus on one question. Commented Nov 13, 2020 at 22:31

1 Answer 1

1

The difference is that in the second version you assign a new value to playList even when it already points to the first node of the list. The only time you should assign to playList is when the list is empty. So your second code will work for adding the first element. But if you add a second element, you eventually make playList point to that new node, and so you "lost" the original first node -- there is nothing referencing that node now.

The principle is that once you have playList initialised to the first node in the list, you should not alter it, unless of course you want to remove the first node in the list.

The first version is correct, because it uses another (temporary) variable to walk through the list, leaving playList untouched.

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

Comments

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.