Advertisement
AparnaDebnath

enque & dequeue

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