#include template< class d_type = double > class no_lista { public: d_type elem; no_lista< d_type > * prox; no_lista< d_type > * ant; no_lista() : prox( 0 ), ant( 0 ) {} }; template< class d_type = double > class iter_lista{ public: no_lista< d_type > * curr; iter_lista( no_lista< d_type > * curr ) : curr( curr ) {} d_type operator*(){ return curr->elem; } void operator++(){ curr = curr->prox; } void operator++( int ){ curr = curr->prox; } void operator--(){ curr = curr->ant; } void operator--( int ){ curr = curr->ant; } bool valid(){ return curr != 0; } }; template< class d_type > std::ostream& operator<< ( std::ostream& os, const iter_lista< d_type >& iter ){ os << iter.curr; return os; } template< class d_type = double > class lista { public: no_lista< d_type > * ini = 0; no_lista< d_type > * fim = 0; lista() {} void push( d_type x ){ no_lista< d_type > * novo = new no_lista< d_type >; novo->elem = x; novo->prox = ini; novo->ant = 0; // Desnecessário, apenas para clareza do código if ( ini != 0 ) { ini->ant = novo; } else { fim = novo; } ini = novo; } iter_lista< d_type > begin(){ return iter_lista< d_type >( ini ); } iter_lista< d_type > end(){ return iter_lista< d_type >( fim ); } iter_lista< d_type > find( d_type x ){ auto curr = begin(); while ( curr.valid() ){ if ( *curr == x ){ break; } curr++; } return curr; } void remove( d_type x ){ auto pos = find( x ); if ( pos.valid() ){ auto curr = pos.curr; if ( curr->ant == 0 ){ ini = curr->prox; } else { curr->ant->prox = curr->prox; } if ( curr->prox == 0 ){ fim = curr->ant; } else { curr->prox->ant = curr->ant; } delete curr; } } void map( void (*f)( d_type ) ){ auto curr = begin(); while ( curr.valid() ){ f( *curr ); curr++; } } ~lista(){ auto curr = ini; while ( curr != 0 ){ auto to_del = curr; curr = curr->prox; delete to_del; } } }; int main( int argc, char ** argv ){ lista<> L; L.push( 3.14 ); L.push( 2.3 ); auto curr = L.begin(); while ( curr.valid() ){ std::cout << *curr << '\n'; curr++; } std::cout << '\n'; L.remove( 3.14 ); curr = L.begin(); while ( curr.valid() ){ std::cout << *curr << '\n'; curr++; } return 0; }