Solved

Adding up a linked list

Posted on 1998-02-11
2
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 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