程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-請大家幫我看下這段實現鏈表操作的C++的代碼。

c++-請大家幫我看下這段實現鏈表操作的C++的代碼。

編輯:編程綜合問答
請大家幫我看下這段實現鏈表操作的C++的代碼。

圖片說明
圖片說明
push_front這個操作有問題。

 #include <iterator>
using namespace std;
template <typename T>
class List{
    struct node{
        node() = default;
        node(const T& x, node *y=nullptr) :m_data(x), m_next(y) {}
        T m_data;
        node *m_next;
    };
    node *m_head;
public:
    class iterator
        : public std::iterator<std::forward_iterator_tag, T>
    {
        node* m_rep;
    public:
        friend class const_iterator;
        friend class List;
        inline iterator(node* x = 0) :m_rep(x){}
        inline iterator(const iterator& x) : m_rep(x.m_rep) {}
        inline iterator& operator=(const iterator& x)
        {
            m_rep = x.m_rep; return *this;
        }
        inline iterator& operator++()
        {
            m_rep = m_rep->m_next; return *this;
        }
        inline iterator operator++(int)
        {
            iterator tmp(*this); m_rep = m_rep->m_next; return tmp;
        }
        inline reference operator*() const { return m_rep->m_data; }
        inline pointer operator->() const { return m_rep; }
        inline bool operator==(const iterator& x) const
        {
            return m_rep == x.m_rep;
        }
        inline bool operator!=(const iterator& x) const
        {
            return m_rep != x.m_rep;
        }
    };

    class const_iterator
        : public std::iterator<std::forward_iterator_tag, const T>
    {
        const node* m_rep;
    public:
        friend class iterator;
        friend class List;
        inline const_iterator(const node* x = 0) :m_rep(x){}
        inline const_iterator(const const_iterator& x) : m_rep(x.m_rep) {}
        inline const_iterator(const iterator& x) : m_rep(x.m_rep){}
        inline const_iterator& operator=(const const_iterator& x)
        {
            m_rep = x.m_rep; return *this;
        }
        inline const_iterator& operator=(const iterator& x)
        {
            m_rep = x.m_rep; return *this;
        }
        inline const_iterator& operator++()
        {
            m_rep = m_rep->m_next; return *this;
        }
        inline const_iterator operator++(int)
        {
            const_iterator tmp(*this); m_rep = m_rep->m_next; return tmp;
        }
        inline reference operator*() const { return m_rep->m_data; }
        inline pointer operator->() const { return m_rep; }
        inline bool operator==(const const_iterator& x) const
        {
            return m_rep == x.m_rep;
        }
        inline bool operator!=(const const_iterator& x) const
        {
            return m_rep != x.m_rep;
        }
    };
    inline iterator begin() { return iterator(m_head); }
    inline iterator end() { return iterator(); }
    inline const_iterator begin() const { return m_head; }
    inline const_iterator end() const { return const_iterator(); }

    List() :m_head(nullptr) {}
    void push_front(const T& data) { node *temp = new node(data,m_head); m_head = temp;}
    void reverse();
    List(const List& l);
    void clear()
    {
        node *next;
        for (node *temp = m_head; temp; temp = temp->m_next)
        {
            next = temp->m_next;
            delete temp;
            temp = next;
        }
        m_head = nullptr;
    }
    ~List() { clear(); }
    size_t size() { int i = 0; for (node*temp = m_head; temp; temp = temp->m_next, ++i); return i; }
    bool empty() { return size(); }
    int locate(const T& t)  //若不存在,則返回-1
    {
        int i = 0;
        for (node *temp = head; temp; temp = temp->m_next, ++i)
        if (temp->m_data == t)
            return i;
        return -1;
    }
};
template <typename T>
void List<T>::reverse()
{
    node *cur = m_head;
    node *prior = nullptr;
    while (cur)
    {
        node *temp = cur->m_next;
        cur->m_next = prior;
        prior = cur;
        cur = temp;
    }
    m_head = prior;
}

template <typename T>
List<T>::List(const List<T>& l) :m_head(nullptr)
{
    while (List<T>::const_iterator it = l.begin(); it != l.end(); ++it)
        this->push_front(*it);
    reverse();
}

最佳回答:


push_front 沒發現問題,但是clear有問題,clear存在內存洩露,同時伴隨崩潰

zay999abc
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved