mhdew

Sum

Mar 3rd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct NODE
  5. {
  6.     int data;
  7.     struct NODE *next;
  8. } node;
  9.  
  10. node *head;
  11.  
  12. void sum()
  13. {
  14.     node *pointer=head;
  15.     int sum=0;
  16.     while(pointer!=NULL){
  17.         sum+=pointer->data;
  18.         pointer=pointer->next;
  19.     }
  20.     printf("%d\n", sum);
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26.     head=(node *)malloc(sizeof(node));
  27.     head->data=3;
  28.     head->next=(node *)malloc(sizeof(node));
  29.     head->next->data=6;
  30.     head->next->next=(node *)malloc(sizeof(node));
  31.     head->next->next->data=2;
  32.     head->next->next->next=(node *)malloc(sizeof(node));
  33.     head->next->next->next->data=7;
  34.     head->next->next->next->next=NULL;
  35.  
  36.     sum();
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment