堆栈程序总是弹出0

时间:2017-06-10 15:13:33

标签: c++ stack

这是我使用喜欢列表

实现的堆栈程序
 #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    struct SNode
    {
        int data;
        struct SNode* next;
    };
    void push(struct SNode** top,int x)
    {
        struct SNode* temp=(struct SNode*)malloc(sizeof(SNode));
        {
            struct SNode* holder=*top;
            temp->data=x;
            temp->next=(holder);
            holder=temp;
            printf("%d was Pushed",x);
        }
    }

我不明白......我认为这里存在spme问题......

void pop(struct SNode** top)
{
    struct SNode* temp;
    struct SNode* holder=*top;
    int x1;
    if(holder==NULL)
    {
        printf("Stack is empty ");
    }

    temp=holder;
    x1=holder->data;
    holder=holder->next;
    printf("%d was popped  from the stack",x1);
    free(temp);
}

也在这里它总是在堆栈顶部显示0

void peek(struct SNode* top)
{
    printf("%d is at the top of the stack",top);
}
void main()
{
    clrscr();
    int x,c,c1;
    jump:

这是我的标签

 printf("Do you want to Push/Pop/Peek an element ??\n");
            printf("1)Push\t2)Pop\t3)Peek\n");
            scanf("%d",&c);
            struct SNode* top=NULL;

切换案例

switch(c)
                {

推送

case 1:


    {
                                printf("Enter Element :");
                                scanf("%d",&x);
                                push(&top,x);
                                printf("\nDo you want to continue :1)Yes 2)No\n");
                                scanf("%d",&c1);
                                if(c1==1)
                                {
                                    goto jump;
                                }
                                else
                                {
                                    printf("GGWP....");
                                }
                            break;

                            }

弹出

case 2:
                        {
                            pop(&top);
                            printf("\nDo you want to continue :1)Yes 2)No\n");
                            scanf("%d",&c1);
                            if(c1==1)
                            {
                                goto jump;
                            }
                            else
                            {
                                printf("GGWP....");
                            }
                            break;

                        }

堆栈顶部

            case 3:
            {
                peek(top);
                printf("\nDo you want to continue :1)Yes 2)No\n");
                scanf("%d",&c1);
                if(c1==1)
                {
                    goto jump;
                }
                else
                {
                    printf("GGWP....");
                }
                break;

            }
                    default:
                    {
                        printf("Enter the correct option ...");
                        goto jump;
                        break;
                    }
                }
                getch();
            }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

此代码有效...我犯了一些错误

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct SNode
{
    int data;
    struct SNode* next;
};
void push(struct SNode** top,int x)
{
    struct SNode* temp=(struct SNode*)malloc(sizeof(SNode));
    {

持有者不会维护结构,因为你必须返回它的值并在void main()中捕获它             // struct SNode * holder = * top;

temp->data=x;
            temp->next=(*top);
            *top=temp;
            printf("%d was Pushed",temp->data);
        }
    }
    void pop(struct SNode** top)
    {
        struct SNode* temp;
        int x1;
        if(*top==NULL)
        {
            printf("Stack is empty ");
        }
        //printf("%d",*top->data);
        temp=*top;
        x1=(*top)->data;
        *top=(*top)->next;
        printf("%d was popped  from the stack",x1);
        free(temp);
    }
    void peek(struct SNode* top)
    {
        printf("%d is at the top os the stack",top->data);
    }
    void main()
    {
        clrscr();
        int x,c,c1;
        int i;
        struct SNode* top=NULL;
        int counter=0;
        jump:
        printf("Do you want to Push/Pop/Peek/Display an element ??\n");
        printf("1)Push\t2)Pop\t3)Peek\t4)Display\n");
        scanf("%d",&c);
        switch(c)
        {
            case 1:
            {
                printf("Enter Element :");
                scanf("%d",&x);
                push(&top,x);
                counter++;
                printf("\nDo you want to continue :1)Yes 2)No\n");
                scanf("%d",&c1);
                if(c1==1)
                {
                    goto jump;
                }
                else
                {
                    printf("GGWP....");
                }
            break;

            }
            case 2:
            {
                pop(&top);
                counter--;
                printf("\nDo you want to continue :1)Yes 2)No\n");
                scanf("%d",&c1);
                if(c1==1)
                {
                    goto jump;
                }
                else
                {
                    printf("GGWP....");
                }
                break;

            }
            case 3:
            {
                peek(top);
                printf("\nDo you want to continue :1)Yes 2)No\n");
                scanf("%d",&c1);
                if(c1==1)
                {
                    goto jump;
                }
                else
                {
                    printf("GGWP....");
                }
                break;

            }
            case 4:
            {
                for(i=0;i<counter;i++)
                {
                    int x2;
                    x2=(top)->data;
                    printf("%d ->",x2);
                    top=(top)->next;

                }
                printf("\nDo you want to continue :1)Yes 2)No\n");
                scanf("%d",&c1);
                if(c1==1)
                {
                    goto jump;
                }
                else
                {
                    printf("GGWP....");
                }
                break;
            }
            default:
            {
                printf("Enter the correct option ...");
                goto jump;
                break;

            }
        }
        getch();
    }