Advertisement
hamaXD

lab 6- 1

Oct 31st, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct listnode {
  4.   int data;
  5.   struct listnode *next;
  6. };
  7.  
  8. typedef struct listnode LN;
  9.  
  10. void insert_at_front(LN **hptr, int d);
  11. void print(LN *head);
  12. int sum(LN *head);
  13.  
  14. int main(){
  15.   LN *head;
  16.   head = NULL; // WHY must have it?
  17.   int d;
  18.   printf("Enter data: ");
  19.   do{
  20.     scanf("%d", &d);
  21.     if(d > 0){
  22.         insert_at_front(&head,d);
  23.     }
  24.   }while(d > 0);
  25.     printf("=");
  26.     print(head);
  27.     printf("\n=%d", sum(head));
  28.   return 0;
  29. }
  30.  
  31. void insert_at_front(LN **hptr, int d){
  32.  
  33.     LN* temp = (LN*)malloc(sizeof(LN));
  34.     temp->data = d;
  35.     temp->next= *hptr;
  36.     *hptr = temp;
  37. }
  38.  
  39. void print(LN *head){
  40.     while(head !=NULL){
  41.         printf("%d ",head->data);
  42.         head = head->next;
  43.     }
  44. }
  45.  
  46. int sum(LN *head){
  47.     int temp=0;
  48.     while(head !=NULL){
  49.         temp+=head->data;
  50.         head = head->next;
  51.     }
  52.     return temp;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement