shopnilSS

AssighnmentTwo_1730034_ShahSadamenyYeasarShopnil

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