-2
這是我用很喜歡列表棧的程序執行棧的程序不斷出現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));
{
struct SNode* holder=*top;
temp->data=x;
temp->next=(holder);
holder=temp;
printf("%d was Pushed",x);
}
}
我不明白this..i認爲有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();
}
任何幫助將不勝感激
解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你應該[編輯]你的問題,以包含一個[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子來重現你的問題,以及你在調試器中所做的觀察。 –
請不要在沒有嚴肅推理的情況下使用'goto'! –
首先,你的「pop()」實際上並沒有彈出(沒有賦值給「* top」)。因此,它會打印頂部元素並釋放它,但「頂部」仍然指向那個晃動的內存。 – axalis