sum of the linked list

Reply

Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

sum of the linked list

 
0
  #1
Oct 2nd, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sum of the linked list

 
0
  #2
Oct 2nd, 2006
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;

}
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: sum of the linked list

 
0
  #3
Oct 2nd, 2006
Originally Posted by Ancient Dragon View Post
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sum of the linked list

 
0
  #4
Oct 2nd, 2006
almost there -- the else and return 0 should not be inside the loop. And move p=p->next; to the bottom of the loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: sum of the linked list

 
0
  #5
Oct 2nd, 2006
Originally Posted by Ancient Dragon View Post
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;
 
}
Originally Posted by vbcielle View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sum of the linked list

 
0
  #6
Oct 2nd, 2006
Originally Posted by vbcielle View Post
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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: sum of the linked list

 
0
  #7
Oct 3rd, 2006
Originally Posted by Ancient Dragon View Post
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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC