- 6
- 0
- 约1.21千字
- 约 3页
- 2017-06-07 发布于重庆
- 举报
数据结构实验报告 ----约瑟夫环
需求分析:
约瑟夫环是程序的一道经典例题,可以使用数据结构的单链表进行编写。
概要设计:
大体上可以使用单链表通过节点的创建和删除来达到人数出列的效果,可以大大缩短程序量。
详细设计:
首先定义一个单链表,然后分别做创建链表、以及对应的输入数据,删除节点对应的删除数据,以及输出功能。最后用主函数实现上述功能。
下为程序源代码:
#include
#include
typedef struct Lnode //创建一个单链表 int num;
int key;
struct Lnode *next;
Joseph;
struct Lnode *head,*p,*p1;
int creatlinklist int n //为节点分配内存,创建节点 int i 0;
head struct Lnode* malloc sizeof struct Lnode ;
if !head return 0; p head;
for i 0;i n-1;i++ p1 struct Lnode* malloc sizeof struct Lnode ;
if !p1 return 0; p- next p1;
p p1; p- next head;
p1 head;
return 0; int input int n //在分配的节点上输入数据 int i 0;
int j 0;
printf please input the keys:\n ;
for i 1;i n+1;i++ scanf %d,j ;
p1- num i;
p1- key j;
p1 p1- next; p1 p;
return j; int output int m,int n \\在约瑟夫环的规定上输出数据删除节点 int i 0;
int a 0;
for i 0;i n;i++ for a 0;a m-1;a++ p1 p1- next; p p1- next;
m p- key;
printf %d\n,p- num ;
p1- next p- next;
free p ; return 0; void main int m 0;
int n 0;
printf 请输入上限值和人数值:\n ;
scanf %d%d,m,n ;
creatlinklist n ;
input n ;
printf 出对的人:\n ;
output m,n ; 调试分析:
程序仅能使用一次,并且输入格式没有提示,容易犯错。大数据耗时比较长。
用户手册:
请根据提示输入数据。
测试结果:
附录:
此程序仅能计算一次约瑟夫环,实用性较差,且大数据耗时也比较长。
原创力文档

文档评论(0)