链表反序的递归算法

下面给出链表的反序的c语言描述的算法

#include "stdio.h"
typedef struct strtest
{
   int a;
   struct strtest *next;
} STRA ;
void revlist(STRA **h, STRA *p)
{
STRA *q;
 if(p->next!=NULL)
 {
  revlist(h,p->next);
  q=p->next;
  q->next=p;
  p->next=NULL;
 }
 else
 {
  *h=p;
  
 }
}
main()
{
  int a[10];
  int i;
  STRA *h=NULL,*p,*q;
  for (i=0;i<10;a[i]=i,i++);
  for (i=0;i<10;i++)
  {
   q=(STRA *)malloc(sizeof(STRA));
   q->a=a[i];
   q->next=NULL;
   if(!h)
   {
     h=q;
     p=h;
    }
 p->next=q;
   p=q;
  }
  revlist(&h,h);
  p=h;
  while(p)
  {
 printf("%d ",p->a);
   p=p->next;
  }
}


你可能感兴趣的:(c,算法,struct,null,语言)