倒置线性表中数据的顺序(c++)

倒置线性表中数据的顺序(c++)

给LList类实现添加一个成员函数,倒置线性表中数据的顺序,且算法的运行时间为O(n)

template<typename E>
void LList<E>::reverse(){
	Link<E> * p,* q;  //Temporary node
	p = head -> next ;
	tail = p;
	head->next = NULL ;
	while( p ) { //Traverse the list until it is empty
		q =p ;
		p = p -> next ;
		q -> next = head -> next ; //insert to the head
		head ->next = q;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43667986/article/details/84927553