Solved

Adding up a linked list

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

20 Experts available now in Live!

Get 1:1 Help Now