数据结构C++版 王红梅 OJ习题

1012: 顺序栈

Description

已知顺序栈类的定义、构造函数及主函数的代码如下,请完成类的其它成员函数,完成相应的输出。

const int StackSize=5; //顺序栈的最大长度(请勿改动)

template <class T>       //定义模板类SeqStack

class SeqStack

{

public:

    SeqStack( ) ;            //构造函数,栈的初始化

       ~SeqStack( );            //析构函数

    void Push(T x);          //将元素x入栈

    T Pop( );                //将栈顶元素弹出

    T GetTop( );            //取栈顶元素(并不删除)

       bool Empty( );           //判断栈是否为空

private:

    T data[StackSize];      //存放栈元素的数组

    int top;                //栈顶指针,指示栈顶元素在数组中的下标

};



/*

 * 前置条件:栈不存在

 * 输    入:无

 * 功    能:栈的初始化

 * 输    出:无

 * 后置条件:构造一个空栈

 */



template <class T>

SeqStack<T>::SeqStack( )

{

       top=-1;

}



/*

 * 前置条件:栈已存在

 * 输    入:无

 * 功    能:销毁栈

 * 输    出:无

 * 后置条件:释放栈所占用的存储空间

 */



template <class T>

SeqStack<T>::~SeqStack( )

{

}

int main()

{

       SeqStack<int> s;

       int x;

       while(1)

       {

              cin>>x;

              if(!x) break;

              try{

                     s.Push(x);

              }

              catch(const char *ms){

                     cout<<"Push:"<<ms<<endl;

              }

       }

       cout<<"Gettop:"<<s.GetTop()<<endl;

      

while(!s.Empty())

       {

              cout<<s.Pop()<<" ";

       }

       cout<<endl;

       try{

              cout<<"Gettop:"<<s.GetTop()<<endl;

       }

       catch(const char *ms){

                     cout<<"Gettop:"<<ms<<endl;

       }

       return 0;

}

Input

Output

Sample Input

1 2 3 4 5 6 7 0

Sample Output

Push:Overflow
Push:Overflow
Gettop:5
5 4 3 2 1 
Gettop:Downflow
#include <iostream>

using namespace std;

const int StackSize = 5; //顺序栈的最大长度(请勿改动)
template<class T>       //定义模板类SeqStack
class SeqStack {
public:
    SeqStack();            //构造函数,栈的初始化
    ~SeqStack();            //析构函数
    void Push(T x);          //将元素x入栈
    T Pop();                //将栈顶元素弹出
    T GetTop();            //取栈顶元素(并不删除)
    bool Empty();           //判断栈是否为空
private:
    T data[StackSize];      //存放栈元素的数组
    int top;                //栈顶指针,指示栈顶元素在数组中的下标
};

template<class T>
SeqStack<T>::SeqStack() {
    top = -1;
}

template<class T>
SeqStack<T>::~SeqStack() {
}

template<class T>
void SeqStack<T>::Push(T x) {
    if (top >= StackSize - 1)
        throw "Overflow";
    data[++top] = x;
}

template<class T>
T SeqStack<T>::Pop() {
    if (top == -1) throw "Downflow";
    return data[top--];
}

template<class T>
T SeqStack<T>::GetTop() {
    if(top == -1)throw "Downflow";
    return data[top];
}

template<class T>
bool SeqStack<T>::Empty() {
    return top == -1;
}

int main() {
    SeqStack<int> s;
    int x;
    while (1) {
        cin >> x;
        if (!x) break;
        try {
            s.Push(x);
        }
        catch (const char *ms) {
            cout << "Push:" << ms << endl;
        }
    }
    cout << "Gettop:" << s.GetTop() << endl;

    while (!s.Empty()) {
        cout << s.Pop() << " ";
    }
    cout << endl;
    try {
        cout << "Gettop:" << s.GetTop() << endl;
    }
    catch (const char *ms) {
        cout << "Gettop:" << ms << endl;
    }
    return 0;
}

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值