Solved

Adding up a linked list

Posted on 1998-02-11
2
157 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 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now