数据结构 栈。将非负整数的十进制转换为八进制输出

代码没有报错但不知道问题出现在哪里,没有输出结果就直接跳出了

img


```c
#include
#include
#include
#include
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef struct {
    ElemType* base;
    ElemType* top;
    int stacksize;
}SqStack;
void InitStack(SqStack* S) {
    S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
    if (!S->base)exit(OVERFLOW);
    S->base = S->top;
    S->stacksize = MAXSIZE;
}
int Push(SqStack* S, ElemType e) {
    if (S->top - S->base >= S->stacksize) {
        return ERROR;
        /*S->base = (ElemType*)realloc(S->base, (S->stacksize + MAXSIZE) * sizeof(ElemType));
        S->top = S->base + S->stacksize;
        S->stacksize = S->stacksize + MAXSIZE;*/
    }
    *S->top++ = e;
    return OK;
}
int Pop(SqStack* S, ElemType e) {
    if (S->top == S->base) {
        return ERROR;
    }
    e = *--S->top;
    printf("%d", e);
    return OK;
}
int Empty(SqStack* S) {
    if (S->base == S->top) {
        return OK;
    }
    else
    {
        return ERROR;
    }
}
int main() {
    SqStack* S;
    SqStack a;
    S = &a;
    int N;
    ElemType e = 0;
    InitStack(&S);
    printf("请输入一个非负整数\n");
    scanf_s("%d", &N);
    system("pause");
    while (N) {
        Push(&S, N % 8);
        N = N / 8;
    }
    system("pause");
    while (!Empty(S))
    {
        Pop(&S, &e);
        
        system("pause");
    }
    return 0;
}

```

这段代码,我看了一下,只需要改一些地方即可,我改好了。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef struct {
    ElemType* base;
    ElemType* top;
    int stacksize;
} SqStack;

void InitStack(SqStack* S) {
    S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
    if (!S->base) exit(OVERFLOW);
    S->top = S->base;
    S->stacksize = MAXSIZE;
}

int Push(SqStack* S, ElemType e) {
    if (S->top - S->base >= S->stacksize) {
        return ERROR;
    }
    *S->top++ = e;
    return OK;
}

int Pop(SqStack* S, ElemType* e) {
    if (S->top == S->base) {
        return ERROR;
    }
    *e = *--S->top;
    return OK;
}

int Empty(SqStack* S) {
    if (S->base == S->top) {
        return OK;
    }
    else {
        return ERROR;
    }
}

int main() {
    SqStack* S;
    SqStack a;
    S = &a;
    int N;
    ElemType e = 0;
    InitStack(S);
    printf("请输入一个非负整数\n");
    scanf_s("%d", &N);
    while (N) {
        Push(S, N % 8);
        N = N / 8;
    }
    while (!Empty(S))
    {
        Pop(S, &e);
        printf("%d", e);
    }
    return 0;
}

供参考:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef struct {
    ElemType* base;
    ElemType* top;
    int stacksize;
}SqStack;
void InitStack(SqStack* S) {
    S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
    if (!S->base)exit(OVERFLOW);
    S->top = S->base;   //S->base = S->top;  修改
    S->stacksize = MAXSIZE;
}
int Push(SqStack* S, ElemType e) {
    if (S->top - S->base >= S->stacksize) {
        return ERROR;
        /*S->base = (ElemType*)realloc(S->base, (S->stacksize + MAXSIZE) * sizeof(ElemType));
        S->top = S->base + S->stacksize;
        S->stacksize = S->stacksize + MAXSIZE;*/
    }
    *S->top++ = e;
    return OK;
}
int Pop(SqStack* S, ElemType* e) { //修改 ElemType e  修改
    if (S->top == S->base) {
        return ERROR;
    }
    *e = *--S->top;   //e = *--S->top;      修改
    printf("%d", *e); //printf("%d", e);    修改
    return OK;
}
int Empty(SqStack* S) {
    if (S->base == S->top)
        return OK;
    else
        return ERROR;
}
int main() {
    SqStack* S;
    SqStack a;
    S = &a;
    int N;
    ElemType e = 0;
    InitStack(S);        //InitStack(&S);  修改
    printf("请输入一个非负整数\n");
    scanf_s("%d", &N);
                        //system("pause"); 修改 
    while (N) {
        Push(S, N % 8);  //Push(&S, N % 8);修改 
        N = N / 8;
    }
                        //system("pause"); 修改
    while (!Empty(S))
    {
        Pop(S, &e);    //Pop(&S, &e);      修改
                       //system("pause");  修改
    }
    return 0;
}

这段代码有以下几个问题:
(1)在 InitStack 函数中,赋值 S->base = S->top 应该改为 S->top = S->base,因为 S->top 指向的是栈顶,初始化时应该指向栈底。
(2)在 Push 函数中,应该先判断栈是否已满,然后再进行入栈操作。
(3)在 Pop 函数中,传入的参数 e 应该是指针类型,因为需要修改 e 的值。而且,弹出栈顶元素后应该将其释放掉,避免内存泄漏。
(4)在 main 函数中,调用 InitStack 函数时,应该传入的是地址而不是指针本身。而且, S 已经是指针类型,不需要再用 & 取地址。