链表问题

kinglonghr kinglonghr 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
把一个链表逆序排列,即将链表的头当链表的尾,链表的尾当链表的头

# 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如何解释,以及能否详细说明一下这个算法非常感谢
...全文
评论 5
收藏 已收藏
分享
返回顶部
kinglonghr
C语言 70,039 成员 243,237 内容