链表问题
代码如下:
把一个链表逆序排列,即将链表的头当链表的尾,链表的尾当链表的头
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct stu
{
int num;
struct stu * next;
}
main()
{
int len =1 , i ;
struct stu *p1, *p2,*head, *new ,*newhead;
//建立链表
p1=p2=head = (struct stu * ) malloc ( sizeof (struct stu ) ) ;
printf("Input number ( 0: list end ) : ");
scanf( "%d",&p1->num);
while ( p1->num != 0 )
{
p1 = ( struct stu * ) malloc ( sizeof ( struct stu ) ) ;
printf("Input number ( 0 :list end ): " );
scanf("%d",&p1->num );
if(p1->num == 0 )
p2->next = NULL ;
else
{
p2->next = p1;
p2 = p1;
len ++ ;
}
}
// 输出原链表
p1=head;
printf("\n the orginal list :\n");
do
{
printf("%4d",p1->num);
if(p1->next !=NULL )
p1 =p1->next ;
}while ( p1->next != NULL ) ;
printf("%4d",p1->num);
//输出新链表
for( i=0;i<len;i++)
{
p2=p1=head;
while (p1->next != NULL )
{
p2 =p1;
p1 =p1->next;
}
if( i == 0 )
newhead = new = p1;
else
new= new->next =p1;
p2 ->next = NULL;
}
printf("\n THhe list :\n");
p1= newhead;
for( i=0;i<len; i++)
{
printf("%4d",p1->num);
p1=p1->next;
}
printf("\n");
}
问题如下:
在输出链表部分的for如何解释,以及能否详细说明一下这个算法非常感谢