fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Queue
  4. {
  5. int *a,rear,fro,cap;
  6. public:
  7. Queue()
  8. {
  9. rear=fro=-1;
  10. }
  11.  
  12. void create(int siz)
  13. {
  14. cap=siz;
  15. a=new int[siz];
  16. // return *this;
  17. }
  18. bool isempty()
  19. {
  20. if(fro==-1)
  21. return 1;
  22. return 0;
  23. }
  24. bool isfull()
  25. {
  26. if(rear%(cap)==fro-1 || (fro==0 && rear==cap-1))
  27. return 1;
  28. return 0;
  29. }
  30. void enqueue(int x)
  31. {
  32. if(isempty())
  33. {
  34. a[++rear]=x;
  35. fro++;
  36. // return *this;
  37. }
  38. else if(isfull())
  39. {
  40. cout<<"\n Queue is Full\n";
  41. // return *this;
  42. }
  43. else
  44. a[++rear%(cap)]=x;
  45. //return *this;
  46. }
  47. void dequeue()
  48. {
  49. if(isempty())
  50. {
  51. cout<<"\nQueue is empty\n";
  52. // return *this;
  53. }
  54. else{
  55. cout<<"\n dealeted element is "<<a[fro++]<<endl;
  56. }
  57. //return *this;
  58. }
  59. void display()
  60. {
  61. for(int i=fro;i<=rear%(cap);i++)
  62. cout<<a[i]<<endl;
  63. }
  64.  
  65. };
  66. int main()
  67. {
  68. Queue q;
  69. q.create(5);
  70. q.enqueue(5);
  71. q.enqueue(2);
  72. q.dequeue();
  73. q.enqueue(7);
  74. q.enqueue(1);
  75. q.enqueue(10);
  76. q.display();
  77. q.dequeue();q.display();
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
 dealeted element is 5
2
7
1
10

 dealeted element is 2
7
1
10