杭电数据结构实习报告1
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实习报告
题目:约瑟夫环
班级:姓名:学号:日期:
一、需求分析
1、编号为1,2,...,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。
一开始任选一个正整数作为报数上限值m,从第一个人开始按顺时针方向自1开始报数,报到m时停止报数。
报m的人出列,将他的密码作为新的m 值,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。
试设计一个程序求出出列顺序。
2、利用单向循环链表存储结构模拟此过程,按照出列的顺序印出各人的编号。
3、程序运行后,首先要求用户指定初始报数上限值,然后读取各人的密码。
可设n不大于30。
4、测试数据
m的初值为20,;n=7,7个人的密码依次为:3,1,7,2,4,8,4,首先m值为6(正确的出列顺序应为6,1,4,7,2,3,5)。
二、详细设计
struct Lnode
{
int number;
int password;
struct Lnode *next;
}Lnode,*p,*q,*head; \\定义单向链表结构
int main(void)
{
int n;
int i;
int m;
int j;
printf("please enter the number of people n:");
scanf("%d",&n);
if(n<=0||n>30)
{printf("\n n is erorr!\n\n");
printf("please enter the number of people again n:");
scanf("%d",&n);} \\输入不大于30的人数n
for(i=1;i<=n;i++)
{if(i==1)
{head=p=(struct Lnode*)malloc(sizeof(struct Lnode)); }
else
{q=(struct Lnode*)malloc(sizeof(struct Lnode));
p->next=q;
p=q;
} \\创建n个结点
printf("please enter the %d people's password:",i);
scanf("%d",&(p->password));
p->number=i; } \\输入密码
p->next=head;
p=head;
printf("please enter the number m:");
scanf("%d",&m);
printf("The password is:\n");
for (j=1;j<=n;j++) \\循环报数,输出序号{for(i=1;i<m;i++,p=p->next);
m=p->password;
printf("%d ",p->number);
p->number=p->next->number;
p->password=p->next->password;
q=p->next;
p->next=p->next->next;
free(q);
}
printf("\n\n");
}
三、测试结果
please enter the number of people n:7
please enter the 1 people's password:3
please enter the 2 people's password:1
please enter the 3 people's password:7
please enter the 4 people's password:2
please enter the 5 people's password:4
please enter the 6 people's password:8
please enter the 7 people's password:4
please enter the number m:20
the password is:
6 1 4
7 2 3 5。