史天亮123 2022-10-06 15:37 采纳率: 75%
浏览 20

顺序栈的初始化代码好像有问题

#include <stdio.h>#include <stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */#define MAXSIZE 100typedef int SElemType;typedef int Status;typedef struct{ SElemType *base; SElemType top; int stacksize;}SqStack;Status InitStack(SqStack S){ S->base=(SElemType)malloc(MAXSIZEsizeof(SElemType)); if(!S->base) return 0; S->top=S->base; S->stacksize=MAXSIZE; return 1;}Status Push(SqStack *S,SElemType e){ if(S->top-S->base==S->stacksize) return 0; *(S->top)++=e; return 1;}SElemType GetTop(SqStack *S){ if(S->top!=S->base) printf("11"); return *(S->top-1);}int main(int argc, char *argv[]) { SqStack *s; //printf("--"); printf("%d",InitStack(s)); Push(s,3); printf("%d",GetTop(s)); return 0;}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-10-08 12:34
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    #define MAXSIZE 100
    typedef int SElemType;
    typedef int Status;
    typedef struct{ 
        SElemType *base; 
        SElemType top; 
        int stacksize;
    }SqStack;
    Status InitStack(SqStack*& S) //修改
    {
        S = (SqStack*)malloc(sizeof(SqStack));  // 修改
        S->base = (SElemType*)malloc(MAXSIZE * sizeof(SElemType));
        if (!S->base) return 0;
        S->top = 0;  //S->base; 修改
        S->stacksize = MAXSIZE;
        return 1;
    }
    Status Push(SqStack *S,SElemType e)
    { 
        if (S->top == S->stacksize)  //(S->top-S->base==S->stacksize)修改
            return 0; 
        S->base[S->top++] = e;      //*(S->top)++=e; 修改
        return 1;
    }
    Status Pop(SqStack* S, SElemType* e) //修改
    {
        if (S->top == 0) {
            *e = -1;
            return 0;
        }
        *e = S->base[--S->top];
        return 1;
    }
    SElemType GetTop(SqStack *S)
    { 
        if (S->top == 0) //(S->top!=S->base)修改 
        {
            printf("11\n");
            return -1;       //修改
        }
        return  S->base[S->top - 1]; //*(S->top-1);修改
    }
    int main(int argc, char *argv[]) 
    { 
        SqStack *s; 
        SElemType e;
        
        printf("%d\n",InitStack(s)); 
        printf("--\n");
        Push(s, 3); 
        Push(s, 5);
        Push(s, 7);
        printf("%d\n",GetTop(s)); 
    
        Pop(s, &e);
        printf("%d\n", e);
        Pop(s, &e);
        printf("%d\n", e);
        Pop(s, &e);
        printf("%d\n", e);
        Pop(s, &e);
        printf("%d\n", e);
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月6日

悬赏问题

  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题