| | |
sum of the linked list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
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?
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?
C Syntax (Toggle Plain Text)
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; } }
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;
}•
•
Join Date: Sep 2006
Posts: 26
Reputation:
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; }

how about this one where i want to locate if the linked list is in the lis?
C Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
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; }
•
•
•
•
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?
C Syntax (Toggle Plain Text)
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; } }
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
•
•
•
•
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
C Syntax (Toggle Plain Text)
int locate(nd **head,int num){ nd *p; p=*head; while(p!=NULL) { p=p->next; if(num==p->x) return 1; } return 0; }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
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.
C Syntax (Toggle Plain Text)
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: ![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: read text file
- Next Thread: Read from a csv file
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






