fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Queue
  5. {
  6. private:
  7. string *arr;
  8. int frnt,rear;
  9. int capacity;
  10.  
  11. public:
  12. Queue(int cap)
  13. {
  14. capacity=cap;
  15. frnt=-1;
  16. rear=-1;
  17. arr=new string[capacity] ;
  18. }
  19.  
  20. void enqueue(string val)
  21. {
  22. if(isfull())
  23. {
  24.  
  25. cout<< "queue is full"<<endl;
  26.  
  27. }
  28. rear=rear+1;
  29. arr[rear]=val;
  30. if(frnt==-1) frnt=0;
  31. }
  32.  
  33. void dequeue()
  34. {
  35. if( isempty())
  36. {
  37. cout<< "queue is empty"<<endl;
  38. }
  39.  
  40. frnt=frnt+1;
  41.  
  42. }
  43. bool isfull()
  44. {
  45. if(rear==capacity-1)
  46. return true;
  47. else
  48. return false;
  49.  
  50.  
  51. }
  52.  
  53. bool isempty()
  54. {
  55. if((rear==-1 && frnt==-1) || frnt>rear)
  56. return true;
  57. else
  58. return false;
  59. }
  60.  
  61. string getFront()
  62. {
  63. return arr[frnt];
  64. }
  65. int getsize()
  66. {
  67. return rear-frnt+1;
  68. }
  69. void print()
  70. {
  71. if(isempty())
  72. {
  73. cout<< "queue is empty"<<endl;
  74. }
  75. for(int i=frnt;i<=rear;i++)
  76. {
  77. cout<<arr[i]<< " "<<endl;
  78. }
  79. }
  80. };
  81.  
  82. int main()
  83. {
  84. Queue q(10);
  85. q.enqueue("apple");
  86. q.enqueue("mango");
  87. q.enqueue("red");
  88. q.print();
  89. q.dequeue();
  90. q.print();
  91. string s=q.getFront();
  92. cout<<s<<endl;
  93. int sz=q.getsize();
  94. cout<<sz<<endl;
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
apple 
mango 
red 
mango 
red 
mango
2