LinkedList - Creating function that takes a value, creates a node, and add it to end of list












2















This is the pseudocode we have to build the LL from:



FUNCTION push(element)
CREATE node
SET node.value TO element
SET node.next TO null

IF the head node does not exist
THEN SET head to node
ELSE
SET current to head
SET current.next to node
END IF
END FUNCTION


The pseudocode itself has an error in it as well.



Below is my attempt to follow that, but right now it's pointing
to the { right after value in the push function.



let head = null,
last,
node,
current,
value = element;

const linkedList = () => {
let node = new Node(value);

push(value) {
if(head === null) {
head = last = node;
} else {
last.next = node;
last = node;
}
}
}


Error : push(value) { <----- This curly bracket is throwing the error. Unexpected token.










share|improve this question




















  • 1





    Is this homework?

    – Mawg
    Nov 28 '18 at 8:31






  • 1





    "The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

    – Mawg
    Nov 28 '18 at 8:32






  • 1





    What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

    – Mawg
    Nov 28 '18 at 8:33













  • This is an exercise that I'm doing

    – cjl85
    Nov 28 '18 at 8:41






  • 1





    Added last line to clarify where the error is

    – cjl85
    Nov 28 '18 at 8:44
















2















This is the pseudocode we have to build the LL from:



FUNCTION push(element)
CREATE node
SET node.value TO element
SET node.next TO null

IF the head node does not exist
THEN SET head to node
ELSE
SET current to head
SET current.next to node
END IF
END FUNCTION


The pseudocode itself has an error in it as well.



Below is my attempt to follow that, but right now it's pointing
to the { right after value in the push function.



let head = null,
last,
node,
current,
value = element;

const linkedList = () => {
let node = new Node(value);

push(value) {
if(head === null) {
head = last = node;
} else {
last.next = node;
last = node;
}
}
}


Error : push(value) { <----- This curly bracket is throwing the error. Unexpected token.










share|improve this question




















  • 1





    Is this homework?

    – Mawg
    Nov 28 '18 at 8:31






  • 1





    "The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

    – Mawg
    Nov 28 '18 at 8:32






  • 1





    What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

    – Mawg
    Nov 28 '18 at 8:33













  • This is an exercise that I'm doing

    – cjl85
    Nov 28 '18 at 8:41






  • 1





    Added last line to clarify where the error is

    – cjl85
    Nov 28 '18 at 8:44














2












2








2








This is the pseudocode we have to build the LL from:



FUNCTION push(element)
CREATE node
SET node.value TO element
SET node.next TO null

IF the head node does not exist
THEN SET head to node
ELSE
SET current to head
SET current.next to node
END IF
END FUNCTION


The pseudocode itself has an error in it as well.



Below is my attempt to follow that, but right now it's pointing
to the { right after value in the push function.



let head = null,
last,
node,
current,
value = element;

const linkedList = () => {
let node = new Node(value);

push(value) {
if(head === null) {
head = last = node;
} else {
last.next = node;
last = node;
}
}
}


Error : push(value) { <----- This curly bracket is throwing the error. Unexpected token.










share|improve this question
















This is the pseudocode we have to build the LL from:



FUNCTION push(element)
CREATE node
SET node.value TO element
SET node.next TO null

IF the head node does not exist
THEN SET head to node
ELSE
SET current to head
SET current.next to node
END IF
END FUNCTION


The pseudocode itself has an error in it as well.



Below is my attempt to follow that, but right now it's pointing
to the { right after value in the push function.



let head = null,
last,
node,
current,
value = element;

const linkedList = () => {
let node = new Node(value);

push(value) {
if(head === null) {
head = last = node;
} else {
last.next = node;
last = node;
}
}
}


Error : push(value) { <----- This curly bracket is throwing the error. Unexpected token.







javascript linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 8:43







cjl85

















asked Nov 28 '18 at 8:29









cjl85cjl85

7510




7510








  • 1





    Is this homework?

    – Mawg
    Nov 28 '18 at 8:31






  • 1





    "The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

    – Mawg
    Nov 28 '18 at 8:32






  • 1





    What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

    – Mawg
    Nov 28 '18 at 8:33













  • This is an exercise that I'm doing

    – cjl85
    Nov 28 '18 at 8:41






  • 1





    Added last line to clarify where the error is

    – cjl85
    Nov 28 '18 at 8:44














  • 1





    Is this homework?

    – Mawg
    Nov 28 '18 at 8:31






  • 1





    "The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

    – Mawg
    Nov 28 '18 at 8:32






  • 1





    What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

    – Mawg
    Nov 28 '18 at 8:33













  • This is an exercise that I'm doing

    – cjl85
    Nov 28 '18 at 8:41






  • 1





    Added last line to clarify where the error is

    – cjl85
    Nov 28 '18 at 8:44








1




1





Is this homework?

– Mawg
Nov 28 '18 at 8:31





Is this homework?

– Mawg
Nov 28 '18 at 8:31




1




1





"The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

– Mawg
Nov 28 '18 at 8:32





"The pseudocode itself has an error in it as well" - but you are not going to tell us what it is?

– Mawg
Nov 28 '18 at 8:32




1




1





What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

– Mawg
Nov 28 '18 at 8:33







What does "` it's pointing to the { " mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {` ? There are several

– Mawg
Nov 28 '18 at 8:33















This is an exercise that I'm doing

– cjl85
Nov 28 '18 at 8:41





This is an exercise that I'm doing

– cjl85
Nov 28 '18 at 8:41




1




1





Added last line to clarify where the error is

– cjl85
Nov 28 '18 at 8:44





Added last line to clarify where the error is

– cjl85
Nov 28 '18 at 8:44












2 Answers
2






active

oldest

votes


















2














Beside the syntax error, you have a logical error as well. You need to treat the head node different from any other node and set the head property to node if not set, but if set, then assign to the last node the next node.



If you no head node, you can not set the last node, because there is actually no one.






class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
this.last = null;
}
push(value) {
var node = new Node(value);
if (!this.head) {
this.head = node;
} else {
this.last.next = node;
}
this.last = node;
}
}

var ll = new LinkedList;

ll.push(10);
console.log(ll);

ll.push(11);
console.log(ll);

ll.push(12);
console.log(ll);

.as-console-wrapper { max-height: 100% !important; top: 0; }





With check for an already inserted value.






class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
this.last = null;
}
push(value) {
var node = new Node(value),
temp = this.head;

while (temp) {
if (temp.value === value) return;
temp = temp.next;
}

if (!this.head) {
this.head = node;
} else {
this.last.next = node;
}
this.last = node;
}
}

var ll = new LinkedList;

ll.push(10);
console.log(ll);

ll.push(11);
console.log(ll);

ll.push(12);
console.log(ll);

ll.push(11);
console.log(ll);

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer


























  • thank you for that, i see where i was way off on

    – cjl85
    Nov 28 '18 at 9:04











  • do you have a moment to chat about linkedlists?

    – cjl85
    Nov 28 '18 at 9:26











  • yes, we can chat.

    – Nina Scholz
    Nov 28 '18 at 9:36











  • i don't know if i have the capabilities to create a chat room or how it works

    – cjl85
    Nov 28 '18 at 9:37











  • just ask here, and if the comments becomes more, the chat opportunity appears.

    – Nina Scholz
    Nov 28 '18 at 9:38



















1














And since we are at it.
Here is a version where you can insert append and remove nodes in ES6. No last node thougth since that would ruin the beauty of it. :)



class Node {
constructor(value) {
this.prev = null;
this.next = null;
this.value = value === undefined? null : value;
this.list = null;
}
remove() {
let prev = this.prev;
let next = this.next;
if (prev) {
prev.next = next;
}
if (next) {
next.prev = prev;
}
return this;
}
insert(node) {
let prev = this.prev;
if (prev) {
prev.next = node;
node.prev = prev;
}
this.prev = node;
node.next = this;
}
append(node) {
let next = this.next;
if (next) {
next.prev = node;
node.next = next;
}
this.next = node;
node.prev = this;
}
}
class LinkedList {
constructor() {
this.head = null;
}
get last() {
return this.list.length > 0 ? this.list[this.list.length] : null;
}
get values() {
let node = this.head;
let values = ;
while(node) {
values.push(node.value);
node = node.next;
};
return values;
}

push(node) {
node.prev = null;
node.next = null;
if (this.head) {
this.head.prev = node;
node.next = this.head;
}
this.head = node;
}
find(v) {
let node = this.head;
let fn = v;
if (!(v instanceof Function)) fn = (el) => el.value === v;
while(node && !fn(node)) {node = node.next;};
return node;
}
forEach(fn) {
let node = this.head;
while(node) {fn(node); node = node.next;};
}
}


Usable like so:



let ll = new LinkedList();
let n1= new Node(1);
let n2= new Node(2);
let n3= new Node(3);
ll.push(n1);
ll.push(n2);
ll.find(1).append(n3);
console.log(ll.values);
ll.find(3).remove();
console.log(ll.values);
ll.find(2).append(n3);
console.log(ll.values);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515119%2flinkedlist-creating-function-that-takes-a-value-creates-a-node-and-add-it-to%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Beside the syntax error, you have a logical error as well. You need to treat the head node different from any other node and set the head property to node if not set, but if set, then assign to the last node the next node.



    If you no head node, you can not set the last node, because there is actually no one.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    With check for an already inserted value.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }








    share|improve this answer


























    • thank you for that, i see where i was way off on

      – cjl85
      Nov 28 '18 at 9:04











    • do you have a moment to chat about linkedlists?

      – cjl85
      Nov 28 '18 at 9:26











    • yes, we can chat.

      – Nina Scholz
      Nov 28 '18 at 9:36











    • i don't know if i have the capabilities to create a chat room or how it works

      – cjl85
      Nov 28 '18 at 9:37











    • just ask here, and if the comments becomes more, the chat opportunity appears.

      – Nina Scholz
      Nov 28 '18 at 9:38
















    2














    Beside the syntax error, you have a logical error as well. You need to treat the head node different from any other node and set the head property to node if not set, but if set, then assign to the last node the next node.



    If you no head node, you can not set the last node, because there is actually no one.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    With check for an already inserted value.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }








    share|improve this answer


























    • thank you for that, i see where i was way off on

      – cjl85
      Nov 28 '18 at 9:04











    • do you have a moment to chat about linkedlists?

      – cjl85
      Nov 28 '18 at 9:26











    • yes, we can chat.

      – Nina Scholz
      Nov 28 '18 at 9:36











    • i don't know if i have the capabilities to create a chat room or how it works

      – cjl85
      Nov 28 '18 at 9:37











    • just ask here, and if the comments becomes more, the chat opportunity appears.

      – Nina Scholz
      Nov 28 '18 at 9:38














    2












    2








    2







    Beside the syntax error, you have a logical error as well. You need to treat the head node different from any other node and set the head property to node if not set, but if set, then assign to the last node the next node.



    If you no head node, you can not set the last node, because there is actually no one.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    With check for an already inserted value.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }








    share|improve this answer















    Beside the syntax error, you have a logical error as well. You need to treat the head node different from any other node and set the head property to node if not set, but if set, then assign to the last node the next node.



    If you no head node, you can not set the last node, because there is actually no one.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    With check for an already inserted value.






    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }








    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value);
    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }





    class Node {
    constructor(value) {
    this.value = value;
    this.next = null;
    }
    }

    class LinkedList {
    constructor() {
    this.head = null;
    this.last = null;
    }
    push(value) {
    var node = new Node(value),
    temp = this.head;

    while (temp) {
    if (temp.value === value) return;
    temp = temp.next;
    }

    if (!this.head) {
    this.head = node;
    } else {
    this.last.next = node;
    }
    this.last = node;
    }
    }

    var ll = new LinkedList;

    ll.push(10);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    ll.push(12);
    console.log(ll);

    ll.push(11);
    console.log(ll);

    .as-console-wrapper { max-height: 100% !important; top: 0; }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '18 at 10:02

























    answered Nov 28 '18 at 8:59









    Nina ScholzNina Scholz

    192k15104176




    192k15104176













    • thank you for that, i see where i was way off on

      – cjl85
      Nov 28 '18 at 9:04











    • do you have a moment to chat about linkedlists?

      – cjl85
      Nov 28 '18 at 9:26











    • yes, we can chat.

      – Nina Scholz
      Nov 28 '18 at 9:36











    • i don't know if i have the capabilities to create a chat room or how it works

      – cjl85
      Nov 28 '18 at 9:37











    • just ask here, and if the comments becomes more, the chat opportunity appears.

      – Nina Scholz
      Nov 28 '18 at 9:38



















    • thank you for that, i see where i was way off on

      – cjl85
      Nov 28 '18 at 9:04











    • do you have a moment to chat about linkedlists?

      – cjl85
      Nov 28 '18 at 9:26











    • yes, we can chat.

      – Nina Scholz
      Nov 28 '18 at 9:36











    • i don't know if i have the capabilities to create a chat room or how it works

      – cjl85
      Nov 28 '18 at 9:37











    • just ask here, and if the comments becomes more, the chat opportunity appears.

      – Nina Scholz
      Nov 28 '18 at 9:38

















    thank you for that, i see where i was way off on

    – cjl85
    Nov 28 '18 at 9:04





    thank you for that, i see where i was way off on

    – cjl85
    Nov 28 '18 at 9:04













    do you have a moment to chat about linkedlists?

    – cjl85
    Nov 28 '18 at 9:26





    do you have a moment to chat about linkedlists?

    – cjl85
    Nov 28 '18 at 9:26













    yes, we can chat.

    – Nina Scholz
    Nov 28 '18 at 9:36





    yes, we can chat.

    – Nina Scholz
    Nov 28 '18 at 9:36













    i don't know if i have the capabilities to create a chat room or how it works

    – cjl85
    Nov 28 '18 at 9:37





    i don't know if i have the capabilities to create a chat room or how it works

    – cjl85
    Nov 28 '18 at 9:37













    just ask here, and if the comments becomes more, the chat opportunity appears.

    – Nina Scholz
    Nov 28 '18 at 9:38





    just ask here, and if the comments becomes more, the chat opportunity appears.

    – Nina Scholz
    Nov 28 '18 at 9:38













    1














    And since we are at it.
    Here is a version where you can insert append and remove nodes in ES6. No last node thougth since that would ruin the beauty of it. :)



    class Node {
    constructor(value) {
    this.prev = null;
    this.next = null;
    this.value = value === undefined? null : value;
    this.list = null;
    }
    remove() {
    let prev = this.prev;
    let next = this.next;
    if (prev) {
    prev.next = next;
    }
    if (next) {
    next.prev = prev;
    }
    return this;
    }
    insert(node) {
    let prev = this.prev;
    if (prev) {
    prev.next = node;
    node.prev = prev;
    }
    this.prev = node;
    node.next = this;
    }
    append(node) {
    let next = this.next;
    if (next) {
    next.prev = node;
    node.next = next;
    }
    this.next = node;
    node.prev = this;
    }
    }
    class LinkedList {
    constructor() {
    this.head = null;
    }
    get last() {
    return this.list.length > 0 ? this.list[this.list.length] : null;
    }
    get values() {
    let node = this.head;
    let values = ;
    while(node) {
    values.push(node.value);
    node = node.next;
    };
    return values;
    }

    push(node) {
    node.prev = null;
    node.next = null;
    if (this.head) {
    this.head.prev = node;
    node.next = this.head;
    }
    this.head = node;
    }
    find(v) {
    let node = this.head;
    let fn = v;
    if (!(v instanceof Function)) fn = (el) => el.value === v;
    while(node && !fn(node)) {node = node.next;};
    return node;
    }
    forEach(fn) {
    let node = this.head;
    while(node) {fn(node); node = node.next;};
    }
    }


    Usable like so:



    let ll = new LinkedList();
    let n1= new Node(1);
    let n2= new Node(2);
    let n3= new Node(3);
    ll.push(n1);
    ll.push(n2);
    ll.find(1).append(n3);
    console.log(ll.values);
    ll.find(3).remove();
    console.log(ll.values);
    ll.find(2).append(n3);
    console.log(ll.values);





    share|improve this answer




























      1














      And since we are at it.
      Here is a version where you can insert append and remove nodes in ES6. No last node thougth since that would ruin the beauty of it. :)



      class Node {
      constructor(value) {
      this.prev = null;
      this.next = null;
      this.value = value === undefined? null : value;
      this.list = null;
      }
      remove() {
      let prev = this.prev;
      let next = this.next;
      if (prev) {
      prev.next = next;
      }
      if (next) {
      next.prev = prev;
      }
      return this;
      }
      insert(node) {
      let prev = this.prev;
      if (prev) {
      prev.next = node;
      node.prev = prev;
      }
      this.prev = node;
      node.next = this;
      }
      append(node) {
      let next = this.next;
      if (next) {
      next.prev = node;
      node.next = next;
      }
      this.next = node;
      node.prev = this;
      }
      }
      class LinkedList {
      constructor() {
      this.head = null;
      }
      get last() {
      return this.list.length > 0 ? this.list[this.list.length] : null;
      }
      get values() {
      let node = this.head;
      let values = ;
      while(node) {
      values.push(node.value);
      node = node.next;
      };
      return values;
      }

      push(node) {
      node.prev = null;
      node.next = null;
      if (this.head) {
      this.head.prev = node;
      node.next = this.head;
      }
      this.head = node;
      }
      find(v) {
      let node = this.head;
      let fn = v;
      if (!(v instanceof Function)) fn = (el) => el.value === v;
      while(node && !fn(node)) {node = node.next;};
      return node;
      }
      forEach(fn) {
      let node = this.head;
      while(node) {fn(node); node = node.next;};
      }
      }


      Usable like so:



      let ll = new LinkedList();
      let n1= new Node(1);
      let n2= new Node(2);
      let n3= new Node(3);
      ll.push(n1);
      ll.push(n2);
      ll.find(1).append(n3);
      console.log(ll.values);
      ll.find(3).remove();
      console.log(ll.values);
      ll.find(2).append(n3);
      console.log(ll.values);





      share|improve this answer


























        1












        1








        1







        And since we are at it.
        Here is a version where you can insert append and remove nodes in ES6. No last node thougth since that would ruin the beauty of it. :)



        class Node {
        constructor(value) {
        this.prev = null;
        this.next = null;
        this.value = value === undefined? null : value;
        this.list = null;
        }
        remove() {
        let prev = this.prev;
        let next = this.next;
        if (prev) {
        prev.next = next;
        }
        if (next) {
        next.prev = prev;
        }
        return this;
        }
        insert(node) {
        let prev = this.prev;
        if (prev) {
        prev.next = node;
        node.prev = prev;
        }
        this.prev = node;
        node.next = this;
        }
        append(node) {
        let next = this.next;
        if (next) {
        next.prev = node;
        node.next = next;
        }
        this.next = node;
        node.prev = this;
        }
        }
        class LinkedList {
        constructor() {
        this.head = null;
        }
        get last() {
        return this.list.length > 0 ? this.list[this.list.length] : null;
        }
        get values() {
        let node = this.head;
        let values = ;
        while(node) {
        values.push(node.value);
        node = node.next;
        };
        return values;
        }

        push(node) {
        node.prev = null;
        node.next = null;
        if (this.head) {
        this.head.prev = node;
        node.next = this.head;
        }
        this.head = node;
        }
        find(v) {
        let node = this.head;
        let fn = v;
        if (!(v instanceof Function)) fn = (el) => el.value === v;
        while(node && !fn(node)) {node = node.next;};
        return node;
        }
        forEach(fn) {
        let node = this.head;
        while(node) {fn(node); node = node.next;};
        }
        }


        Usable like so:



        let ll = new LinkedList();
        let n1= new Node(1);
        let n2= new Node(2);
        let n3= new Node(3);
        ll.push(n1);
        ll.push(n2);
        ll.find(1).append(n3);
        console.log(ll.values);
        ll.find(3).remove();
        console.log(ll.values);
        ll.find(2).append(n3);
        console.log(ll.values);





        share|improve this answer













        And since we are at it.
        Here is a version where you can insert append and remove nodes in ES6. No last node thougth since that would ruin the beauty of it. :)



        class Node {
        constructor(value) {
        this.prev = null;
        this.next = null;
        this.value = value === undefined? null : value;
        this.list = null;
        }
        remove() {
        let prev = this.prev;
        let next = this.next;
        if (prev) {
        prev.next = next;
        }
        if (next) {
        next.prev = prev;
        }
        return this;
        }
        insert(node) {
        let prev = this.prev;
        if (prev) {
        prev.next = node;
        node.prev = prev;
        }
        this.prev = node;
        node.next = this;
        }
        append(node) {
        let next = this.next;
        if (next) {
        next.prev = node;
        node.next = next;
        }
        this.next = node;
        node.prev = this;
        }
        }
        class LinkedList {
        constructor() {
        this.head = null;
        }
        get last() {
        return this.list.length > 0 ? this.list[this.list.length] : null;
        }
        get values() {
        let node = this.head;
        let values = ;
        while(node) {
        values.push(node.value);
        node = node.next;
        };
        return values;
        }

        push(node) {
        node.prev = null;
        node.next = null;
        if (this.head) {
        this.head.prev = node;
        node.next = this.head;
        }
        this.head = node;
        }
        find(v) {
        let node = this.head;
        let fn = v;
        if (!(v instanceof Function)) fn = (el) => el.value === v;
        while(node && !fn(node)) {node = node.next;};
        return node;
        }
        forEach(fn) {
        let node = this.head;
        while(node) {fn(node); node = node.next;};
        }
        }


        Usable like so:



        let ll = new LinkedList();
        let n1= new Node(1);
        let n2= new Node(2);
        let n3= new Node(3);
        ll.push(n1);
        ll.push(n2);
        ll.find(1).append(n3);
        console.log(ll.values);
        ll.find(3).remove();
        console.log(ll.values);
        ll.find(2).append(n3);
        console.log(ll.values);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 9:37









        JGoodgiveJGoodgive

        542413




        542413






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515119%2flinkedlist-creating-function-that-takes-a-value-creates-a-node-and-add-it-to%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks