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. int size=0;
  11.  
  12. public:
  13. Queue(int cap)
  14. {
  15. capacity=cap;
  16. frnt=-1;
  17. rear=-1;
  18. arr=new string[capacity] ;
  19. }
  20.  
  21. void enqueue(string val)
  22. {
  23. if(isfull())
  24. {
  25.  
  26. cout<< "queue is full"<<endl;
  27.  
  28. }
  29. else{
  30. rear=(rear+1)%capacity;
  31. arr[rear]=val;
  32. if(frnt==-1) frnt=0;
  33. }
  34. size++;
  35. }
  36.  
  37. void dequeue()
  38. {
  39. if( isempty())
  40. {
  41. cout<< "queue is empty"<<endl;
  42. }
  43. else{
  44.  
  45. frnt=(frnt+1)%capacity;
  46. size--;
  47. }
  48.  
  49. }
  50. bool isfull()
  51. {
  52. if(size==capacity)
  53. return true;
  54. else
  55. return false;
  56.  
  57.  
  58. }
  59.  
  60. bool isempty()
  61. {
  62. if(size==0)
  63. return true;
  64. else
  65. return false;
  66. }
  67.  
  68. string getFront()
  69. {
  70. return arr[frnt];
  71. }
  72. int getsize()
  73. {
  74. return size;
  75. }
  76. void print()
  77. {
  78. if(isempty())
  79. {
  80. cout<< "queue is empty"<<endl;
  81. }
  82. int j=frnt;
  83. for(int i=0;i<size;i++)
  84. {
  85. cout<< arr[j]<< " ";
  86. j=(j+1)%capacity;
  87. }
  88. cout<<endl;
  89. }
  90. };
  91.  
  92. int main()
  93. {
  94. Queue q(10);
  95. q.enqueue("apple");
  96. q.enqueue("mango");
  97. q.enqueue("red");
  98. q.enqueue("green");
  99. q.enqueue("blue");
  100. q.enqueue("orange");
  101. q.print();
  102.  
  103. cout<<q.getsize()<<endl;
  104. q.dequeue();
  105. q.print();
  106. string s=q.getFront();
  107. cout<<s<<endl;
  108.  
  109. cout<<q.getsize()<<endl;
  110. return 0;
  111. }
  112.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
apple mango red green blue orange 
6
mango red green blue orange 
mango
5