关于#c++#的问题:这个pop_stk函数为什么用不了,总感觉是栈的判空函数的问题

想请问一下大家,我写了一个链栈,这个pop_stk函数为什么用不了,总感觉是栈的判空函数的问题。

#include
#include<string>
using namespace std;
//类内数据域的别名(便于之后数据类型的变动)
typedef int type;
//标明数组长度
#define MAX 100
//栈链表的节点
class link_stack_node {
public:
    link_stack_node() {
        num = 0;
        next = NULL;
    }
    //构造一个节点(值,栈底方向下一个节点的地址)
    link_stack_node(type value, link_stack_node* next_local) {
        num = value;
        next = next_local;
    }
    type num;
    link_stack_node* next;
};
//栈类
class stack {
public:
    stack() {
        link_stack_node* top = new link_stack_node;
        size = 1;
    }
    stack(link_stack_node* node) {
        link_stack_node* top = node;
        size = 1;
    }
    link_stack_node* top;
    int size;
};
//初始化(初始化后第一个节点的值)
stack* stk_create(type n) {
    return new stack(new link_stack_node(n, NULL));
}

//链表接口
// 只读接口
int get_stk_len(stack* stk);    //声明获取栈长度的函数
//栈判空(需要判断是否为空的栈)
int stk_is_empty(stack* stk) {
    return get_stk_len(stk);
}
//长度读取接口(需要读取长度的栈)
int get_stk_len(stack* stk) {
    return stk->size;
}
//栈顶元素获取(需要获取元素的栈)
type get_stk(stack* stk) {
    if (!stk_is_empty(stk)) {    //非空则获取栈顶元素
        return stk->top->num;
    }
    else {
        cout << "栈中无元素存在!";
        return NULL;
    }
}

//可写接口
//入栈(要操作的栈,新节点的值)
void push_stk(stack* stk, type n) {
    stack* temp = stk;
    stk->top = new link_stack_node(n, temp->top);
    if (!stk->top) {
        cout << "创建新节点时,内存分配错误!";
        stk = temp;
        return;
    }
    stk->size++;
}
//出栈(要操作的栈)
void pop_stk(stack* stk) {
    if (!stk_is_empty(stk)) {    //判断栈是否为空,非空则出栈
        link_stack_node* temp = stk->top;
        stk->top = stk->top->next;
        delete temp;
        stk->size--;
    }
    else {
        cout << "栈已空!";
    }
}
//清空栈
void clean_stk(stack* stk) {
    while (!stk_is_empty(stk)) {
        pop_stk(stk);    //若栈不为空则调用出战函数,
    }
}

int main() {
    //初始化一个栈
    stack* stk = stk_create(1);
    push_stk(stk, 2);    //入栈一个元素
    cout << get_stk_len(stk) << endl;
    /*
    clean_stk(stk);
    */
    pop_stk(stk);    //出栈第一次
    cout << get_stk_len(stk) << endl;    //输出栈内有几个元素

    pop_stk(stk);    //出栈第二次
    cout << get_stk_len(stk) << endl;
    cout << get_stk(stk);
    return 0;
}

pop_stk看看是不是重复释放了。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^