Question

could someone help me make a linked list in javascript, that has methods of inserting, updating...

could someone help me make a linked list in javascript, that has methods of inserting, updating element by index, deleting by index, obtaining all the elements, obtaining elements by index, I would be very grateful to you to help me :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find code below and output screenshot at the end

function Node(value) {
this.value = value;
this.next = undefined;
}

function LinkedList() {
var head = undefined;
var length = 0;

return {
insertByValue: function(data) {
if (!data) return;

var node = new Node(data);

if (head) {
node.next = head;
}

head = node;
length++;
},
deleteByValue: function(value) {
var currentNode = head;

if (head.value === value) {
head = head.next;
return;
}

while (currentNode) {
if (currentNode.next) {
var next = currentNode.next;

if (next.value === value) {
currentNode.next = next.next;
length--;
break;
}
}

currentNode = currentNode.next;
}
},
searchByValue: function(value) {
var currentNode = head;
var foundNode = undefined;

while (currentNode) {
if (currentNode.value === value) {
foundNode = currentNode;
break;
}

currentNode = currentNode.next;
}

return foundNode;
},
get size() {
return length;
},
print: function() {
var result = [];

var currentNode = head;
while (currentNode) {
result.push(currentNode.value);

currentNode = currentNode.next;
}

return result;
},
   UpdateByIndex: function(index, value) {
       if (index > length) return;

var currentNode = head;
   var count = 0;
while (currentNode) {
if(count === index) {
           currentNode.value = value;
       }
       count++;
currentNode = currentNode.next;
}
   },
   deleteByIndex: function(index) {
var currentNode = head;
   var count = 0;
while (currentNode) {
       if (index == 0) {
           head = head.next;
           return;
       }
  
       if (currentNode && currentNode.next) {
           var next = currentNode.next;
           count++;
           if (index === count) {
               currentNode.next = next.next;
               length--;
               break;
           }
           }

           currentNode = currentNode.next;
}
},
   getByIndex: function(index) {
var currentNode = head;
var foundNode = undefined;
   var count = 0;
   if(index > length) return;
  
while (currentNode) {
  
if (index === count) {
foundNode = currentNode;
break;
}
       count++;
currentNode = currentNode.next;
}

return foundNode;
}
}
}

Add a comment
Know the answer?
Add Answer to:
could someone help me make a linked list in javascript, that has methods of inserting, updating...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Someone could help me to make in assembler 8086 (MASM), a program that does the SHELL...

    Someone could help me to make in assembler 8086 (MASM), a program that does the SHELL SORT, I would be grateful to help me :)

  • Would someone be able to help me with this please? It has to do with Linked...

    Would someone be able to help me with this please? It has to do with Linked List Arrays, and I'm very confused on what to do. Any help along with steps would be very much appreciated. Cheers! You are hired by a company to implement a singly-linked list that will hold integers, using Java programming language. However, the boss of the company insists that you don’t use any objects or structures in your implementation. You may use only integer arrays...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Would you please help me to do this problem? this is c programming. 1. write figures of inserting...

    Would you please help me to do this problem? this is c programming. 1. write figures of inserting 3 elements into a stack linked list and a queue linked list with code fragments and then delete 2 elements from each with associated code fragments. thank you.

  • JavaScript Can someone please help me with a solution to this THNX ! Given a list...

    JavaScript Can someone please help me with a solution to this THNX ! Given a list of numbers and a number B, return whether any two numbers from the list add up to B.

  • Could someone help me out with the answering this? Explaining the theory would be very helpful...

    Could someone help me out with the answering this? Explaining the theory would be very helpful A NumberList is a linked list used to represent the digits of a number. For example 983 is represented as: [ Node(9), Node(8), Node (3) ] Write the pseudocode for an algorithm which performs addition on 2 NumberLists, such that: [ Node (9), Node(8), Node (3) ] + [ Node(1), Node (0), Node (4), Node (3)] = [ Node (2), Node(0), Node(2), Node (6)]...

  • Can someone please help, third time I'm asking. I need a basic javascript with no push...

    Can someone please help, third time I'm asking. I need a basic javascript with no push or splice command. Please don't post a picture of the code,because my vision is poor and I won't be able to see it. Also, I need breakdown of what's in HTMLand what' s in javascript. All requirements are below. Thanks for your help. This is a 2 part assignment, but both parts can be completed in one program. Also, please follow ALL Required Programming...

  • could someone help me with these problems I would be very grateful :) Problem a) a...

    could someone help me with these problems I would be very grateful :) Problem a) a small factory uses a 750W compressor, a 5.2kW air conditioning unit and a 1250J / s rotor for its production What will be the capacity of the fuse (in A) to support the load of the 3 devices connected in parallel at a voltage of 480V? b) Referring to the previous problem, what is the cost (in $) of using the 5.2 kW air...

  • Could someone please help me solve this in Python? Brief explanation of the code comments would...

    Could someone please help me solve this in Python? Brief explanation of the code comments would be greatly appreciated. indices_second_biggest: This function returns a list containing the indices of the second largest value in its sole argument, a nonempty matrix (list of lists), i.e. [row index, col index]. If the largest value occurs more than once, it counts as the second largest also. If there is only one element in the matrix, return [0, 0].

  • someone could help me with these problems :(, I would be very grateful if they detail...

    someone could help me with these problems :(, I would be very grateful if they detail your answer Problem a) A capacitor C1 = 3uF is connected in series with a capacitor C2 of parallel plates, which has a dielectric of dielectric constant 2.5, distance of separation of plates of 2x10 ^ (- 4) and area of ​ 54.23m ^ 2, both initially charged and subsequently connected to an 8V battery What is the energy density C2 (in SI units)?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT