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.