[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding up a linked list

Posted on 1998-02-11
2
Medium Priority
?
176 Views
Last Modified: 2010-04-10
I have a linked list and need to total it up. How would I go
about doing it ??  The best I came up with is:

struct List
{ int data;
  node * next;
}
typedef node* next;

cur= listhead* next;
     total = data;

while (cur!= listhead* next)
        total =+ data;
         
         cur = cur* next;

Thanks for the help.
0
Comment
Question by:chasa
2 Comments
 
LVL 1

Accepted Solution

by:
yl earned 450 total points
ID: 1181814
struct List
{
 int data;
 node * next;
};

int sum(struct List* listhead)
{
 struct List* cur = listhead;
 int total = 0;

 while (cur != NULL) {
   total += cur->data;
   cur = cur->next;
 }

 return (total);
}

0
 
LVL 3

Expert Comment

by:q2guo
ID: 1181815
the line total =+(head->data) should read
total += head->data
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question