The problem is that I am unable to use the pop function.
int pop(int stack[],int *top,int item)
{
if(*top==-1) {
printf("Stack Underflow");
return 0;
}
return stack[(*top)--];
}
Here, if I use stack[*top--]
it doesn't seem to work! What is the difference? Why is the top variable in the main function is not getting decremented?
int main()
{
int stack[4], top = -1, item, id, ch;
for(;;) {
printf("Enter your choice:\n1.push\n2.pop\n3.Print top element\n4.Print all elements\n5.Exit\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the item to be pushed:\n");
scanf("%d",&item);
push(stack,&top,item);
break;
case 2:
id=pop(stack,&top,item);
printf("%d was popped\n",id);
break;
case 4:
print(stack,&top,item);
break;
case 5:
exit(0);
}
}
}
CodePudding user response:
return stack[(*top)--];
here if I use [*top--]
(*top)--
gets the object that top
points to and decrements that object. Because *top
points to the caller’s top
, that is the value you want to use to look up an array element. Then --
decrements the caller’s top
, making it point to the next most recent item in the stack.
*top--
is *(top--)
, which decrements top
and gets the object that the undecremented value points to. That also gets the caller’s top
, but it decrements the function’s local top
, not the caller’s object. Because the function’s top
is decremented, it no longer points to the caller’s top
.
CodePudding user response:
What (*top)--
does is:
- Dereferences
top
, i.e. accesses the value whichtop
is pointing to. - Decrements that value.
What *top--
does is:
- Decrements
top
, i.e. the value oftop
itself - Dereferences that value.
Besides that, I think it would be better if you define a stack structure instead of using a raw array and an integer as a pointer.
#define STACK_CAPACITY 3 // Adjust it as you want
struct stack {
int items[STACK_CAPACITY];
int top;
};
void stack_init(struct stack *s)
{
s->top = -1;
}
int stack_push(struct stack *s, int item)
{
if (s->top == STACK_CAPACITY-1)
return 0; // fail: stack is full
s->items[ s->top] = item;
return 1; // success: item pushed
}
int stack_pop(struct stack *s, int *top)
{
if (s->top == -1)
return 0;
if (top != NULL) // if top is NULL, ignore it
*top = s->items[s->top];
s->top--;
return 1;
}
Here is how you can use it:
int main()
{
struct stack s;
stack_init(&s);
if (!stack_push(&s, 1))
printf("Stack is full\n");
if (!stack_push(&s, 2))
printf("Stack is full\n");
if (!stack_push(&s, 3))
printf("Stack is full\n");
if (!stack_push(&s, 4))
printf("Stack is full\n");
if (!stack_push(&s, 5))
printf("Stack is full\n");
int item;
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 3
stack_pop(&s, NULL); // Ignore the top
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 1
if (!stack_pop(&s, NULL)) {
printf("Stack is empty: cannot pop\n");
}
}
Also, don't use scanf()
to read user input. fgets()
is much safer.
CodePudding user response:
In C, all postfix operators are higher precedence than all prefix (or infix) operators. That's the way the langauge is defined. So
*top--
is equivalent to
*(top--)
and if you want
(*top)--
you need the explicit parenthesis.