fork download
  1. //Program to reverse queue but in O(n) and O(1) space
  2.  
  3. #include<conio.h>
  4. #include<stdio.h>
  5. #include<iostream.h>
  6. #define MAX 7
  7. int Q[MAX];
  8. int f = -1, r = -1;
  9. void display();
  10. int rev();
  11. int isempty();
  12. int isfull();
  13. int enq(int d);
  14. int deq();
  15. int main()
  16. {
  17. int i;
  18. for(i=1;i<=MAX;i++)
  19. enq(i);
  20. display();
  21. rev();
  22. display();
  23.  
  24.  
  25. getch();
  26. return 0;
  27. }
  28. int rev()
  29. {
  30. if(!isempty()) //if not empty
  31. {
  32. int temp = deq();
  33. rev();
  34. enq(temp);
  35. }
  36. else return 0;
  37.  
  38. }
  39. void display()
  40. {
  41. int i;
  42. cout<<"\n";
  43. for(i=f;i<=r;i++)
  44. cout<<" "<<Q[i];
  45.  
  46. }
  47. int isempty()
  48. {
  49. if((f==-1 && r==-1))
  50. return 1;
  51. else
  52. return 0;
  53.  
  54. }
  55. int isfull()
  56. {
  57. if(f==r+1 || (f==0 && r==MAX-1))
  58. return 1;
  59. else
  60. return 0;
  61.  
  62. }
  63. int enq(int d)
  64. {
  65. if(isfull())
  66. {
  67. cout<<"\n Cannot insert already full - ";
  68. return 0;
  69. }
  70. //decide new position of r
  71. if(f==r && r==-1)
  72. {r=0;f=0;} //only 1 element
  73. else if(r==MAX-1)
  74. r=0;
  75. else
  76. r=r+1;
  77. Q[r] = d;
  78. return 1;
  79.  
  80. }
  81. int deq()
  82. {
  83. if(isempty())
  84. {
  85. cout<<"\n Already empty - ";
  86. return 0;
  87. }
  88. int temp = Q[f];
  89. //now decide new postion of f
  90. if(f==r) //only one element was der
  91. {
  92. f = r = -1; //empty the queue
  93. }
  94. else if (f==MAX-1)
  95. f=0;
  96. else
  97. f+=1; //increment front
  98.  
  99. return temp;
  100. }
  101.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:18: error: conio.h: No such file or directory
prog.cpp:5:21: error: iostream.h: No such file or directory
prog.cpp: In function ‘int main()’:
prog.cpp:25: error: ‘getch’ was not declared in this scope
prog.cpp: In function ‘void display()’:
prog.cpp:42: error: ‘cout’ was not declared in this scope
prog.cpp: In function ‘int enq(int)’:
prog.cpp:67: error: ‘cout’ was not declared in this scope
prog.cpp: In function ‘int deq()’:
prog.cpp:85: error: ‘cout’ was not declared in this scope
stdout
Standard output is empty