Advertisement
SAADQUAMER

queueSum

Dec 9th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 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 enqueue(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.  
  33. int dequeue()
  34. {
  35.     int data=0,n;
  36.     node*temp;
  37.     temp=head;
  38.     data=head->a;
  39.     head=head->next;
  40.     free(temp);
  41.     return data;
  42. }
  43. int main()
  44.  
  45. {
  46.     int n,x,i;
  47.     head=NULL;
  48.     printf("Input the Number of Node:");
  49.     scanf("%d",&n);
  50.     for(i=0; i<n; i++)
  51.     {
  52.         scanf("%d",&x);
  53.         enqueue(x);
  54.  
  55.     }
  56.     int sum=0,z,q,l;
  57.     for(l=0; l<n; l++)
  58.     {
  59.         q=dequeue();
  60.         sum=sum+q;
  61.  
  62.     }
  63.     printf("\nTotal SUM IS:%d",sum);
  64.  
  65.  
  66.  
  67.     return;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement