有M个人围一圈玩报数,凡报到N的出退出,输出每次退出的人的编号。
#include
"stdio.h"
struct
game
{
int
ID
game
*pNext
}
void
main()
{
int
i,m=17,n=3
game
*pPrev,*pNode,*pTop
printf("Input
M
N:"册陆)
scanf("%d
%d",&m,&n)
if(n>m||n<1)
return
pTop=new
game
pTop->ID=1
pPrev=pTop
for(i=2i<=mi++)
{
pNode=new
game
pNode->腊激ID=i
pPrev->pNext=pNode
pPrev=pNode
}
pNode->pNext=pTop
pPrev=pNode
pNode=pTop
i=0
while(pNode->pNext!=pNode)
{
i++
if(i%n==0)
{
printf("%d
",pNode->ID)
pPrev->pNext=pNode->pNext
delete
pNode
pNode=pPrev->pNext
i=0
}
else
{
pPrev=pNode
pNode=pNode->pNext
}
}
delete
pNode
}
/************************结构体链表******************************************************/struct
student
{
unsigned
int
stu_ID
char
sex
int
age
struct
student
*st
}
struct
student
*p,*h,*s
student*
crate_list(){//建立链表,返回链表的头结点
int
i=20
unsigned
short
c
p
=
new
student
h
=
p
p->stu_ID=1000
p->sex
=
'M'
p->age
=
i
do{
s=new
student
s->stu_ID=i+1000
s->sex
=
'M'
s->age
=
i
p->st
=
s
p
=
p->st
i++
}while(i<31)
p
=
NULL
return
h
}
bool
delete_node(student*
p,int
a){//传入头结点和年龄
if(p){
student
*
temp=p,*p_pre
while(temp->age!=a){
p_pre=temp
temp=temp->st
if(!temp)
return
false
}
p_pre->st=temp->st
delete
temp
temp=NULL
return
true
}
return
false
}
void
display_list(student
*
h){//打印枯迅皮链表中的数据
student*
temp=p
while(temp->st)
cout<<temp->stu_ID<<"
"<<temp->没差sex<昌磨<"
"<<temp->age<<endl
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)