typedef struct node{
int n;
struct node*next;
node(int i=0):n(i),next(NULL){};
}Node,*PNode;
void init(PNode& head){ //十个节点 【0-9】
PNode pr,fr;
pr=fr=head=new Node();;
for(int i=1;i<10;i++){
fr=new Node(i);
pr->next=fr;
pr=fr;
}
}
void release(PNode& head){
PNode fr =head;
while(head!=NULL){
fr=head->next;
delete head;
head=fr;
}
}
void reverse_link(PNode& head){ <span style="white-space:pre"> </span>//翻转
if(head==NULL)return ;
PNode pre1,pre2;<span style="white-space:pre"> </span>//head后的两个相邻节点
pre1=head->next;
head->next=NULL;
while(pre1!=NULL){
pre2=pre1->next;
pre1->next=head;
head=pre1;
pre1=pre2;
}
}
int main(){
PNode tmp;
PNode head=NULL;
init(head);
tmp=head;
while(tmp!=NULL){ //翻转前
cout<<tmp->n<<"\t";
tmp=tmp->next;
}
cout<<endl;
reverse_link(head); //翻转
tmp=head;
while(tmp!=NULL){ //翻转后
cout<<tmp->n<<"\t";
tmp=tmp->next;
}
release(head);
return 0;
}
很久以前的一个小题,复习数据结构再来做一下。