舟--C++约瑟环问题

[复制链接]
查看11 | 回复0 | 2006-3-22 13:38:24 | 显示全部楼层 |阅读模式
# include # include# define NUll 0 struct ring {
int num;
int key;
struct ring *next; }; //////////////////////////////////////////////// struct ring *creat(int person)/////////////////////////// {
struct ring *head,*p1,*p2;
head=p1=p2=(struct ring *)malloc(sizeof(struct ring));
head->next=NULL;
//head->key=head->num=1; for(int i=1;inum=i;
p2->key=i+2;
p1->next=p2;
p1=p2;
}
p1->next=head;
return head; } //////////////////////////////////////////////// void print(struct ring *head,int person)/////////////////// {
head = head->next;
coutnumkeynext;
} } //////////////////////////////////////////////// void huan(struct ring *head) {
int floop=1;
struct ring *p=head->next;
while(floop)
{
int i=1;
while(ikey-1)
{
p=p->next;
i++;
}
struct ring *a;
a=p->next;
p->next=a->next;
coutnumkeynext;
if(p->next==p)
floop=0;
free(a);
}
} /////////////////////////////////////////////// int main()///////////////////////////////////// {
struct ring *list;
int person;
cout>person;
list=creat(person);//创建
print(list,person);//输出
// int start;//开始的删除数
// cout>start;
cout<<"约瑟夫环的结果为"<<endl;
huan(list);//输入循环体 return 0; }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行