猴子选大王程序,帮我注释一下,#include #include typedef struct monkey{int num;struct monkey *next;} Monkey,*LINK;LINK createList(int M) { LINK p,head1,p2;int i;head1=p=p2=(LINK)malloc(sizeof(Monkey));for(i=1;inext=p;p2=p;}p2->next=head1;p=

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/05 12:47:12

猴子选大王程序,帮我注释一下,#include #include typedef struct monkey{int num;struct monkey *next;} Monkey,*LINK;LINK createList(int M) { LINK p,head1,p2;int i;head1=p=p2=(LINK)malloc(sizeof(Monkey));for(i=1;inext=p;p2=p;}p2->next=head1;p=
猴子选大王程序,帮我注释一下,
#include
#include
typedef struct monkey
{
int num;
struct monkey *next;
} Monkey,*LINK;
LINK createList(int M)
{ LINK p,head1,p2;
int i;
head1=p=p2=(LINK)malloc(sizeof(Monkey));
for(i=1;inext=p;
p2=p;
}
p2->next=head1;
p=head1;
printf("duihouzijinxingbianhao\n");
for(i=1;inum=i;
printf("%dhaohouzi:%d\n",p->num,p->num);
p=p->next;
}
return head1;
}
int selectKing(LINK head,int N,int M)
{
LINK p,p2,head2=NULL;
int i;
i=0;
p=head;
while(1)
{
i++;
printf("%dhaohouzibao:%d\n",p->num,i);
if(p->next==p)
return p->num;
if(i==N)
{
i=0;
printf("%dhaohoubeitaotai\n",p->num);
printf("\n");
p2->next=p->next;
p=p2->next;
continue;
}
else
{
if(i==N-1)
p2=p;
p=p->next;
}
}
}
int main()
{
LINK head=NULL;
int M,N,a;
printf("shuruhouzishuliang:");
scanf("%d",&M);
printf("shuruxiaoyuzongshudeyigeshu:");
scanf("%d",&N);
head=createList(M);
a=selectKing(head,N,M);
printf("\n%dhaohoushihouwang\n",a);
return 0;
}\x1a

猴子选大王程序,帮我注释一下,#include #include typedef struct monkey{int num;struct monkey *next;} Monkey,*LINK;LINK createList(int M) { LINK p,head1,p2;int i;head1=p=p2=(LINK)malloc(sizeof(Monkey));for(i=1;inext=p;p2=p;}p2->next=head1;p=
猴子选大王都用上计算机程序了呀?真是科学,比人选总统还先进.
#include
#include
typedef struct monkey
{
int num;
struct monkey *next;
} Monkey,*LINK;
LINK createList(int M) // 创建长度为M的循环链表,每个结点对应一个猴子
{ LINK p,head1,p2;
int i;
head1=p=p2=(LINK)malloc(sizeof(Monkey));
for(i=1;inext=p;
p2=p;
}
p2->next=head1;
p=head1;
printf("duihouzijinxingbianhao\n");
for(i=1;inum=i;
printf("%dhaohouzi:%d\n",p->num,p->num);
p=p->next;
}
return head1;
}
int selectKing(LINK head,int N,int M) // 选猴王
{
LINK p,p2,head2=NULL;
int i;
i=0;
p=head;
while(1)
{
i++;
printf("%dhaohouzibao:%d\n",p->num,i);
if(p->next==p) { // 如果满足条件,说明只剩一个猴子,那他就是王了.
i = p->num;
free(p); // 原程序没有这句,会有内存泄漏
return i;
}
if(i==N)
{ // 报数报道N的猴子,出列.从链表里删除对应的结点
i=0; // 重新开始计数
printf("%dhaohoubeitaotai\n",p->num);
printf("\n");
p2->next=p->next;
free(p); // 原程序漏了这句,会导致内存泄漏
p=p2->next;
continue;
}
else
{ // 如果报数
if(i==N-1) // 如果报数是N-1,说明下一个结点(即猴子)要被删除.为了能正确删除节点,要先把前面的结点指针保存到p2
p2=p;
p=p->next;
}
}
}
int main()
{
LINK head=NULL;
int M,N,a;
printf("shuruhouzishuliang:");
scanf("%d",&M);
printf("shuruxiaoyuzongshudeyigeshu:");
scanf("%d",&N);
head=createList(M);
a=selectKing(head,N,M);
printf("\n%dhaohoushihouwang\n",a);
return 0;
}\x1a