数据结构中的一个有关问题。实现基于链表的线性表中数据的倒置输出

数据结构中的一个问题。实现基于链表的线性表中数据的倒置输出。
[code=C/C++][/code]
#include<iostream>
using namespace std;
template <class Elem>
class Link{
public:
Elem element;
Link*next;
Link(const Elem& elemval,Link* nextval=NULL)
{
element=elemval;
next=nextval;
}
Link(Link* nextval=NULL)
{
next=nextval;
}
};

template <class Elem>
class LList:public Link<Elem>{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init(){
fence=head=tail=new Link<Elem>;
leftcnt=rightcnt=0;
}
void removeall(){
while(head!=NULL){
fence=head;
head=head->next;
delete fence;
}
}
public:
LList(int DefaultListSize){
init();
}
~LList(){
removeall();
}
void clear(){
removeall();
init();
}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart(){
fence=head;
rightcnt+=leftcnt;
leftcnt=0;
}
void setEnd(){
fence=tail;
leftcnt+=rightcnt;
rightcnt=0;
}
void prev();
void next(){
if(fence!=tail){
fence=fence->next;
leftcnt++;
righgtcnt--;
}
}
int leftLength() const{
return leftcnt;
}
int rightLength() const{
return rightcnt;
}
bool setPos(int pos);
bool getValue(Elem& it) const{
if(rightcnt==0) return false;
it=fence->next->element;
return true;
}
void print(Link<Elem>* temp=head) const;
//新增的成员函数:实现倒置线性表元素的顺序的功能
bool reset(Link<Elem> * &newhead);
};
template<class Elem>
bool LList<Elem>::insert(const Elem& item){
fence->next=new Link<Elem>(item,fence->next);
if(fence==tail) tail=fence->next;
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::append(const Elem& item){
tail=tail->next=new Link<Elem>(item);
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::remove(Elem& item){
if(fence->next==NULL) return false;
item=fence->next->element;
Link<Elem>* Itemp=fence->next;
fence->next=Itemp->next;
if(tail==Itemp) tail=fence;
delete Itemp;
rightcnt--;
return true;
}
template<class Elem>
void LList<Elem>::prev(){
Link<Elem>* Temp=head;
if(fence==head) return;
while(Temp->next!=fence)
Temp=Temp->next;
fence=temp;
leftcnt--;
rightcnt++;
}
template<class Elem>
bool LList<Elem>::setPos(int pos){
if(pos<0||pos>rightcnt+leftcnt) return false;
fence=head;
for(int i=0;i<pos;i++)
fence=fence->next;
rightcnt=rightcnt+leftcnt-pos;
leftcnt=pos;
return true;
}
template<class Elem>
void LList<Elem>::print(Link<Elem>* temp) const{
cout<<"<";
while(temp!=fence){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<"|";
while(temp->next!=NULL){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
template<class Elem>
bool LList<Elem>::reset(Link<Elem>* & newhead){
Link<Elem>* ptr=head;
Link<Elem>* sp;
while(ptr!=NULL){
if(newhead==NULL)
newhead=head;
else{
sp=new Link<Elem>();
sp=ptr;
sp->next=newhead;
newhead=sp;
}
ptr=ptr->next;