Advertisement
SAADQUAMER

SUM

Nov 11th, 2019
127
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. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int a;
  7.     struct node *next;
  8.  
  9. } node;
  10.  
  11. node *head;
  12. void insert_end(int aN)
  13. {
  14.     node *N=(node*)malloc(sizeof(node));
  15.     N->a=aN;
  16.     N->next=NULL;
  17.     if(head==NULL)
  18.     {
  19.         head=N;
  20.     }
  21.     else
  22.     {
  23.         node *list=head;
  24.         while(list->next!=NULL)
  25.         {
  26.             list=list->next;
  27.         }
  28.         list->next=N;
  29.     }
  30. }
  31.  
  32. void display()
  33. {   int sum=0,x;
  34.     node*list=head;
  35.     while(list!=NULL)
  36.     {   x=list->a;
  37.         sum=sum+x;
  38.         list=list->next;
  39.     }
  40.     printf("SUM IS: %d\n",sum);
  41. }
  42. int main()
  43.  
  44. {
  45.     int n,x,i;
  46.     head=NULL;
  47.     printf("Input the Number of Node:");
  48.     scanf("%d",&n);
  49.     for(i=0; i<n; i++)
  50.     {
  51.         scanf("%d",&x);
  52.         insert_end(x);
  53.  
  54.     }
  55.     display();
  56.  
  57.     return;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement