2301_78618908 2023-10-24 23:40 采纳率: 12.5%
浏览 4

顺序栈的入栈出栈操作但是只能输入没有输出怎么办

这是我的代码

#include <stdio.h>
#include <malloc.h>
#define sizemax 10
#define error 0
#define ok 1

typedef struct {
    int *base;
    int *top;

} sqstack;

int init(sqstack *s) {
    s = (sqstack *)malloc(sizeof(sqstack));
    s->base=NULL;
    s->top=NULL;
    return ok;
}


//入栈
int push(sqstack *s, int m) {
    if (s->top - s->base == sizemax)
        return error;
    *(s->top) = m;
    s->top++;
    return ok;
}
//出栈 
int pop(sqstack *s) {
    int n;

    if (s->top==NULL) {
        return error;
    }
    n = *(s->top);
    s->top--;
    return n;
}


int main() {
    sqstack s;
    init(&s);
    int a, e, i,j;
    for (i = 0; i < 5; i++) {
        scanf("%d", &e);
        push(&s, e);
    }
    printf("\n");
    printf("pop a number \n");
    scanf("%d", &a);
    for (j = 0; j < a; j++) {
        printf("%d ",pop(&s));
    }
    return 0;

}


































































































































#include <stdio.h>
#include <malloc.h>
#define sizemax 10
#define error 0
#define ok 1

typedef struct {
    int *base;
    int *top;

} sqstack;

int init(sqstack *s) {
    s = (sqstack *)malloc(sizeof(sqstack));
    s->base=NULL;
    s->top=NULL;
    return ok;
}


//入栈
int push(sqstack *s, int m) {
    if (s->top - s->base == sizemax)
        return error;
    *(s->top) = m;
    s->top++;
    return ok;
}
//出栈 
int pop(sqstack *s) {
    int n;

    if (s->top==NULL) {
        return error;
    }
    n = *(s->top);
    s->top--;
    return n;
}


int main() {
    sqstack s;
    init(&s);
    int a, e, i,j;
    for (i = 0; i < 5; i++) {
        scanf("%d", &e);
        push(&s, e);
    }
    printf("\n");
    printf("pop a number \n");
    scanf("%d", &a);
    for (j = 0; j < a; j++) {
        printf("%d ",pop(&s));
    }
    return 0;

}
  • 写回答

2条回答 默认 最新

  • micthis 2023-10-25 08:22
    关注
    #include <stdio.h>
    #include <stdlib.h>
    #define sizemax 10
    #define error 0
    #define ok 1
    typedef struct {
        int *base;
        int *top;
    } sqstack;
    int init(sqstack *s) {
        s->top = s->base = (int *)malloc(sizemax*sizeof(int));
        return ok;
    }
    //入栈
    int push(sqstack *s, int m) {
        if (s->top - s->base == sizemax)
            return error;
        *(s->top) = m;
        s->top++;
        return ok;
    }
    //出栈 
    int pop(sqstack *s) {
        int n;
        if (s->top==s->base) {
            return error;
        }
        s->top--;
        n = *(s->top);
        return n;
    }
    int main() {
        sqstack s;
        init(&s);
        int a, e, i,j;
        for (i = 0; i < 5; i++) {
            scanf("%d", &e);
            push(&s, e);
        }
        printf("\n");
        printf("pop a number \n");
        scanf("%d", &a);
        for (j = 0; j < a; j++) {
            printf("%d ",pop(&s));
        }
        free(s.base);
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月24日

悬赏问题

  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥20 spring boot集成mqtt的使用问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物