#include <cstdlib>
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
};
void fo(node *list) //輸出
{
while(list!=NULL)
{
cout<<list->data<<endl;
list=list->next;
}
}
node* invertList (node *oldList)
{
node *newList,*temp;
newList=NULL;
while(oldList!=NULL)
{
temp=oldList->next; //暫存,以便於之後更新oldList
oldList->next=newList; //將切斷的oldnode接到新的List
newList=oldList; //更新newList
oldList=temp; //更新oldList
}
return newList;
}
void invertListByAdress (node **oldList)
{
node *newList,*temp;
newList=NULL;
while(*oldList!=NULL)
{
temp=(*oldList)->next;
(*oldList)->next=newList;
newList=*oldList;
*oldList=temp;
}
*oldList=newList;
}
void invertListByRef (node* &oldList)
{
node *newList,*temp;
newList=NULL;
while(oldList!=NULL)
{
temp=oldList->next;
oldList->next=newList;
newList=oldList;
oldList=temp;
}
oldList=newList;
}
int main(int argc, char *argv[])
{
node *a=new node,*b = new node, *c=new node;
node *first=a;
a->data=10;
b->data=70;
c->data=30;
a->next=b;
b->next=c;
c->next=NULL;
fo(first);
//first=invertList(first);
//invertListByAdress(&first);
invertListByRef(first);
cout<<"--------"<<endl;
fo(first);
system("PAUSE");
return EXIT_SUCCESS;
}