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