JHJiban

Enqueue & Dequeue LTest

Feb 22nd, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 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, *tail=NULL;
  9.  
  10. void enQueue(int x)
  11. {
  12.     Data *new_node=(Data*)malloc(sizeof(Data));
  13.     new_node->a=x;
  14.     new_node->next=NULL;
  15.     new_node->prev=NULL;
  16.     if(head==NULL)
  17.     {
  18.         head=new_node;
  19.         tail=head;
  20.         return;
  21.     }
  22.     new_node->prev=tail;
  23.     tail->next=new_node;
  24.     tail=new_node;
  25. }
  26.  
  27. int deQueue()
  28. {
  29.     Data *dlt=head;
  30.     int x;
  31.     if(head==NULL)
  32.     {
  33.         printf("Empty!\n");
  34.         return -1;
  35.     }
  36.     else if(head->next==NULL)
  37.     {
  38.         head=NULL;
  39.         tail=NULL;
  40.         x = dlt -> a;
  41.         free(dlt);
  42.         return x;
  43.     }
  44.     head=head->next;
  45.     head->prev=NULL;
  46.     x = dlt -> a;
  47.     free(dlt);
  48.     return x;
  49. }
  50.  
  51. void Print()
  52. {
  53.     int x=0;
  54.     Data *tmp=head;
  55.     while(tmp!=NULL)
  56.     {
  57.         printf("%d. Enqueued Value: %d\n", ++x, tmp -> a);
  58.         tmp=tmp->next;
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     int i,n,DQ=0,max=0;
  65.     float deQ,avr,sum=0;
  66.     printf("Enter number of Enqueue: ");
  67.     scanf("%d",&n);
  68.     for(i=0;i<n;i++)
  69.     {
  70.         int vlu;
  71.         printf("\nEnter Enqueue Value %d: ",i+1);
  72.         scanf("%d",&vlu);
  73.         enQueue(vlu);
  74.     }
  75.     Print();
  76.     int j,m;
  77.     printf("\nEnter number of Dequeue: ");
  78.     scanf("%d",&m);
  79.     if(m>n)
  80.     {
  81.         for(j=0;j<n;j++)
  82.     {
  83.         int deQ;
  84.         deQ = deQueue();
  85.         sum+=deQ;
  86.         if(max<deQ)
  87.         {
  88.             max=deQ;
  89.         }
  90.     }
  91.     Print();
  92.     avr=sum/n;
  93.     printf("\nAverage of Dequeued  value: %.2f",avr);
  94.     printf("\nMax of Dequeued value: %d",max);
  95.  
  96.     return 0;
  97.     }
  98.     for(j=0;j<m;j++)
  99.     {
  100.         int deQ;
  101.         deQ = deQueue();
  102.         sum+=deQ;
  103.         if(max<deQ)
  104.         {
  105.             max=deQ;
  106.         }
  107.     }
  108.     Print();
  109.     avr=sum/m;
  110.     printf("\nAverage of Dequeued value: %.2f",avr);
  111.     printf("\nMax of Dequeued value: %d",max);
  112.  
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment