美文网首页
链栈的操作

链栈的操作

作者: 无聊的CairBin | 来源:发表于2021-10-10 15:17 被阅读0次

    链栈的定义

    #include <iostream>
    using namespace std;
    
    //链栈,理论上只要内存够大不存在上溢,只存在下溢(栈空后继续取出元素)
    typedef struct _QNode
    {
        int data;
        struct _QNode *next;
    }StNode;
    

    链栈的操作

    初始化

    bool initStack(StNode* &st)
    {
        st = new StNode;
        if(!st) return false;
        st->next = NULL;
        return true;
    }
    

    判断栈空

    bool isEmpty(StNode *st)
    {
        if(st->next == NULL)
            return true;
        else
            return false;
    }
    

    入栈

    bool pushStack(StNode* &st, int e)
    {
        
        StNode *node = new StNode;
        if(!node) return false;
        
        
        node->data = e;
        node->next = st->next;
        st->next = node;
        
        return true;
    }
    

    出栈

    bool popStack(StNode* &st, int &e)
    {
        if(!(st->next)) return false;   //栈空
        StNode *p;
        p = st->next;
        
        e = p->data;
        st->next = p->next;
        delete p;
        
        return true; 
    }
    

    相关文章

      网友评论

          本文标题:链栈的操作

          本文链接:https://www.haomeiwen.com/subject/qspholtx.html