直接看代码
#ifndef LIST_H
#define LIST_H
template <typename T>
class List
{
private:
struct Node
{
T data;//值域
Node *prev;//前驱
Node *next;//后继
Node( const T & d = T(), Node * p = nullptr, Node * n = nullptr )
: data( d ), prev( p ), next( n )
{
}
};
public:
class const_iterator
{
public:
// 因为我们提供给client使用List::const_iterator类型,若不是基于此考虑,构造方法应该是private
const_iterator( ) : current( nullptr )
{
}
// 返回当前位置存贮的值
const T & operator* ( ) const
{
return retrieve( );
}
//++先
const_iterator & operator++ ( )
{
current = current->next;
return *this;
}
//++后
const_iterator operator++ ( int )
{
const_iterator old = *this;
++( *this );
return old;
}
//--先
const_iterator & operator-- ( )
{
current = current->prev;
return *this;
}
//--后
const_iterator operator-- ( int )
{
const_iterator old = *this;
--( *this );
return old;
}
bool operator== ( const const_iterator & rhs ) const
{
return current == rhs.current;
}
bool operator!= ( const const_iterator & rhs ) const
{
return !( *this == rhs );
}
protected:
Node *current;
T & retrieve( ) const
{
return current->data;
}
//让当前指针指向正确的位置
const_iterator( Node *p ) : current( p )
{ }
friend class List<T>;
};
class iterator : public const_iterator
{
public:
iterator( )
{
}
T & operator* ( )
{
return retrieve( );
}
const T & operator* ( ) const
{
return const_iterator::operator*( );
}
iterator & operator++ ( )
{
current = current->next;
return *this;
}
iterator operator++ ( int )
{
iterator old = *this;
++( *this );
return old;
}
iterator & operator-- ( )
{
current = current->prev;
return *this;
}
iterator operator-- ( int )
{
iterator old = *this;
--( *this );
return old;
}
protected:
iterator( Node *p ) : const_iterator( p )
{
}
friend class List<T>;
};
public:
List( )
{
init( );
}
~List( )
{
clear( );
delete head;
delete tail;
}
List( const List & rhs )
{
init( );
*this = rhs;
}
const List & operator= ( const List & rhs )
{
if( this == &rhs )
return *this;
clear( );
for( const_iterator itr = rhs.begin( ); itr != rhs.end( ); ++itr )
push_back( *itr );
return *this;
}
iterator begin( )
{
return iterator( head->next );
}
const_iterator begin( ) const
{
return const_iterator( head->next );
}
iterator end( )
{
return iterator( tail );
}
const_iterator end( ) const
{
return const_iterator( tail );
}
int size( ) const
{
return theSize;
}
bool empty( ) const
{
return size( ) == 0;
}
void clear( )
{
while( !empty( ) )
pop_front( );
}
// front, back, push_front, push_back, pop_front, and pop_back
// are the basic double-ended queue operations.
T & front( )
{
return *begin( );
}
const T & front( ) const
{
return *begin( );
}
T & back( )
{
return *--end( );
}
const T & back( ) const
{
return *--end( );
}
void push_front( const T & x )
{
insert( begin( ), x );
}
void push_back( const T & x )
{
insert( end( ), x );
}
void pop_front( )
{
erase( begin( ) );
}
void pop_back( )
{
erase( --end( ) );
}
// Insert x before itr.
iterator insert( iterator itr, const T & x )
{
Node *p = itr.current;
Node *newNode = new Node(x);
newNode->next = p;
newNode->prev = p->prev;
p->prev->next = newNode;
p->prev = newNode;
theSize++;
return iterator(newNode);
}
// Erase item at itr.
iterator erase( iterator itr )
{
Node *p = itr.current;
iterator retVal( p->next );
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
theSize--;
return retVal;
}
iterator erase( iterator start, iterator end )
{
for( iterator itr = start; itr != end; )
itr = erase( itr );
return end;
}
private:
int theSize;
Node *head;
Node *tail;
void init( )
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
};
#endif