链表问题
代码如下:
把一个链表逆序排列,即将链表的头当链表的尾,链表的尾当链表的头
# 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如何解释,以及能否详细说明一下这个算法非常感谢
把一个链表逆序排列,即将链表的头当链表的尾,链表的尾当链表的头
# 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如何解释,以及能否详细说明一下这个算法非常感谢
...全文
相关推荐
Josephus问题C++单循环链表实现
这个完全符合课本上习题的要求,我作业交的就是这个,我自己还在里面做了详细的注释
josephu问题 单链表 双链表
josephu问题 单链表 双链表 分别实现
报数问题的链表求解
13人围成一圈,从第一个人开始报数1,2,3,报到三的人推出圈子,找出最后留在圈内的人
链表解决的约瑟夫问题
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。
顺序链表,单链表,静态链表求解的约瑟夫问题josephus的C语言代码,可以直接运行
用顺序链表,单链表,静态链表三种结构求解的约瑟夫josephus问题,完整的代码,可以直接编译,运行的,


