0001 template <typename T> void List<T>::reverse() { //前后倒置 0002 if ( _size < 2 ) return; //平凡情况 0003 ListNodePosi(T) p; ListNodePosi(T) q; 0004 for ( p = header, q = p->succ; p != trailer; p = q, q = p->succ ) 0005 p->pred = q; //自前向后,依次颠倒各节点的前驱指针 0006 trailer->pred = NULL; //单独设置尾节点的前驱指针 0007 for ( p = header, q = p->pred; p != trailer; p = q, q = p->pred ) 0008 q->succ = p; //自前向后,依次颠倒各节点的后继指针 0009 header->succ = NULL; //单独设置头节点的后继指针 0010 swap ( header, trailer ); //头、尾节点互换 0011 }