數據結構學習記錄(三)——順序棧的簡單實現

簡單寫了一個順序棧的實現,代碼如下:

#ifndef CIRCLE_H
#define CIRCLE_H


#define MaxSize 50;

typedef int ElemType;
struct SqStack                           //定義棧結構
{
    ElemType data[MaxSize];
    int top;
};



//棧的初始化
void InitStack(SqStack &T)
{
    t.top=-1;
}

//棧的判空
bool IsEmpty(SqStack T)
{
    if(T.top==-1)
        return true;
    else
        return false;
}

//入棧操作
bool Push(SqStack &T,ElemType x)
{
    if(T.top==MaxSize-1)
        return false;
    else
    {
        T->data[++T.top]=x;
        return true;
    }
}

//出棧操作
bool Pop(SqStack &T,ElemType &x)
{
    if(IsEmpty(T))
        return false;
    else
        {
            x=T.data[T.top--];
    return true;
        }
}

//取棧頂元素
bool Top(SqStack T,ElemType &x)
{
    if(IsEmpty(T))
        return false;
    else
    {
        x=T.data[T.top];
        return true;
    }
}


#endif

C++好久沒用,臨近考試未經測試,若有編譯錯誤我虛心改正!

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