zoukankan      html  css  js  c++  java
  • LinkStack

    #include <iostream>
    using namespace std;
    
    template <class T>
    class Node
    {
    public:
        T data;
        Node *next;
        Node () { }
        Node (T d, Node *p): data(d), next(p) { }
    };
    
    template <class T>
    class LinkStack
    {
    private:
        Node<T> *top;    
    public:
        LinkStack(): top(NULL) { }
        
        ~LinkStack() {makeEmpty(top); }
    /*    
        LinkStack(LinkStack<T> &L) 
        {
            cpy(this, L.top);
        }
        
        void cpy(LinkStack<T> *s, Node<T> *x)
        {
            if(x->next) cpy(s, x->next);
            s->Push(x->data);
        } 
    */    
        void Push(const T &x) //
        {
            top = new Node<T>(x, top);
        }
        
        bool Pop(T &x) //
        {
            if(IsEmpty()) return false;
            
            getTop(x);
            Node<T> *d = top;
            top = top->next;
            delete d;
            return true;
        }
        
        bool getTop(T &x) const //
        {
            if(IsEmpty()) return false;
            x = top->data;
            return true;
        }
        
        bool IsEmpty() const { return (top == NULL) ? true : false; } //ok
        
        int getSize() const //ok
        {
            int ret = 0;
            for(Node<T> *p = top; p; p = p->next)
                ret++;
            return ret;
        }
        
        void makeEmpty(Node<T> *p) //
        {
            if (p) makeEmpty(p->next);
            delete p;
        }
        
        void output() //
        {
            Node<T> *temp = NULL;
            
            while(!IsEmpty()) 
            {
                T t;
                if(Pop(t)) cout << t << " ";
                temp = new Node<T> (t, temp);
            }
            cout << endl; 
            top = temp;
        }
    };

    Mind:

      a.拷贝构造函数总是写不好, 好像因为析构函数的某个位置没搞好, 然后就导致了<< 没法重载, 写的好Low..

  • 相关阅读:
    JPA
    XMPP技术之Smack库的自定义消息扩展
    VMVare的窗口自适应
    linux c tcp p2p
    linux 消息队列
    基数排序-LSD
    基数排序-纪念欧某新
    归并排序
    锦标赛排序
    快速排序 之添加复合插入排序和原始序列取中值左pivot
  • 原文地址:https://www.cnblogs.com/QQ-1615160629/p/5971696.html
Copyright © 2011-2022 走看看