0

I am implementing a Queue using two queues and It is outputting garbage value and not the required value. I tried getting rid of pointer all together and passing address of the stack but it still outputting the same garbage value.

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

struct stack
{
    int size;
    int top;
    int *arr;
};

int isfull(struct stack *s)
{
    if (s->top == (s->size) - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void Enqueue(struct stack *a, int data)
{
    if (isfull(a))
    {
        printf("Overflow");
    }
    else
    {

        a->top++;
        a->arr[a->top] = data;
    }
}

int isempty(struct stack *s)
{
    if (s->top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int pop(struct stack *a)
{
    if (isempty(a))
    {
        printf("Underflow");
    }
    else
    {
        int x = a->arr[a->top];
        a->top--;
        return x;
    }
}

int Dequeue(struct stack *a)
{
    struct stack *b=(struct stack*)malloc(sizeof(struct stack));
    b->size = 33;
    b->top = -1;
    b->arr = (int *)malloc(b->size * sizeof(int));
    if (isempty(a))
    {
        printf("Underflow");
    }
    else
    {
        while (!isempty(a))
        {
            Enqueue(b, pop(a));
        }
        int x = pop(b);
        while (!isempty(b))
        {
            Enqueue(a, pop(b));
        }
        return x;
    }
}

int main()
{
    struct stack *a=(struct stack*)malloc(sizeof(struct stack));
    a->size = 33;
    a->top = -1;
    a->arr = (int *)malloc(a->size * sizeof(int));

    Enqueue(a, 0);
    Enqueue(a, 1);
    Enqueue(a, 2);

    printf("The element removed is %d\n", Dequeue(a));

    return 0;
}

Can You explain why its not showing the correct value.Is there a way to get rid of this issue while using the same code formal.

Yash Narang
  • 3
  • 1
  • 2
  • You have the same problem for `a` and `b`. This line `struct stack *b;` only defines a pointer variable. It is not initialized and does not point to any valid memory location. Reading content of an uninitialized variable causes undefined behaviour. Dereferencing an invalid memory address also causes undefined behaviour. You must allocate some memore where your pointers will point to. – Gerhardh Sep 01 '22 at 05:59
  • Some general notes: You never check any pointer for `NULL`. You should do that. You should also enable all compiler warnings. For GCC use `-Wall -Wextra -pedantic`. It should tell you about using `b` without assigning a value to it first. It should also tell you that you are missing return statements in non-void functions. You should fix all warnings before you run your program. Otherwise you will face problems the compiler already told you about. – Gerhardh Sep 01 '22 at 06:04
  • Do You mean like this" struct stack *a=(struct stack *)malloc(sizeof(struct stack));" – Yash Narang Sep 01 '22 at 06:09
  • Yes, but without the cast. Casting return value of `malloc` is not required and discouraged in C – Gerhardh Sep 01 '22 at 07:07
  • [Turn up your warnings and treat them as errors](https://godbolt.org/z/WboxKncY8) – WhozCraig Sep 01 '22 at 07:12

0 Answers0