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存在內存洩露,同時伴隨崩潰