這裏寫一個最簡單的棧:

主類:

class seStack
{
    int data[100];//數據
    int top;//棧頂
public:
    seStack();
    ~seStack();
    void Pushin(int x);//入棧
    int Pop();//出棧
    int gettop();//獲得棧頂數據
    bool emptys();//檢查是否爲空
};

各種實現函數

檢查:

bool seStack::emptys()
{
    if(top==-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

返回頂數據

int seStack::gettop()
{
    if(emptys())
    {
        cout<<"空";
    }
    else
    {
        return data[top];
    }
}

出棧

int seStack::Pop()
{
    if(emptys())
    {
       cout<<"空";
    }
    else
    {
        int x=data[top];
        top--;
        return x;
    }
}

入棧

void seStack::Pushin(int x)
{
    if(top==maxsize-1)
    {
        cout<<"越界";
    }
    else
    {
        top++;
        data[top]=x;
    }
}

構造函數

seStack::seStack()
{
    top=-1;
}

這個棧很簡單。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章