Question

In: Computer Science

using java Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24,...

using java

Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24, 1. Assume that the Node.value is an int, and the reference to the first value is a Node called n. Write a method that computes the sum of all the values in the nodes of a linked list. For example, your method shall return the sum of all the nodes, in this example shall return 45

Solutions

Expert Solution

Below is code for method which returns sum of all nodes in java.

   public int sumOfAllNodes() {
        Node cur = head;
        int sum = 0;
        while(cur != null) 
        {  
            sum = sum + cur.value;
            cur = cur.next;
        }
        return sum;
    }

Below is whole code.

public class Main {
    static class Node {
        int value;
        Node next;

        public Node(int a) {
            value = a;
            next = null;
        }
    }

    Node head; // tail

    public Main() {
        head = null; 
    }

    public void insertFirst(int a) {
        Node newNode = new Node(a);
        newNode.next = head;
        head = newNode;
        // if (tail == null) tail = newNode;
    }


    
    public int sumOfAllNodes() {
        Node cur = head;
        int sum = 0;
        while(cur != null) 
        {  
            sum = sum + cur.value;
            cur = cur.next;
        }
        return sum;
    }

public String toString() {
        if (head == null) return "The list is empty.";
        StringBuilder sb = new StringBuilder();
        sb.append(head.value);
        Node cur = head.next;
        while ( cur != null ) {
            sb.append(" -> " + cur.value);
            cur = cur.next;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Main list = new Main();
        list.insertFirst(1);
        list.insertFirst(24);
        list.insertFirst(7);
        list.insertFirst(13);
        System.out.println(list);
        int sum = list.sumOfAllNodes();
        System.out.println(sum);

    }
}

Output:


Related Solutions

Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the...
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the LinkedList. 2. (2 pts) Create the LinkedList using the ListNode class. 3. (4 pts) Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. 4. (2 pts) Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value...
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
13-32 (Objectives 13-4, 13-6, 13-7) The following are parts of a typical audit for a company...
13-32 (Objectives 13-4, 13-6, 13-7) The following are parts of a typical audit for a company with a fiscal year-end of July 31. Understand internal control and assess control risk. Perform substantive analytical procedures for accounts payable. Confirm accounts payable. Perform tests of controls and substantive tests of transactions for the acquisition and payment and payroll and personnel cycles. Perform other tests of details of balances for accounts payable. Perform tests for review of subsequent events. Accept the client. Issue...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides for lecture 8). Add the following methods to the class and submit the completed LinkedList class. int size() which returns the size of the linked list Link getElementByIndex(int index) which returns the Link/node specified by the index. For example, getElementByIndex(0) should return the first Link, getElementByIndex(2) should return the third Link. If index is not in range, your method should return null. boolean hasDuplicate()...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
Consider the data. xi 2 6 9 13 20 yi 7 19 8 24 22 (a)...
Consider the data. xi 2 6 9 13 20 yi 7 19 8 24 22 (a) What is the value of the standard error of the estimate? (Round your answer to three decimal places.) (b) Test for a significant relationship by using the t test. Use α = 0.05. State the null and alternative hypotheses. H0: β1 ≠ 0 Ha: β1 = 0 H0: β1 ≥ 0 Ha: β1 < 0     H0: β1 = 0 Ha: β1 ≠ 0 H0:...
Consider the data. xi 2 6 9 13 20 yi 7 17 10 28 24 (a)...
Consider the data. xi 2 6 9 13 20 yi 7 17 10 28 24 (a) What is the value of the standard error of the estimate? (Round your answer to three decimal places.) _________ (b) Test for a significant relationship by using the t test. Use α = 0.05. State the null and alternative hypotheses. H0: β0 = 0 Ha: β0 ≠ 0 H0: β1 = 0 Ha: β1 ≠ 0     H0: β0 ≠ 0 Ha: β0 = 0...
Consider the following sum (which is in expanded form): 1−4 + 7−10 + 13−16 + 19−22...
Consider the following sum (which is in expanded form): 1−4 + 7−10 + 13−16 + 19−22 +···±(3n−2). Note that this is slightly different from the previous sum in that every other term is negative. (a) Write it as a summation (∑). (b) Evaluate the sum for every integer n from 1 to 9. (Be careful - if you get this wrong, you will likely get the rest of this question wrong!) (c) Write a closed-form formula for the value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT