霍天青 2015-11-26 15:50 采纳率: 100%
浏览 1389
已采纳

请大家帮我看下这段实现链表操作的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();
}
  • 写回答

3条回答 默认 最新

  • Huppert 2015-11-28 03:00
    关注

    push_front 没发现问题,但是clear有问题,clear存在内存泄露,同时伴随崩溃

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于cpci总线的几个问题,有点迷糊
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护