Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct listnode {
- int data;
- struct listnode *next;
- };
- typedef struct listnode LN;
- void insert_at_front(LN **hptr, int d);
- void print(LN *head);
- int sum(LN *head);
- int main(){
- LN *head;
- head = NULL; // WHY must have it?
- int d;
- printf("Enter data: ");
- do{
- scanf("%d", &d);
- if(d > 0){
- insert_at_front(&head,d);
- }
- }while(d > 0);
- printf("=");
- print(head);
- printf("\n=%d", sum(head));
- return 0;
- }
- void insert_at_front(LN **hptr, int d){
- LN* temp = (LN*)malloc(sizeof(LN));
- temp->data = d;
- temp->next= *hptr;
- *hptr = temp;
- }
- void print(LN *head){
- while(head !=NULL){
- printf("%d ",head->data);
- head = head->next;
- }
- }
- int sum(LN *head){
- int temp=0;
- while(head !=NULL){
- temp+=head->data;
- head = head->next;
- }
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement