4

Stack - Pop function print a zero I have not pushed

 2 years ago
source link: https://www.codesd.com/item/stack-pop-function-print-a-zero-i-have-not-pushed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Stack - Pop function print a zero I have not pushed

advertisements

I did an assignment to create a stack using structures. My problem is when i define a variable(num) in pop, whenever i pop it an extra 0 shows up. So instead of printing num i printed st.stk[st.top] and then it works.

I want to know why i get that 0 as output when the comment marks are removed and i print num instead of st.stk[st.top]

#include<stdio.h>
#include<stdio.h>
#define SIZE 4

struct stack{
    int stk[SIZE];
    int top;
} st;

main()
{
    st.top=-1;
    int option,ans;
    printf("\nStack implementation in C");

    do {
        printf("\nChoose what you want to do \n1.Push\n2.Pop\n3.Display\n4.Exit");
        scanf("%d",&option);

        switch(option)
        {
            case 1:
                push();
                break;

            case 2:
                pop();
                break;

            /*case 3:
                display();
                break;

            case 4:
                exit(1);
                break;*/
            default:
                printf("wrong choice");
                break;
        }

  } while(ans != 4);

    getch();
}

void push()
{
    int num;

    if(st.top==SIZE-1)  //if top== 3     0.1.2.3 } 4 digits
    {
        printf("\nStack overflaow");
        return; //exit from function if stack is overflowed
    }

    printf("\nPlease enter neumber you want to enter");
    scanf("%d",&num);

    st.stk[st.top]=num;
    st.top++;
}

void pop()
{
    //int num; 

    if(st.top==-1) //back to initial position
    {
        printf("\nStack underflow");
        return;
    }

    //num=st.stk[st.top];
    st.top--;
    printf("\nItem popped is %d", st.stk[st.top]);M
}

Thanks for any answers, this is my first question, and of course i searched before posting but was unable to find any answers :(

**I can post the whole code if needed


You have a couple off-by-one errors in your push() and pop() functions that are probably causing you headaches. To fix them you must simply revise the order of your operations.

To fix the pop() function, try the following:

void pop()
{
    int num; 

    if(st.top==-1) //back to initial position
    {
       printf("\nStack underflow");
       return;
    }

    num = st.stk[st.top];
    st.top--;

    // At this point st.stk[st.top] is no longer the stacked element (since we changed st.top).
    printf("\nItem popped is %d", num); // Use the value you red before you changed the stack.
}

To fix the push() function, try the following:

void push()
{
    int num;

    if(st.top == SIZE - 1)  //if top== 3     0.1.2.3 } 4 digits
    {
        printf("\nStack overflaow");
        return; //exit from function if stack is overflowed
    }

    printf("\nPlease enter neumber you want to enter");
    scanf("%d",&num);

    // Here st.top refers to the index of the last element that was pushed on the stack or -1 when the stack is empty.
    // We must first increment it and then assign the value.
    st.top++;
    st.stk[st.top]=num;
}

In the future when trying to find off-by-one errors it helps to go through the operations on paper or in your head trying out both the pop() and push() functions on an empty stack, on a full stack and on a half-full/half-empty stack.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK