Question

Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the...

Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the linked list) that will return a sum of all values.

Homework Answers

Answer #1

Linked list is a data structure where we have 2 parts one wil store the data value and other will store the address of the next node to establish a link between the nodes

The data in the node can be accesed with arrow operator like node->data it will print the data inside the node .

int sumfun(struct Node* head)

{

    struct Node* temp = head;

    int sum = 0;

    while (temp != NULL) {

        sum += temp->data;

        temp = temp->next; // To increment the temp

    }

    return sum;

}

This is the required function

Know the answer?
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
Similar Questions
USE C++ Write a sum() function that is used to find the sum of the array...
USE C++ Write a sum() function that is used to find the sum of the array through pointers. In this program we make use of * operator. The * (asterisk) operator denotes the value of variable. Input: array = 2, 4, -6, 5, 8, -1 Output: sum = 12
Program is in C++ Write a function named “removeInvalidDate” that accepts the vector of pointers to...
Program is in C++ Write a function named “removeInvalidDate” that accepts the vector of pointers to Reminder objects. It will go through that list and delete the Reminder objects with an invalid date (day is not between 1 and 31 and month is not between 1 and 12) from the list. It will return how many objects that it has deleted from the list. Reminder class and objects are not given. This is all the question provides.
Python Question: Write a function which returns the sum of squares of the integers 1 to...
Python Question: Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0 You may not use the built-in Python sum function. The Solution Must be recursive >>> sum_of_squares(1) 1 >>> sum_of_squares(4) 30 >>> sum_of_squares(8) # 64 + 49 + 36 + ... +...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
(C++) Write a function called triple that takes an array of integers as a parameter as...
(C++) Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
C++ please! Write a recursive function to compute the sum of the Taylor series. use double...
C++ please! Write a recursive function to compute the sum of the Taylor series. use double taylor(int a, int n)
Use the Design Recipe to write a function count_evens_NxN,  that consumes a nested list representing a matrix...
Use the Design Recipe to write a function count_evens_NxN,  that consumes a nested list representing a matrix of size NxN. The function should return the number of even numbers in the matrix. For this function, 0 is considered an even number.  Include a Docstring! Note: You may assume the list argument passed to the function is a nested list of integers. Write 3 assert_equal statements to test your function.
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
How would I write this function in C programming language? Function header: int input(int *a, int...
How would I write this function in C programming language? Function header: int input(int *a, int *b, int *c) This function should attempt to input 3 integers from the keyboard and store them in the memory locations pointed to by a, b, and c. The input may not be successful in reading all three values. The function should return the number of values that were successfully read.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Emerald Co. uses a perpetual inventory system and records purchases of merchandise at net cost. The...
    asked 41 minutes ago
  • Movie stars and U.S. presidents have fished Pyramid Lake. It is one of the best places...
    asked 1 hour ago
  • what advantages do percent distributions have over frequency distributions when comparing populations at different years?
    asked 1 hour ago
  • Richard has just been given a 6-question multiple-choice quiz in his history class. Each question has...
    asked 1 hour ago
  • Write about westernized elements in life; westernization in our life, and this includes Americanization.
    asked 2 hours ago
  • The college Physical Education Department offered an Advanced First Aid course last summer. The scores on...
    asked 2 hours ago
  • Researchers hypothesized that increasing a woman's level of arousal would increase her perceptions of attractiveness of...
    asked 3 hours ago
  • Equation 37-14b in the textbook gives the energy emitted by Hydrogen when electrons transition between states...
    asked 3 hours ago
  • Theory of Computation Please provide explanation too on how it works a. Give an NFA recognizing...
    asked 3 hours ago
  • Question 1 A sequential pattern detection circuit (state machine) has input A and output Y, which...
    asked 3 hours ago
  • Natural Foods Inc. is planning to invest in new manufacturing equipment to make a new garden...
    asked 3 hours ago
  • Explain why a callable bond's price would be expected to decline less than an otherwise comparable...
    asked 3 hours ago