954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sum of the linked list

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?

int sum(nd **head){
  nd *p,*sum1;
  p=*head;
  sum1=0;
  while(p!=NULL)
  {
    sum1->x=p->x;
    p=p->next;
    return sum1->x=sum1->x+p->x;
  }
 
}
vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

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;

}
Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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?

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;
  }
}
vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

almost there -- the else and return 0 should not be inside the loop. And move p=p->next; to the bottom of the loop.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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?

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;
  }
}



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

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 
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 andthink 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.

int locate(nd **head,int num){
  nd *p;
  p=*head;
  while(p!=NULL)
  {
    p=p->next;
    if(num==p->x)
        return 1;
  }
  return 0;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

int locate(nd **head,int num){
  nd *p;
  p=*head;
  while(p!=NULL)
  {
    p=p->next;
    if(num==p->x)
        return 1;
  }
  return 0;
}


thankls it work :):cheesy:

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You