【问题标题】:Stack program always popping 0堆栈程序总是弹出 0
【发布时间】:2017-06-10 15:13:32
【问题描述】:

这是我使用喜欢列表实现的堆栈程序

 #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();
            }

任何帮助将不胜感激

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该在edit 您的问题中包含一个重现您的问题的Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 请不要在没有认真理由的情况下使用goto
  • 首先,您的“pop()”实际上并没有弹出(没有分配给“*top”)。因此,它打印顶部元素并释放它,但“顶部”仍然指向那个悬空的内存。
  • 为什么要用C++标签?
  • 为什么要void main()

标签: c++ stack


【解决方案1】:

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

#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));
    {

Holder 不会维护结构,因为你必须返回它的值并在 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();
    }

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多