首页 > 其他 > 详细

数据结构之——基于链表的栈的模板实现

时间:2017-06-14 18:01:24      阅读:321      评论:0      收藏:0      [点我收藏+]

 

//节点的结构
template<typename T>
struct node
{
    T data;
    node<T>* next;
    node():next(nullptr){};
    node(T t):data(t),next(nullptr){};
}
//模板类构造栈类
template<typename T>
Class MyStack
{
public:
    stack();
    void push(T &x);
    T top();
    void pop();
    int counts();
    bool empty();
    ~stack();
private:
    int count;
    node<T>* head;
}
template<typename T>
void MyStack<T>::push(T &x)
{
    node<T>* pnode= new node<T>(x);//申请空间
    pnode->next=head->next;
    head->next=pnode;
    count++;
}
template<typename T>
bool MyStack<T>::empty()
{
    return count==0;
}
template<typename T>
T MyStack<T>::top()
{
    if(!empty())
        return head->next->dada;
}
template<typename T>
void MyStack<T>::pop()
{
    if(!empty())
    {
        node<T>* del=head->next;
        head->next=haed->next->next;
        delete del;
        count--;
    }
}
template<typename T>
int MyStack<T>::counts()
{
    return count;
}

 

数据结构之——基于链表的栈的模板实现

原文:http://www.cnblogs.com/gaoxiaoyan123/p/7010015.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!