Advertisement
Saykot

LAB

Feb 22nd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct data
  4. {
  5.     int a;
  6.     struct data *next,*prev;
  7. } data;
  8. data *head=NULL;
  9. data *tail=NULL;
  10.  
  11. void enQueue(int x)
  12. {
  13.     data *node=(data*)malloc(sizeof(data));
  14.     node->a=x;
  15.     node->prev=NULL;
  16.     node->next=NULL;
  17.     if(head==NULL)
  18.     {
  19.         head=node;
  20.         tail=node;
  21.         return;
  22.     }
  23.     tail->next=node;
  24.     node->prev=tail;
  25.     tail=node;
  26.     return;
  27. }
  28.  
  29. int deQueue()
  30. {
  31.     int x;
  32.     data *temp=head;
  33.     if(head==NULL)
  34.     {
  35.         printf("NO DATA FOUND/n");
  36.         return -1;
  37.     }
  38.     else if(temp->next==NULL)
  39.     {
  40.         x=temp->a;
  41.         free(temp);
  42.         return x;
  43.     }
  44.     head=temp->next;
  45.     x=temp->a;
  46.     free(temp);
  47.     return x;
  48.  
  49. }
  50. void print()
  51. {
  52.     data *temp=head;
  53.     while(temp!=NULL)
  54.     {
  55.         printf("%4d",temp->a);
  56.         temp=temp->next;
  57.     }
  58.     printf("\n");
  59.     return;
  60. }
  61. int main()
  62. {
  63.     int i,a1,a2,a3,a4,x,count=0,sum=0,ans;
  64.     scanf("%d",&a1);
  65.     for(i=0; i<a1; i++)
  66.     {
  67.         scanf("%d",&a3);
  68.         enQueue(a3);
  69.     }
  70.  
  71.     scanf("%d",&a4);
  72.     if(a1<a4)
  73.     {
  74.         for(i=0; i<a1; i++)
  75.         {
  76.             count++;
  77.             x=deQueue();
  78.             sum+=x;
  79.         }
  80.  
  81.         ans=sum/count;
  82.         printf("Average :%5d\n",ans);
  83.         return 0;
  84.  
  85.     }
  86.     for(i=0; i<a4; i++)
  87.     {
  88.         count++;
  89.         x=deQueue();
  90.         sum+=x;
  91.     }
  92.  
  93.     print();
  94.  
  95.     ans=sum/count;
  96.     printf("Average :%5d\n",ans);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement