Question

In: Computer Science

This all has to be in javascript Use this linked list to answer the questions below:...

This all has to be in javascript

Use this linked list to answer the questions below:

var list = new LinkedList();

list.append(1);

list.append(2);

list.append(3);

list.append(4);

1. We are going to extend LinkedList with a sum method. As the name implies, all elements in the linked list should be summed together. First, write a few tests that will eventually show your code works correctly (after you implement the function in (2)). Note: see the LinkedList implementation above for more details. Hint: note that the sum of the elements in list above is 10.

2. Finish the implementation of the sum function below. As the name implies, all elements in the linked list should be summed together. Return null if there are no elements in the list. Make sure to validate your code using the tests from (1).

LinkedList.prototype.sum = function() {

note: use 'this' to refer to the linked list, e.g., this.getHead()

};

Solutions

Expert Solution

// User defined class node 
class Node { 
    
    // constructor 
    constructor(element) { 
        this.element = element; 
        this.next = null
    } 
    
} 


// linkedlist class 
class LinkedList { 
    
    constructor() { 
        this.head = null; 
        this.size = 0; 
    } 

    // append element at the end of list 
    append(element) { 
        // creates a new node 
        var node = new Node(element); 
    
        // to store current node 
        var current; 
    
        // if list is Empty add the 
        // element and make it head 
        if (this.head == null) 
                this.head = node; 
        else { 
                current = this.head; 
    
                // iterate to the end of the list
                while (current.next) { 
                        current = current.next; 
                } 
    
                // add node 
                current.next = node; 
        } 
        this.size++; 
    } 

    
}


// Extending LinkedList with sum method
class LinkedLists extends LinkedList {
    sum() {
        // To store sum
        var s = 0;
        
        // To store current node
        var current = this.head;
        
        if (current == null) {
            return null;
        }
        
        while (current) {
            s += current.element;
            current = current.next;
        }
    
        return s;
    }
}

var list = new LinkedLists();

console.log(list.sum())         // Output: null
list.append(1);          
console.log(list.sum())         // Output: 1
list.append(2);
console.log(list.sum())         // Output: 3
list.append(3);
console.log(list.sum())         // Output: 6
list.append(4);
console.log(list.sum())         // Output: 10

Go through this and let me know through comments if you have any doubts about this.

Please give a thumbs up if you are satisfied with the Answer.

Thank you!!!


Related Solutions

This all has to be in javascript Use the following variables and data structures to answer...
This all has to be in javascript Use the following variables and data structures to answer this question. var queue = new Queue(); var stack = new Stack(); 1. Enqueue the numbers 1, 2, 3, 4, and 5 in the queue. Then print the contents of the queue to the console. Note: queue and stack both have a print() method. 2. Dequeue each number from the queue and push it onto the stack. Then print the contents of the stack...
Please answer all the questions. Thank you Use the following data to answer the questions below:...
Please answer all the questions. Thank you Use the following data to answer the questions below: Column 1 Column 2 Column 3 Y in $ C in $ 0 500 500 850 1,000 1,200 1,500 1,550 2,000 1,900 2,500 2,250 3,000 2,600 What is mpc and mps? Compute mpc and mps. Assume investment equals $ 100, government spending equals $ 75, exports equal $ 50 and imports equal $ 35. Compute the aggregate expenditure in column 3. Draw a graph...
Answer all the list questions below. Provide reasoning for the identified correct answer. Also, provide reasoning...
Answer all the list questions below. Provide reasoning for the identified correct answer. Also, provide reasoning for the invalid answers. I will thumb you down if you do not follow the above instructions completely -------------------------------------------------------------------------------------------------------------------- 31) Which of the following genes does not encode a transcription factor? a)c- myc b) RAR alpha c) ras d) p43 --------------------------------------------------------- 32) A relatively small circular DNA molecule that encodes oncogenes and is present in many types of cancer is called a _____. a)...
Answer all the list questions below. Provide reasoning for the identified correct answer. Also, provide reasoning...
Answer all the list questions below. Provide reasoning for the identified correct answer. Also, provide reasoning for the invalid answers. I will thumb you down if you do not follow the above instructions completely -------------------------------------------------------------------------------------------------------------------- 25) Which of the following is false regarding mis-match repair? a) It is initiated by specific proteins that function in sequential steps, not as a singular large complex b) It is the same mechanism as recombination repair c) It involves DNA ligase d) It involves...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2)     struct node    { int item; node* next;            node(int x, node* t)          { item = x; next = t; }     };     typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2) struct node { int item; node* next; node(int x, node* t) { item = x; next = t; } }; typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with the following change in order...
Use the following information to answer the questions below:
Use the following information to answer the questions below: note: all sales are credit sales Income Stmt info: 2016 2017 Sales $ 975,000 $        1,072,500 less Cost of Goods Sold: 325,000 346,125 Gross Profit 650,000 726,375 Operating Expenses 575,000 609,500 Earnings before Interest & Taxes 75,000 116,875 Interest exp 25,000 31,000 earnings before Taxes 50,000 85,875 Taxes 20,000 34,350 Net Income $ 30,000 $              51,525 Balance Sheet info: 12/31/2016 12/31/2017 Cash 60,000 $ 63,600 Accounts Receivable 80,000 $ 84,000 Inventory...
Use the following data to answer the questions below:
Use the following data to answer the questions below:             Q                 VC             MC           AVC              1                 $10              ___            ___                                   2                16              ___            ___                       3                20              ___            ___                                   4                25              ___            ___              5                31              ___            ___              6                  38              ___            ___                  7                  46              ___            ___8                  55              ___            ___9                  65              ___            ___         a. Calculate the marginal cost and average variable cost for each level of production.b. How much would the firm produce if it could sell its product for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT