Advertisement
lamia17

Queue

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