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
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
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
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
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
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341