Solved

Adding up a linked list

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
gdb doesn't stop on breakpoint 2 68
c++, dynamic object by json 1 42
DCT of  2D array using fftw in c++ 9 41
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

810 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