素食蛤蟆 发表于 2018-4-24 14:33:13

麻烦大神帮忙看看这代码问题在哪?谢谢啦!

本帖最后由 素食蛤蟆 于 2018-4-24 22:56 编辑

#include<stdio.h>
#include<stdlib.h>

#define STACK_INIT_SIZE 100
#define STACKINCRESEMENT 10

typedef int SElemtype;

typedef struct
{
    SElemtype *base;
    SElemtype *top;
    int Stacksize;
}SqStack;

int InitStack(SqStack S)
{
    S.base=(SElemtype *)malloc(STACK_INIT_SIZE*sizeof(SElemtype));
    if(!S.base)
    {
      exit(0);
    }
    S.top=S.base;
    S.Stacksize=STACK_INIT_SIZE;

    return 1;
}

int Push(SqStack S,SElemtype e)
{
    if(S.top-S.base>=S.Stacksize)
    {
      S.base=(SElemtype *)realloc(S.base,(S.Stacksize+STACKINCRESEMENT)*sizeof(SElemtype));
      S.top=S.base+S.Stacksize;
      S.Stacksize+=STACKINCRESEMENT;
    }
    *S.top++=e;//执行到此处时卡死
    return 1;
}

int Pop(SqStack S,SElemtype num)
{
    if(S.top==S.base)
    {
      exit(0);
    }
    num=*--S.top;
    return num;
}

int main()
{
    int i;
    int e;
    //int temp;
    int num;
    SqStack(S);
    InitStack(S);
    for(i=0;i<5;i++)
    {
      printf("please input the elem you what to push into the stack:\n");
      scanf("%d",&e);
      Push(S,e);
    }
    for(i=0;i<5;i++)
    {
      Pop(S,num);
      printf("the %d th number is %d \n",5-i,num);
    }
    system("PAUSE");
    return 1;
}

BngThea 发表于 2018-4-24 14:36:37

报错信息呢

素食蛤蟆 发表于 2018-4-24 14:38:29

BngThea 发表于 2018-4-24 14:36
报错信息呢

输入第一个数据就死啦。。。

BngThea 发表于 2018-4-24 15:50:03

素食蛤蟆 发表于 2018-4-24 14:38
输入第一个数据就死啦。。。

设置断点,看执行到哪里出错

段坤11 发表于 2018-5-1 20:13:14

666

素食蛤蟆 发表于 2018-5-8 17:31:01

段坤11 发表于 2018-5-1 20:13
666

感谢大侠送来的嘲讽!

Neverturnback 发表于 2018-5-9 13:22:26

把SqStack(S);定义成全局变量0 -0就不会报错勒, 还有Pop(S,num)貌似你没有输入num的值, 最后Pop()这个函数返回的值num是干嘛的 0-0 你要保存这个num的话要把Pop()付值给一个变量啊0- 0.以上就是我发现的几个问题。。。。(我目前也还是新手,如果就说错的地方请指出来)

邹普凡 发表于 2018-6-14 21:42:02

呵呵

dable 发表于 2018-6-22 11:52:24

来学习一下

晓响雷鸣 发表于 2018-6-22 20:12:12

报错信息或语法错误处截图下最好

liuzhengyuan 发表于 2018-7-12 20:17:37

(⊙o⊙)…~~~好复杂

长乐夜未央 发表于 2018-7-12 20:28:45

这个看不太懂,只能帮顶了。。。
页: [1]
查看完整版本: 麻烦大神帮忙看看这代码问题在哪?谢谢啦!