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

1013: 链式栈

Description

已知链栈类的定义、构造函数及main函数如下,请完成其余的成员函数实现。

template <class T>

class LinkStack

{

public:

    LinkStack( );              //构造函数,置空链栈

    ~LinkStack( );             //析构函数,释放链栈中各结点的存储空间

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

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

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

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

private:

    Node<T> *top;             //栈顶指针即链栈的头指针

};



/*

 * 前置条件:栈不存在

 * 输    入:无

 * 功    能:栈的初始化

 * 输    出:无

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

 */



template <class T>

LinkStack<T>::LinkStack( )

{

       top=NULL;

}

 int main()

{

       LinkStack<char> s;

       char ch;

       while(1)

       {

              cin>>ch;

              if(ch=='#') break;

              s.Push(ch);           

       }

       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

asdfgh#

Sample Output

Gettop:h
h g f d s a 
Gettop:Downflow
//
// Created by Legends丶Hu on 2020/2/4.
//
#include <iostream>

using namespace std;
template<class T>
struct Node {
    T data;
    Node<T> *next;
};

template<class T>
class LinkStack {
public:
    LinkStack();              //构造函数,置空链栈
    ~LinkStack();             //析构函数,释放链栈中各结点的存储空间
    void Push(T x);           //将元素x入栈
    T Pop();                  //将栈顶元素出栈
    T GetTop();               //取栈顶元素(并不删除)
    bool Empty();             //判断链栈是否为空栈
private:
    Node<T> *top;             //栈顶指针即链栈的头指针
};


template<class T>
LinkStack<T>::LinkStack() {
    top = NULL;
}

template<class T>
LinkStack<T>::~LinkStack() {
    while (top) {
        Node<T> *p = top;
        top = top->next;
        delete p;
    }
}

template<class T>
void LinkStack<T>::Push(T x) {
    Node<T> *s = new Node<T>;
    s->data = x;
    s->next = top;
    top = s;
}

template<class T>
T LinkStack<T>::Pop() {
    if(top == NULL) throw "Downflow";
    T x = top->data;
    Node<T> *p = top;
    top = top->next;
    delete p;
    return x;
}

template<class T>
T LinkStack<T>::GetTop() {
    if (top == NULL) throw "Downflow";
    return top->data;
}

template<class T>
bool LinkStack<T>::Empty() {
    return top == NULL;
}

int main() {
    LinkStack<char> s;
    char ch;
    while (1) {
        cin >> ch;
        if (ch == '#') break;
        s.Push(ch);
    }
    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;
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值