Advertisement
shopnilSS

random

Mar 9th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int head = -1 , tail = -1 , array[3] , size = 3; //declare head and tail as -1 and took a array with size 3
  5. //ISFULL
  6. bool isEmpty(){
  7. if(head == -1 && tail == -1){
  8. return true;
  9. } //check the condition is it empty??
  10. return false;
  11. }
  12.  
  13. //ISEMPTY
  14. /*bool isFull(){
  15. if(tail > size - 1){
  16. return true;
  17. }//check the condition is it full??
  18. return false;
  19. }*/
  20. bool isFull(){
  21. if(tail >= size - 1){
  22. return true;
  23. }//check the condition is it full??
  24. return false;
  25. }
  26.  
  27. //ENQUEUE
  28. void enqueue(int val){
  29. head = 0;
  30. if(isFull() == false){
  31. tail++;
  32. array[tail] = val; //store the parameter value into the tail index
  33.  
  34. }else{
  35. cout << "Array is full" << endl;
  36. }//check the condition and add a value into the tail section
  37.  
  38. }
  39.  
  40.  
  41. //DEQUEUE
  42. void dequere(){
  43. if(isEmpty()){
  44. return;
  45. }else{
  46. if(tail == 0 && head == 0){
  47. head++;//when we use only one data.
  48. }else{
  49. array[head];
  50. head++;
  51. }
  52. }//check the condition and remove the head data or first data from the queue
  53. }
  54.  
  55. //PEEK
  56. int peek(){
  57. if(!(isEmpty())){
  58. return array[head];//return the peek data from the head section.
  59. }
  60. return -1;//return -1 if the array is full.
  61. }
  62.  
  63.  
  64. //Main function
  65. int main(){
  66. peek();
  67. enqueue(100);
  68. enqueue(200);
  69. enqueue(300);
  70. enqueue(300);
  71. //dequere();
  72.  
  73. cout << peek();
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement