C语言写Pop函数取栈的出错
Pop函数如下intPop(Stack*pstack,int*pname){if(pstack->top<0){return0;}*pname=pstack->data[...
Pop函数如下
int Pop (Stack * pstack, int * pname)
{
if(pstack->top<0)
{
return 0;
}
* pname = pstack->data[pstack->top];
pstack->top--;
return 1;
}
Push函数如下:
int Push (Stack * pstack, int num)
{
if(pstack->top>Stack_size)
{
printf("Push Error!");
return 0;
}
pstack->top++;
pstack->data[pstack->top]=num;
return 0;
}
进行一次入栈出栈倒是正常,但是多次入栈,然后全部出栈的时候Debug就会报错SIGSEGV。我实在看不出来Pop函数哪里不对了。反正调试的时候Watch窗口里面显示数据都入进栈去了。出结果还不对了。。。求救
栈是数组栈,声明如下
typedef struct
{
int data[Stack_size] ;
int top;
} Stack ; 展开
int Pop (Stack * pstack, int * pname)
{
if(pstack->top<0)
{
return 0;
}
* pname = pstack->data[pstack->top];
pstack->top--;
return 1;
}
Push函数如下:
int Push (Stack * pstack, int num)
{
if(pstack->top>Stack_size)
{
printf("Push Error!");
return 0;
}
pstack->top++;
pstack->data[pstack->top]=num;
return 0;
}
进行一次入栈出栈倒是正常,但是多次入栈,然后全部出栈的时候Debug就会报错SIGSEGV。我实在看不出来Pop函数哪里不对了。反正调试的时候Watch窗口里面显示数据都入进栈去了。出结果还不对了。。。求救
栈是数组栈,声明如下
typedef struct
{
int data[Stack_size] ;
int top;
} Stack ; 展开
1个回答
展开全部
Pop函数改成这样:
int Pop (Stack * pstack, int * pname)
{
if(pstack->top<=0)
{
return 0;
}
pstack->top--;
* pname = pstack->data[pstack->top];
return 1;
}
Push函数改成这样:
int Push (Stack * pstack, int num)
{
if(pstack->top>=Stack_size)
{
printf("Push Error!");
return 0;
}
pstack->data[pstack->top]=num;
pstack->top++;
return 0;
}
试试(原来那样当元素达到最大数目时pstack->top就越界了)。
int Pop (Stack * pstack, int * pname)
{
if(pstack->top<=0)
{
return 0;
}
pstack->top--;
* pname = pstack->data[pstack->top];
return 1;
}
Push函数改成这样:
int Push (Stack * pstack, int num)
{
if(pstack->top>=Stack_size)
{
printf("Push Error!");
return 0;
}
pstack->data[pstack->top]=num;
pstack->top++;
return 0;
}
试试(原来那样当元素达到最大数目时pstack->top就越界了)。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询