猴子选大王程序,帮我注释一下,#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/06/28 09:43:10
![猴子选大王程序,帮我注释一下,#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=](/uploads/image/z/2530864-64-4.jpg?t=%E7%8C%B4%E5%AD%90%E9%80%89%E5%A4%A7%E7%8E%8B%E7%A8%8B%E5%BA%8F%2C%E5%B8%AE%E6%88%91%E6%B3%A8%E9%87%8A%E4%B8%80%E4%B8%8B%2C%23include+%23include+typedef+struct+monkey%7Bint+num%3Bstruct+monkey+%2Anext%3B%7D+Monkey%2C%2ALINK%3BLINK+createList%28int+M%29+%7B+LINK+p%2Chead1%2Cp2%3Bint+i%3Bhead1%3Dp%3Dp2%3D%28LINK%29malloc%28sizeof%28Monkey%29%29%3Bfor%28i%3D1%3Binext%3Dp%3Bp2%3Dp%3B%7Dp2-%3Enext%3Dhead1%3Bp%3D)
猴子选大王程序,帮我注释一下,#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