Home > Software design >  why return arr[front]; is not working or showing me in output
why return arr[front]; is not working or showing me in output

Time:01-09

i am facing an issue in code of queue , i wrote code for queue but when i want to get the output of the code for my peek function nothing shows up in my OUTPUT section here is the code .

#include <iostream>
using namespace std;
class queue {
  int *arr;
  int front;
  int back;

public:
  int n = 10;
  queue() {
    arr = new int[n];
    front = -1;
    back = -1;
  }
  void push(int x) {
    if (back == n - 1) {
      cout << "queue over flow" << endl;
      return;
    }
    back  ;
    arr[back] = x;
    if (front == -1) {
      front  ;
    }
  }
  void pop() {
    if (front == -1 || front > back) {
      cout << "no element or queue underflow " << endl;
    }
    front  ;
  }
  int peek() {
    if (front == -1 || front > back) {
      cout << "no element or queue underflow " << endl;
      return -1;
    }
    return arr[front];
  }
  bool empty() {
    if (front == -1 || front > back) {
      cout << "no element or queue underflow " << endl;
      return true;
    }
    return false;
  }
};
int main() {
  queue a;

  a.push(5);
  a.peek();
}

CodePudding user response:

You nowhere generate any output from a.peek(), so this is as expected. Your computer only does what you tell it to do - generate an output exactly when you tell it to!

CodePudding user response:

i wrote code for queue but when i want to get the output of the code for my peek function nothing shows up in my OUTPUT section

peek has output only when front == -1 || front > back is true. Since it isn't true, there is no output.

  •  Tags:  
  • Related