zoukankan      html  css  js  c++  java
  • 2.C++单链表

    Slist:

    #ifndef SLIST_H_
    #define SLIST_H_
    
    #include <cassert>
    
    template<typename T>
    struct Slist_node
    {
        T element;
        Slist_node * next;
        Slist_node():element(),next(0){}
        explicit Slist_node(const T& elem):element(elem),next(0){}
        ~Slist_node(){next=0;}
    };
    template<typename T>
    class Slist
    {
        typedef Slist_node<T>* Ptrn;
    private:
        Slist_node<T>* hb_head;
        int hb_size;
    public:
        Slist():hb_head(new Slist_node<T>),hb_size(0){}
        ~Slist();
        bool empty() const {return hb_size ? false : true;}
        int size() const {return hb_size;}
    
        void reverse();
    
        T value(int pos) const;
        void push_front(const T& elem);
        T pop_front();
        void push_back(const T& elem);
        T pop_back();
        void insert_after(const T& elem, int pos);
        T erase(int pos);
        void erase(int begin, int end);
        void clear();
    };
    
    template<typename T>
    Slist<T>::~Slist()
    {
        clear();
        delete hb_head;
    }
    
    template<typename T>
    void Slist<T>::clear()
    {
        if(hb_size != 0)
            erase(1,hb_size);
    }
    
    template<typename T>
    void Slist<T>::erase(int begin, int end)
    {
        if(begin>hb_size || end>hb_size || begin<0 || begin>end)
            cout<<"error:position out of range!"<<endl;
        else
        {
            Ptrn ptr = hb_head;
            Ptrn prev = 0;
            int i=0;
            while(i!=begin)
            {
                prev = ptr;
                ptr = ptr->next;
                ++i;
            }
            while(i<=end)
            {
                Ptrn ptm = ptr;
                ptr = ptr->next;
                delete ptm;
                --hb_size;
                ++i;
            }
            prev->next = ptr;
        }
    }
    
    template<typename T>
    T Slist<T>::erase(int pos)
    {
        assert(pos<=hb_size && pos!=0);
        Ptrn ptr = hb_head;
        Ptrn prev = 0;
        int i=0;
        while(i!=pos)
        {
            prev = ptr;
            ptr = ptr->next;
            ++i;
        }
        T t = ptr->element;
        prev->next = ptr->next;
        delete ptr;
        --hb_size;
        return t;
    }
    
    template<typename T>
    void Slist<T>::insert_after(const T& elem, int pos)
    {
        Ptrn ptr = hb_head;
        int i=0;
        while(i!=pos)
        {
            ptr = ptr->next;
            i++;
        }
        Slist_node<T>* pNew = new Slist_node<T>;
        pNew->element = elem;
        pNew->next = ptr->next;
        ptr->next = pNew;
        ++hb_size;
    }
    
    template<typename T>
    void Slist<T>::push_front(const T& elem)
    {
        insert_after(elem,0);
    }
    
    template<typename T>
    void Slist<T>::push_back(const T& elem)
    {
        insert_after(elem,hb_size);
    }
    
    template<typename T>
    T Slist<T>::pop_front()
    {
        return erase(1);
    }
    
    template<typename T>
    T Slist<T>::pop_back()
    {
        return erase(hb_size);
    }
    
    template<typename T>
    T Slist<T>::value(int pos) const
    {
        Ptrn ptr = hb_head;
        int i=0;
        while(i!=pos)
        {
            ptr=ptr->next;
            i++;
        }
        return ptr->element;
    }
    
    template<typename T>
    void Slist<T>::reverse()
    {
        Ptrn pbegin = hb_head->next;
        hb_head->next = 0;
        hb_size = 0;
        Ptrn ptr = pbegin;
        while(ptr!=0)
        {
            push_front(ptr->element);
            Ptrn ptm = ptr;
            ptr = ptr->next;
            delete ptm;
        }
    }
    
    #endif

    Test:

    #include<iostream>
    #include"s_list.h"
    
    using namespace std;
    int main()
    {
        Slist<int> slist;
        int num = slist.size();
        cout<<num<<endl;
    
        slist.push_back(2);
        for (int i=0; i<slist.size(); i++)
            cout<<slist.value(i+1)<<'-';
        cout<<endl;
    
        slist.push_front(4);
        for (int i=0; i<slist.size(); i++)
            cout<<slist.value(i+1)<<'-';
        cout<<endl;
    
        slist.insert_after(7,1);
        cout<<"size = "<< slist.size() <<endl;
    
        for (int i=0; i<slist.size(); i++)
            cout<<slist.value(i+1)<<'-';
        cout<<endl;
    
        slist.reverse();
        cout<<"size = "<<slist.size()<<endl;
        for (int i=0; i<slist.size(); i++)
            cout<<slist.value(i+1)<<'-';
        cout<<endl;
    
        slist.clear();
        cout<<"size = "<<slist.size()<<endl;
    
    
        return 0;
    }
  • 相关阅读:
    missing requires of libmysqlclient.so.18()(64bit)
    Ambari安装HDP问题:User root is not allowed to impersonate anonymous.User: hcat is not allowed to impersonate ambari-qa
    ambari2.6.50 openssl 版本问题:SSLError: Failed to connect. Please check openssl library versions. Openssl error upon host registration
    HDP 2.6 requires libtirpc-devel
    Kafka 如何读取offset topic内容 (__consumer_offsets)
    Linux集群时间同步方法
    centos7 ambari2.6.1.5+hdp2.6.4.0 大数据集群安装部署
    Centos7.3离线(rpm方式)安装mysql服务
    ubuntu安装rpm的方法
    kerberos环境storm配置:Running Apache Storm Securely
  • 原文地址:https://www.cnblogs.com/jx-yangbo/p/4839626.html
Copyright © 2011-2022 走看看