1

I am writing a Queue data structure and I am not able to retain the value of the integer in the array once the value is returned in the stack. The pop function is doing exactly what it needs to do but why doesn't main get that information? What am I missing? malloc?

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

int QUE[20];
const int EMPTY = -1;
int position = -1;
int retrieve = 0;

//push, append to front of array
bool push(int num) {
    if (position >= 20) return false;
    position += 1;
    QUE[position] = num;
    return true;
}

//pop from top of array
bool pop() {

    if(QUE[retrieve] == 0) return false;
    int hold = QUE[retrieve];
    printf("%d",hold);
    retrieve ++;
    return hold;

}

// PEEK

// First in first out

int main() {
    push(12);
    push(90);
    push(22);

    int t;
    //why does pop equal 1
    while ((t = pop()) != 0) {
        printf("t = %d\n",t);

    }

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Colorful Codes
  • 577
  • 4
  • 16
  • 1
    true being interpreted as value 1. – Tony Tannous Jun 16 '20 at 16:46
  • Does this answer your question? [Does the C standard explicitly indicate truth value as 0 or 1?](https://stackoverflow.com/questions/37291681/does-the-c-standard-explicitly-indicate-truth-value-as-0-or-1) – Tony Tannous Jun 16 '20 at 16:47
  • You also have an off-by-one bug in `push()`: when `position == 19` on entry you will write to `QUE[20]` which is out of bounds. And you have no bounds checking on `retrieve` in `pop()`. – Nate Eldredge Jun 16 '20 at 16:48
  • I see thank you. Also, I am confused why this question was given a thumbs down. Anyone know? – Colorful Codes Jun 16 '20 at 16:58

3 Answers3

2

You're trying to pass two different kinds of information – a boolean state 'the pop succeeded' and an integer value popped from the queue – within the same value. That's bad; and the mismatch led you to declaring the return type as bool, which causes the resulting value of t being either zero or one (as a conversion of false or true, respectively, to the int type).

Try to split the action into testing and fetching phase, like:

bool anyItemInQueue()
{
    return _add_appropriate_condition_here_;
}

int main()
{
    ....

    while( anyItemInQueue() )
    {
        int t = pop();

        .... // use t here
    }
}

or pass another variable to receive another value:

bool pop(int *result)
{
    if( anyItemInQueue() )
    {
        *result = QUE[retrieve];
        ....                        // some housekeeping here
        return true;                // success status
    }
    return false;                   // failure status
}

int main()
{
    ....
    int t;
    while( pop( & t ) )      // point at t to receive the popped value
    {
        .... // use t here
    }
}
CiaPan
  • 9,381
  • 2
  • 21
  • 35
1

It is because any non zero value is being converted to bool true and then to integer. The integer value of the bool true is 1

0___________
  • 60,014
  • 4
  • 34
  • 74
1

Your code has undefined behavior.

Let's consider for example the function push

//push, append to front of array
bool push(int num) {
    if (position >= 20) return false;
    position += 1;
    QUE[position] = num;
    return true;
}

and let's also assume for simplicity that the array QUE has only one element that is it is declared like

int QUE[1];

In this case the queue can only contain one pushed value due to the capacity of the array.

So after a first call of push like

push( 0 );

you will have that position is equal to 0 and the queue contains the value 0.

If to call the function a second time like for example

push( 1 );

the condition within the function

if (position >= 1) return false;

will not evaluate to true because the current value of position is 0. As a result the function will try to write the value 1 into the invalid location of the array QUE[1].

The array contains only one element but the function allows to write one more element.

Now let's consider the function pop

bool pop() {

    if(QUE[retrieve] == 0) return false;
    int hold = QUE[retrieve];
    printf("%d",hold);
    retrieve ++;
    return hold;

}

and the same queue that already contains only one element equal to 0 (See the previous call push( 0 )).

As the condition of the if statement

if(QUE[retrieve] == 0) return false;

evaluates to true (the queue indeed contains the value 0 pushed on the queue early) then the function will return false as if the queue is empty though it is not empty.

So and this function is invalid.

Moreover in the loop in main

while ((t = pop()) != 0) {
    printf("t = %d\n",t);

}

it seems you are trying to output values stored in the queue. However the function does not return such values. Due to the return type bool that is a typedef for the C standard type _Bool any returned value is converted either to 0 or 1.

So the program is in whole wrong.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335