0

This is my code

    int front=-1, rear=-1, CQUEUE[MAX];
    int isFull()
    {
      if((rear=MAX-1 && front==0) || front==rear+1)
        return 1;
      else
        return 0;
    }
    void enQueue()
    {
      printf("\nValue of rear=%d front=%d",rear,front);
      char ch;
      if(!isFull())
      {
        printf("\nValue of rear=%d front=%d",rear,front);
      ......

This is the output on calling the function enQueue()

Value of rear=-1 front=-1
Value of rear=0 front=-1

The value of rear is getting increased by 1 although I'm not increasing it manually.

ashchk
  • 161
  • 13
Sathvik K S
  • 109
  • 1
  • 2
  • 11
  • 5
    `rear=MAX-1` -> `rear==MAX-1` – kaylum Sep 28 '20 at 04:55
  • That almost always points to an instance where you are writing beyond the end of some array you have declared, or otherwise write more to a stack variable than it will hold causing the additional bytes to overwrite your other variable storage space giving the illusion that something magically changed -- it didn't, you just invoked *Undefined Behavior* and are seeing the result... (or in this case you mess up an `=` that should be an `==`). Enabling compiler warnings would have pointed that out for you. – David C. Rankin Sep 28 '20 at 04:56

2 Answers2

1

Look at your first if statement.

if (rear=MAX-1  ...)
// maybe better if you type:
if (rear==MAX-1 ...)
dunajski
  • 381
  • 3
  • 15
1

You are using the assignment operator = where you should be using the eq comparison operator == in isFull

int isFull()
{
  if((rear==MAX-1 && front==0) || front==rear+1)
    return 1;
  else
    return 0;
}