求教C语言单项链表逆置NODE * fun(NODE *h)函数每步步骤详细解读

#include
#include
#define N 5
typedef struct node {
int data;
struct node next;
} NODE;
/
*********found**********/
NODE * fun(NODE h)
{ NODE *p, *q, *r;
p = h;
if (p == NULL)
return NULL;
q = p->next;
p->next = NULL;
while (q)
{
/
*********found**********/
r = q->next;
q->next = p;
p = q;
/**********found**********/
q = r;
}
return p;
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h=NULL;
for(i=0; i { _q=(NODE *)malloc(sizeof(NODE));_
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p=h;
if (p==NULL) printf("The list is NULL!\n");
else
{ printf("\nHead ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}
main()
{ NODE *head;
int a[N]={2,4,6,8,10};
head=creatlist(a);
printf("\nThe original list:\n");
outlist(head);
head=fun(head);
printf("\nThe list after inverting :\n");
outlist(head);
}

 #include  <stdio.h>
#include  <stdlib.h>
#define    N    5 //数组长度 5
typedef struct node { //定义节点
  int  data; //数据
  struct node  *next; //下一个节点
} NODE;
/**********found**********/
NODE * fun(NODE *h) //插入函数
{ NODE  *p, *q, *r;
  p = h; // p指向头节点
  if (p == NULL) //如果为空,返回,不处理
    return NULL;
  q = p->next; // q指向p的下一个节点
  p->next = NULL;// 的下一个节点为空
  while (q) // 循环,直到q为null
  {
/**********found**********/
    r = q->next; // 让r指向q的下一个,
    q->next = p; // q的下一个为p
    p = q; // p为q
/**********found**********/
    q = r; //q为r,经过这几步,p被插入到链表里
  }
  return  p;
}
NODE *creatlist(int  a[]) //根据数组a创建链表
{  NODE  *h,*p,*q;        int  i;
   h=NULL;
   for(i=0; i<N; i++)
   {  _q=(NODE *)malloc(sizeof(NODE));_ //分配内存
      q->data=a[i]; // 数据复制
      q->next = NULL; //下一个节点为空
      if (h == NULL)  h = p = q; //如果头节点为空,都为空
      else    {  p->next = q;  p = q;   } // 把q插入
   }
   return  h; //返回头节点
}
void outlist(NODE  *h) //输出链表
{  NODE  *p;
   p=h;
   if (p==NULL)  printf("The list is NULL!\n"); //链表为空
   else
   {  printf("\nHead  "); //输出链表头
      do //循环
      {  printf("->%d", p->data); p=p->next;  } //打印链表当前元素,并且指向下一个元素
      while(p!=NULL); // 直到链表节点为null
      printf("->End\n"); //输出结束
  }
}
main()
{  NODE  *head; //定义头节点
   int  a[N]={2,4,6,8,10}; //数组
   head=creatlist(a); //根据数组得到链表
   printf("\nThe original list:\n"); //输出原始链表
   outlist(head); //输出
   head=fun(head); //插入节点
   printf("\nThe list after inverting :\n");
   outlist(head); //输出插入后的链表
}