943,781 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2278
  • C RSS
Oct 2nd, 2006
0

sum of the linked list

Expand Post »
i want to add all the inserted linked list by using this code

but what happens here if i input 15 then 35 =50 but when i input again 30 the sum is 50whats wrong with my code?

  1.  
  2. int sum(nd **head){
  3. nd *p,*sum1;
  4. p=*head;
  5. sum1=0;
  6. while(p!=NULL)
  7. {
  8. sum1->x=p->x;
  9. p=p->next;
  10. return sum1->x=sum1->x+p->x;
  11. }
  12.  
  13. }
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: sum of the linked list

why is sum a linked list and not a simple integer?
int sum(nd **head){
  nd *pl
  int sum1 = 0;
  p=*head;
  while(p!=NULL)
  {
    sum1  += p->x;
    p=p->next;
  }
    return sum1;

}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 2nd, 2006
0

Re: sum of the linked list

why is sum a linked list and not a simple integer?
int sum(nd **head){
  nd *pl
  int sum1 = 0;
  p=*head;
  while(p!=NULL)
  {
    sum1  += p->x;
    p=p->next;
  }
    return sum1;
 
}
actualy i dont know how to di this ...i tired you code it works

how about this one where i want to locate if the linked list is in the lis?
  1.  
  2. int locate(nd **head,int num){
  3. nd *p;
  4. p=*head;
  5. while(p!=NULL)
  6. {
  7. p=p->next;
  8. if(num==p->x)
  9. return 1;
  10. else
  11. return 0;
  12. }
  13. }
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: sum of the linked list

almost there -- the else and return 0 should not be inside the loop. And move p=p->next; to the bottom of the loop.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 2nd, 2006
0

Re: sum of the linked list

why is sum a linked list and not a simple integer?
int sum(nd **head){
  nd *pl
  int sum1 = 0;
  p=*head;
  while(p!=NULL)
  {
    sum1  += p->x;
    p=p->next;
  }
    return sum1;
 
}
Click to Expand / Collapse  Quote originally posted by vbcielle ...
actualy i dont know how to di this ...i tired you code it works

how about this one where i want to locate if the linked list is in the lis?
  1.  
  2. int locate(nd **head,int num){
  3. nd *p;
  4. p=*head;
  5. while(p!=NULL)
  6. {
  7. p=p->next;
  8. if(num==p->x)
  9. return 1;
  10. else
  11. return 0;
  12. }
  13. }
int locate(nd **head,int num){
nd *p;
p=*head;
while(p!=NULL)
{
p=p->next;
}
if(num==p->x)
return 1;
else
return 0;
}

the code above always return to xero
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: sum of the linked list

Click to Expand / Collapse  Quote originally posted by vbcielle ...
int locate(nd **head,int num){
nd *p;
p=*head;
while(p!=NULL)
{
p=p->next;
}
if(num==p->x)
return 1;
else
return 0;
}

the code above always return to xero
Look and think about the logic in the code you posted, pay attention to the while loop. All it does is iterate through the linked list without stopping. The if statement needs to be inside the loop -- I did not tell you to move it outside the loop. The else part shoud not be there at all.

  1. int locate(nd **head,int num){
  2. nd *p;
  3. p=*head;
  4. while(p!=NULL)
  5. {
  6. p=p->next;
  7. if(num==p->x)
  8. return 1;
  9. }
  10. return 0;
  11. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 3rd, 2006
0

Re: sum of the linked list

Look and think about the logic in the code you posted, pay attention to the while loop. All it does is iterate through the linked list without stopping. The if statement needs to be inside the loop -- I did not tell you to move it outside the loop. The else part shoud not be there at all.

  1. int locate(nd **head,int num){
  2. nd *p;
  3. p=*head;
  4. while(p!=NULL)
  5. {
  6. p=p->next;
  7. if(num==p->x)
  8. return 1;
  9. }
  10. return 0;
  11. }

thankls it work :cheesy:
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: read text file
Next Thread in C Forum Timeline: Read from a csv file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC