Advertisement
Zahid_hasan

circular queue

Mar 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #define n 5
  3. int queue[n];
  4. int front =-1;
  5. int rear =-1;
  6. void insert();
  7. void delete();
  8. void display();
  9. void peek();
  10.  
  11. int main()
  12. {
  13. int choice;
  14. while(1)
  15. {
  16. printf("\t\t\t1.Insert");
  17. printf("\n\t\t\t2.Delete");
  18. printf("\n\t\t\t3.Display");
  19. printf("\n\t\t\t4.Top");
  20. printf("\n\t\t\t5.Quit");
  21. printf("\nEnter your choice: ");
  22. scanf("%d",&choice);
  23. switch(choice)
  24. {
  25. case 1: insert();
  26. break;
  27. case 2: delete();
  28. break;
  29. case 3: display();
  30. break;
  31. case 4: peek();
  32. break;
  33. case 5: exit(1);
  34. break;
  35. default: printf("error\n");
  36. }
  37. fflush(stdin);
  38. }
  39. }
  40. void insert()
  41. {
  42. int x;
  43. printf("Enter the value to insert: ");
  44. scanf("%d",&x);
  45. if(front==-1 && rear==-1)
  46. {
  47. front=rear=0;
  48. queue[rear]=x;
  49. }
  50. else if(((rear+1)%n)== front)
  51. {
  52. printf("Queue is full.Overflow");
  53. }
  54. else
  55. {
  56. rear =(rear+1)%n;
  57. queue[rear]=x;
  58. }
  59. }
  60. void delete()
  61. {
  62. if(front==-1 && rear==-1)
  63. {
  64. printf("Underflow");
  65. }
  66. else if(front == rear)
  67. {
  68. front=rear=-1;
  69.  
  70. }
  71. else
  72. {
  73. printf("Dequeued:%d\n",queue[front]);
  74. front=(front+1)%n;
  75. }
  76. }
  77. void display()
  78. {
  79. int i=front;
  80. if(front==-1 && rear==-1)
  81. {
  82. printf("Queue is empty");
  83. }
  84. else
  85. {
  86. printf("\n\nQueue elements are\n");
  87. while(i!=rear)
  88. {
  89. printf("%d\t",queue[i]);
  90. i=(i+1)%n;
  91. }
  92. printf("%d",queue[rear]);
  93. }
  94. }
  95. void peek()
  96. {
  97. if(front==-1 && rear==-1)
  98. {
  99. printf("it is empty\n");
  100. }
  101. else
  102. {
  103. printf("\n\nPeek element:%d\n",queue[front]);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement