📄 cs_list.h
字号:
return my_head->_element;
}
/****************************************************************************/
template <class element>
inline const element &
list<element>::tail () const
{
assert ( my_tail );
// tail() - list is empty
return my_tail->_element;
}
/****************************************************************************/
template <class element>
void
list<element>::add_to_head ( const element &the_element )
{
node *newnode = new node;
check_mem ( newnode );
newnode->_element = the_element;
newnode->_previous = NULL;
newnode->_next = my_head;
if ( my_size++ )
my_head->_previous = newnode;
else
my_tail = newnode;
my_head = newnode;
}
/****************************************************************************/
template <class element>
void
list<element>::add_to_tail ( const element &the_element )
{
node *newnode = new node;
check_mem ( newnode );
newnode->_element = the_element;
newnode->_previous = my_tail;
newnode->_next = NULL;
if ( my_size++ )
my_tail->_next = newnode;
else
my_head = newnode;
my_tail = newnode;
}
/****************************************************************************/
template <class element>
void
list<element>::add_to_tail ()
{
node *newnode = new node;
check_mem ( newnode );
newnode->_previous = my_tail;
newnode->_next = NULL;
if ( my_size++ )
my_tail->_next = newnode;
else
my_head = newnode;
my_tail = newnode;
}
/****************************************************************************/
template <class element>
void
list<element>::remove_head ()
{
assert ( my_iterator_count == 0 );
//error - iterators_present
node *old_head = my_head;
if ( old_head )
{
if ( old_head->_next )
{
old_head->_next->_previous = NULL;
my_head = old_head->_next;
}
else
my_head = my_tail = NULL;
delete old_head;
my_size--;
}
}
/****************************************************************************/
template <class element>
void
list<element>::remove_tail ()
{
assert ( my_iterator_count == 0 );
//error - iterators_present
node *old_tail = my_tail;
if ( old_tail )
{
if ( old_tail->_previous )
{
old_tail->_previous->_next = NULL;
my_tail = old_tail->_previous;
}
else
my_head = my_tail = NULL;
delete old_tail;
my_size--;
}
}
/****************************************************************************/
template <class element>
element &
list<element>::operator[] ( int index )
{
assert ( index >= 0 && index < my_size ); //error - invalid_index;
register node *n = my_head;
for ( register int i = 0; i < index; i++ )
n = n->_next;
return n->_element;
}
/****************************************************************************/
template <class element>
const element &
list<element>::operator[] ( int index ) const
{
assert ( index >= 0 && index < my_size ); //error - invalid_index;
register node *n = my_head;
for ( register int i = 0; i < index; i++ )
n = n->_next;
return n->_element;
}
/****************************************************************************/
template <class element>
inline int
list<element>::size () const
{
return my_size;
}
/****************************************************************************/
template <class element>
inline bool
list<element>::is_empty () const
{
return ( my_size == 0 );
}
/****************************************************************************/
template <class element>
void
list<element>::clear ()
{
register node *nextnode;
assert ( my_iterator_count == 0 );
//clear() - cannot clear, iterators present
for ( register node *n = my_head; n; n = nextnode )
{
nextnode = n->_next;
delete n;
}
my_size = 0;
my_head = my_tail = NULL;
}
/****************************************************************************/
template <class element>
list<element>
list<element>::all_such_that ( bool ( *f ) ( const element & ) ) const
{
list<element> new_list;
for ( register const node *n = my_head; n; n = n->_next )
if ( f ( n->_element ) )
new_list += n->_element;
return new_list;
}
/****************************************************************************/
/*
template <class element>
void
list<element>::error ( const char *msg )
{
cerr << "list: " << msg << '\n';
assert ( 0 );
}
*/
/****************************************************************************/
template <class element>
inline
const_iterator<element>::const_iterator ()
{
my_list = NULL;
my_current = NULL;
}
/****************************************************************************/
template <class element>
inline
const_iterator<element>::const_iterator ( const list<element> &the_list )
{
my_list = &the_list;
// even though list is a const, update its iterator count.
( (list<element> &) the_list ).my_iterator_count++;
my_current = the_list.my_head;
}
/****************************************************************************/
template <class element>
inline
const_iterator<element>::const_iterator (
const const_iterator<element> &the_iterator )
: my_list ( the_iterator.my_list )
{
if ( my_list )
( (list<element> *) my_list )->my_iterator_count++;
my_current = the_iterator.my_current;
}
/****************************************************************************/
template <class element>
inline
const_iterator<element>::~const_iterator ()
{
if ( my_list )
( (list<element> *) my_list )->my_iterator_count--;
}
/****************************************************************************/
template <class element>
inline const_iterator<element>&
const_iterator<element>::iterate_over ( const list<element> &the_list )
{
if ( my_list )
( (list<element> *) my_list )->my_iterator_count--;
my_list = &the_list;
( (list<element> *) my_list )->my_iterator_count++;
my_current = my_list->my_head;
return *this;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::iterate_over ()
{
if ( my_list )
{
( (list<element> *) my_list )->my_iterator_count--;
my_list = NULL;
my_current = NULL;
}
return *this;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::operator= (
const const_iterator<element> &the_iterator )
{
if ( my_list )
( (list<element> *) my_list )->my_iterator_count--;
my_list = the_iterator.my_list;
if ( my_list )
( (list<element> *) my_list )->my_iterator_count++;
my_current = the_iterator.my_current;
return *this;
}
/****************************************************************************/
template <class element>
inline const element &
const_iterator<element>::current () const
{
assert ( my_current );
//error - no_current_el;
return my_current->_element;
}
/****************************************************************************/
template <class element>
inline const element *
const_iterator<element>::ptr () const
{
if ( my_current )
return &my_current->_element;
else
return NULL;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::beginning ()
{
if ( my_list )
my_current = my_list->my_head;
return *this;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::end ()
{
if ( my_list )
my_current = my_list->my_tail;
return *this;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::operator++ () // prefix
{
assert ( my_current );
//error - no_current_el;
my_current = my_current->_next;
return *this;
}
/****************************************************************************/
template <class element>
inline const_iterator<element> &
const_iterator<element>::operator-- () // prefix
{
assert ( my_current );
//error - no_current_el;
my_current = my_current->_previous;
return *this;
}
/****************************************************************************/
template <class element>
inline void
const_iterator<element>::operator++ (int) // postfix
{
assert ( my_current );
//error - no_current_el;
my_current = my_current->_next;
}
/****************************************************************************/
template <class element>
inline void
const_iterator<element>::operator-- (int) // postfix
{
assert ( my_current );
//error - no_current_el;
my_current = my_current->_previous;
}
/****************************************************************************/
template <class element>
inline const element &
const_iterator<element>::operator* () const
{
assert ( my_current );
//error - no_current_el;
return my_current->_element;
}
/****************************************************************************/
template <class element>
inline const element *
const_iterator<element>::operator-> () const
{
assert ( my_current );
//error - no_current_el;
return &my_current->_element;
}
/****************************************************************************/
template <class element>
inline bool
const_iterator<element>::at_beginning () const
{
return ( my_current && !my_current->_previous );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -