Question

how can I find the sum of data that are in the list using an ARRAY...

how can I find the sum of data that are in the list using an ARRAY of linked list . I just need a small example please. I'm not sure how to move from head[0] to head[1]....???

// This is the struct that I am working on:

struct node

{

int data;

node * next;

}

class List

{

public:

list();

~list();

private:

node ** head;

int size;

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int sum() {
    int s = 0;

    // move one by one on all the indices of array
    // which means one by one take a horizontal linked list
    for(int i=0; i<size; i++) {
        // find the sum of list at index i
        node *start = head[i];

        while(start != NULL) {
            s += start->data;
            start = start->next; // move to next node in list
        }
    }
    return s;
}
Add a comment
Know the answer?
Add Answer to:
how can I find the sum of data that are in the list using an ARRAY...
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
  • Write a recursive function in C++ that displays all nodes data (integers) in an array of...

    Write a recursive function in C++ that displays all nodes data (integers) in an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this is determined by another part of the program) }

  • If i have a node list type string, how do i put the data from each...

    If i have a node list type string, how do i put the data from each node into a string array and print the results? I am creating the node list myself so i just need generic code how to print the data from each node into a String array. 1670 * 168 * Returns an array of the words in the list, in the order that the client * would expect. * * 169 170 171 172 * *...

  • (C++) compute and return the sum of the ints contained in the Array table using RECURSION....

    (C++) compute and return the sum of the ints contained in the Array table using RECURSION. Start with the following function. int sum(node * head[]);    // // //This is the provided header file. //arr.h #include #include #include const int SIZE = 5;   //size of the array of head pointers struct node { int data; node * next; }; /* These functions are already written and can be called to test out your code */ void build(node * head[]); //supplied...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignmet you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

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
ADVERTISEMENT