zzk.213 2022-09-22 21:38 采纳率: 87.8%
浏览 41

最后的DestroyStack应该怎么按照注释来写


#include<stdio.h>
#include<stdlib.h>
typedef struct{
    double* values;
    int top;
    int maxTop;
} Stack;

bool CreateStack(Stack* stack, int size) {
    if (size <= 0)
        return false;
    stack->values = (double*)malloc(sizeof(double) * size);
    stack->top = -1; 
    stack->maxTop = size - 1;//max的最大index是size-1
        return true;
}
bool IsEmpty(Stack* stack) {
    if (stack->top == -1)
        return true;
    else
        return false;
}
bool IsFull(Stack* stack) {
    if (stack->top == stack->maxTop)
        return true;
    else
        return false;
}
bool Top(Stack* stack, double* x) {
    if (stack->top == -1) {
        return false;
    }
    else {
        *x = stack->values[stack->top];
        return true;
    }
}
bool Push(Stack* stack, double x) {
    if (IsFull(stack))
        return false;
    stack->values[++stack->top] = x;
    return true;
}
bool Pop(Stack* stack, double* x) {
    if (stack->top == -1)
        return false;
    else {
        *x = stack->values[stack->top];//
        stack->top--;
        return true;
    }
}
/*void DisplayStack(Stack* stack) {
    int i;
    for (i = stack->top; i > 0; i--) {
        printf("%d\n", stack->values[i]);
    }*/
void DestroyStack(Stack** pstack) {
    //frees the memory occupied by the stack values and stack struct
    // 将stack values和stack struct占用的内存free掉
    //point the stack to NULL
    //将stack指向NULL
}
  • 写回答

2条回答 默认 最新

  • 梦里逆天 2022-09-22 22:45
    关注

    这么写行不行?

    void DestroyStack(Stack** pstack) {
        //frees the memory occupied by the stack values and stack struct
        // 将stack values和stack struct占用的内存free掉
        free(pstack->values);
        free(pstack->maxTop);
        //point the stack to NULL
        //将stack指向NULL
        pstack->top = NULL;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月22日