单链表选首领问题
#include <stdio.h>#include <stdlib.h>
struct node
{
int code;
struct node *next;
}NODE,*LinkList;
LinkList create_list(int n)
{
LinkList head,p;
int i;
head=(node*)malloc(sizeof(node));
head->code=1;head->next=head;
for(i=n;i>1;--i)
{
p=(node*)malloc(sizeof(node));
p->code=i;p->next=head->next;head->next=p;
}
return head;
}
void output(LinkList head)
{
LinkList p;
p=head;
do{
printf("%4d",p->code);p=p->next;
}while(p!=head);
printf("\n");
}
void play(LinkList head,int n)
{
LinkList p,q;
int c=1,k;
p=head;k=n;
while(k>1)
{
if(c==2)
{
q=p->next;p->next=q->next;printf("%4d is out!",q->code);free(q);c=0;k--;
}
else
{
c++;p=p->next;
}
}
printf("\n%4d was the winner.",p->code);
}
void main()
{
LinkList head;
int n;
printf("input the number of players:");scanf("%d",&n);
head=create_list(n);
if(head)
{
output(head);
play(head,n);
}
}
按书上差不多的写的,麻烦帮我看看有什么问题,我运行不了
[ 本帖最后由 caochuan2000 于 2009-11-3 17:36 编辑 ]