【发布时间】: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()?