执行顺序

通过三个指针不断的移动,从而实现逆置。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct Lnode{
  int data;
  Lnode *next;
}Lnode,*linklist;
int main(){
  linklist a,p,q;
  int n;
  while(~scanf("%d",&n)){
     a=(linklist)malloc(sizeof(Lnode));
     p=a;
     int tmp;
     for (int i=1;i<=n;i++){
        q=(linklist)malloc(sizeof(Lnode));
        scanf("%d",&tmp);
        q->data=tmp;
        p->next=q;
        p=q;
     }
     p=a->next;//指向头结点的下个,头结点为空值我们要省去哪个空的头结点
     a->next=NULL;//那么这个尾节点为指向的应该是空
     while(p!=NULL){//把p当成遍历的元素
        q=p;//保存当前p
        p=p->next;//p指向了下一个节点
        q->next=a->next;//把q指向前一个节点,这里由于我们是尾插法,我们把a->next变成q节点的后一节点
        a->next=q;//前移
     }
     p=a->next;
     while(p!=NULL){
        printf("%d\n",p->data);
        p=p->next;
     }
  }
  return 0;
}