Full width home advertisement

Java

Tech News

Ad

  1. /*
  2. implemaintaction Of Circular Queue
  3. */
  4. #include<stdio.h>
  5. #define N 5
  6.  
  7. int queue[N];
  8. int front=-1,rear=-1;
  9. //Function Declaractions
  10. void Enqueue();
  11. void Dequeue();
  12. void Peek();
  13. void Display();
  14.  
  15. //Driver Program
  16. int main(){
  17. int choice;
  18. do{
  19. printf("\n***Circular Queue***\n");
  20. printf("1. Enqueue\n");
  21. printf("2. Dequeue\n");
  22. printf("3. Peek\n");
  23. printf("4. Display\n");
  24. printf("0 To Exit\n");
  25. printf("Enter Choice ");
  26. scanf("%d",&choice);
  27. switch(choice){
  28. case 1:
  29. Enqueue();
  30. break;
  31. case 2:
  32. Dequeue();
  33. break;
  34. case 3:
  35. Peek();
  36. break;
  37. case 4:
  38. Display();
  39. break;
  40. default:
  41. printf("\n!Wrong Choice!\n");
  42. }
  43. }while(choice!=0);
  44. return 0;
  45. }
  46.  
  47. void Enqueue(){
  48. int x;
  49. if(front==-1 && rear==-1){
  50. printf("\nEnter X ");
  51. scanf("%d",&x);
  52. front=rear=0;
  53. queue[rear]=x;
  54. }
  55. else if((rear+1)%N==front){
  56. printf("\nQueue Is Full\n");
  57. }
  58. else{
  59. printf("\nEnter X ");
  60. scanf("%d",&x);
  61. rear=(rear+1)%N;
  62. queue[rear]=x;
  63. }
  64. }
  65.  
  66. void Dequeue(){
  67. if(front==-1 && rear==-1){
  68. printf("\nQueue Is Empty\n");
  69. }
  70. else if(front==rear){
  71. front=rear=-1;
  72. }
  73. else{
  74. printf("%d",queue[front]);
  75. front=(front+1)%N;
  76. }
  77. }
  78.  
  79. void Peek(){
  80. if(front==-1 && rear==-1){
  81. printf("\nQueue Is Empty\n");
  82. }
  83. else{
  84. printf("%d ",queue[front]);
  85. }
  86. }
  87.  
  88. void Display(){
  89. int i=front;
  90. if(front==-1 && rear==-1){
  91. printf("\nQueue Is Empty\n");
  92. }
  93. else{
  94. while(i!=rear){
  95. printf("%d ",queue[i]);
  96. i=(i+1)%N;
  97. }
  98. printf("%d",queue[i]);
  99. }
  100. }
OUTPUT

Circular Queue Array




No comments:

Post a Comment

Bottom Ad [Post Page]