1、代码 1:#include using namespace std;typedef struct Nodeint data;struct Node * next;Lnode,*LinkList;LinkList Creat(int num)LinkList L;Lnode *s,*r,*head;int i=1;L=r=NULL;while(1)s=new Lnode;s-data=i;i+;if(L=NULL) L=s;head=L;elser-next=s;r=s;if(i=num+1)break;r-next=head;return L;int out(LinkList L,int nu
2、m,int step)Lnode *p,*s,*n;p=L;for(int i=1;inext;coutdatanext=p-next;/把当前的人前的人和当前的人后的人连上.p=p-next;/下次从当前的人的下一个人开始数delete n;/把这个人删除(就是释放这块内存)/*int i=0;while(1)for(int j=0;jnext;s=p;p=p-next;s=p-next;coutdatanext;p=s;p=p-next;s-next=p-next;coutdata;delete p;int main()LinkList L;int num,step;cinnumstep;
3、L=Creat(num);out(L,num,step);/get(L,step);return 0; 代码 2:(很好)# include # include struct stuint num;struct stu *next;int m,n;struct stu *p,*q,*s;struct stu *create()struct stu *head,*s1,*r;int i;head=NULL;for(i=1;inum =i;if(head=NULL)head=s1;else r-next =s1;r=s1;if(r) r-next =head;return head;struct
4、stu * get_index(struct stu *head,int k)int j=1;if (knext ;j+;return s;void f1 (struct stu *head,struct stu *h)int x,i;x=m/2;p=h;i=1;printf (“依次出列的人的编号为:“);while (p-next!=p)q=p-next;for (i=1;inext;q=p-next;p-next =q-next ;printf (“%d “,q-num);free(q);p=p-next ;printf (“最后剩下的人的编号为%dn“,p-num );void f2
5、(struct stu *head,struct stu *h)int i,x;x=m/2;i=1;p=h;printf (“依次出列的人的编号为:“);dofor (i=1;inext ;p=q-next ;q-next =p-next ;printf (“%d “,p-num);free(p);p=q-next ;while (q-next!=q);printf (“n最后剩下的人的编号为%dn“,q-num);void main ()int k;struct stu *head,*h;printf (“请输入总人数:“);scanf (“%d“,printf (“请输入退出圈子的人的编号
6、:“);scanf(“%d“,printf (“请输入开始报数的人的编号:“);scanf (“%d“,head=create ();h=get_index(head,k);if (m%2=0) f1(head,h);else f2(head,h); 200分求高手周五前写出约瑟夫环实验报告,很急,谢谢悬赏分:200 - 解决时间:2009-4-4 04:04 问题描述约瑟夫问题的一种描述是:编号为 1,2,n的 n个人按顺时针方向围坐一圈,每人持有一个密码(正整数) 。一开始任选一个正整数作为报数上限值 m,从第一个人开始按顺时针方向自 1开始顺序报数,报到 m时停止报数。报 m的人出列,将
7、他的密码作为新的 m值,从他在顺时针方向上的下一个人开始重新从 1报数,如此下去,直至所有人全部出列为止。试设计一个程序求出出列顺序。基本要求利用单向循环链表存储结构模拟此过程,按照出列的顺序印出各人的编号。实现提示程序运行后,首先要求用户指定初始报数上限值,然后读取各人的密码。可设n30。此题所用的循环链表中不需要“头结点” ,请注意空表和非空表的界限。实验报告格式一、 需求分析(一定要写)二、 概要设计(详细写)三、 详细设计(即程序代码,一定要有注释)四、 调试分析(很重要)大概内容:程序一次写下来肯定会有各种错误 通过你自己调试,找出错误,知道正常编译运行 这个过程就是你怎么调怎么写嘛
8、 比如编译报了什么错,然后你是怎么改正的,如何提高运行效率.写出程序调试过程中的感想,等等五、 测试结果(几组数据,常规数据和边缘数据)源程序请用 C语言写,然后报告写成 word的格式,发到 ,请各位大哥不要再只给我源程序了(源程序最好不要有 warning错误) ,拜托了,很急的,谢谢,还有,求管理员别再删了,我都付出 920分了,谢谢啊。 提问者: aaaazzzzpo - 六级最佳答案数据结构实验报告:编制一个 Joseph约瑟夫环示例程序一、 需求分析1. 输入的形式和输入值的范围本程序中,输入报数上限值 m和人数上限 l,密码,均限定为正整数,输入的形式为一个以“回车符”为结束标志
9、的正整数。2. 输出的形式从屏幕显示出列顺序。3. 程序功能提供用户从键盘输入,Joseph 约瑟夫环的必要数据,并显示出列顺序。4. 测试数据(1)输入2073 1 7 2 4 8 4输出6 1 4 7 2 3 5二、 概要设计以单向循环链表实现该结构。1. 抽象数据类型的定义为:ADT LNode数据对象:D=ai | aiCharSet,i= 1,2,n,n0数据关系:R1= | ai D, I=2,n基本操作:InitList(typedef int ElemType;/*定义类型*/typedef struct LNodeElemType data;struct LNode * ne
10、xt;LNode, *LinkList;/*1初始化,置表头指针为空*/void InitList(LinkList HL-next = HL;/*3返回长度,空则返回 0*/int ListLength(LinkList HL)int i=0;LinkList p;p = HL-next;while(p != HL)i+;p = p-next;return i;/*5返回指定位置元素的值,超出则停止运行*/ElemType GetElem(LinkList HL,int pos)略/*6遍历所有元素*/void TraverseList(LinkList HL)LinkList p;p =
11、HL-next;if(p = HL)printf(“空拉!n“);exit(1);while(p != HL)printf(“%-5d“, p-data);p = p-next;printf(“n“);/*8修改某位置的元素值,成功返回 1,否则返回 0*/int UpdatePosList(LinkList HL,int pos,ElemType x)略/*10插入元素到表尾*/void InsertLastList(LinkList* HL,ElemType x)略/*主函数*/void main()int a30;/ = 3, 1, 7, 2, 4, 8, 4;int m, n, l;L
12、inkList p;InitList(p);doprintf(“Input upper limit number (m): “);scanf(“%d“, getchar();printf(“Input upper limit people (n): “);scanf(“%d“, getchar();while(m 0; n-)if(m != n else if(m = n)c = n;else if(m % n = 0)c = 1;while(c 0)if(s + 1 = l)s = l;elses = (+s) % l;if(GetElem(p, s) != -1)c-;printf(“%-
13、5d“, s);m = GetElem(p, s);UpdatePosList(p, s, -1);printf(“n*End*n“);四、 调试分析程序的编写和调试基本正常。遇到的问题主要是:指针的指向的边界问题。本实验采用数据抽象的与模块化程序设计方法。思路清晰,实现时调试顺利,各模块具有很好的可重用性,得到了一次良好的程序设计训练。五、 用户使用说明如何使用,详细步骤根据提示输入学生人数和学生信息示例:Input upper limit number (m): 20Input upper limit people (n): 7Input peoples password: 3 1 7 2
14、 4 8 4六、 测试结果测试结果,输入输出。完整严格。输入字母与负数 如:a,-1提示继续输入正确的值:Input upper limit number (m):输入正确的值得:Input upper limit number (m): 20Input upper limit people (n): 7Input peoples password: 3 1 7 2 4 8 4*Order*6 1 4 7 2 3 5*End*Input upper limit number (m): 5Input upper limit people (n): 5Input peoples password:
15、 1 2 3 4 5*Order*5 1 2 4 3*End* #include “Stdio.h“#include “Conio.h“#include“malloc.h“typedef struct nodeint num;int m;struct node *link; node;int c;void JOSEPH(int n,int c)node *p,*r,*list;int i;for (i=1;inum=i;printf(“nplease input the passwarld:“);scanf (“%d“,if (i=1)list=p ;elser-link=p;r=p;p-li
16、nk=list;p=list;while(p-link!=p)for (i=1;ilink;printf (“%dn“,p-num);c=p-m;r-link=p-link;free(p);p=r-link;printf(“%d“,p-num);int main(void)int n;printf(“please input the frist passwarld:“) ;scanf(“%d“,printf(“please input the number of n:“);scanf (“%d“,JOSEPH( n, c) ;getch(); #includetypedef struct aj
17、oseint num;int code;struct ajose *next;joseph;joseph *creat(void)joseph *head,*p1,*p2;int i=0,code;head=(joseph *)malloc(sizeof(joseph);p2=p1=(joseph *)malloc(sizeof(joseph);head-next=NULL;printf(“nplease input 1s code(end with 0):n“);scanf(“%d“,while(code) i+;p1=(joseph *)malloc(sizeof(joseph);p1-n
18、um=i;p1-code=code;if(i=1)head-next=p1;elsep2-next=p1;p2=p1;printf(“please input 1s code(end with 0):n“);scanf(“%d“,p2-next=head-next;head-num=i;printf(“ncreat success!n“);return head;void game(joseph *head,int m)joseph *p1,*p2;int i=1;p2=head;p1=head-next;while(head-num1)if(i=m)p2-next=p1-next;m=p1-
19、code;i=1;printf(“%3d“,p1-num);free(p1);p1=p2-next;head-num-;continue;elsei+;p2=p1;p1=p1-next; printf(“%3d“,p1-num);return ; void main()joseph *head;int m;head=creat();printf(“nplease input m:“);scanf(“%d“,game(head,m);getch();代码 4:#include #include typedef struct LPeopleint num;struct LPeople *next;
20、peo;void Joseph(int n,int m) /用循环链表实现int i,j;peo *p,*q,*head;head=p=q=(peo *)malloc(sizeof(peo);p-num=0;p-next=head;for(i=1;inum=i;q-next=p;p-next=head;q=p;p=p-next;i=0;j=1;while(inext=p-next;p=q-next;printf(“%4d“,j%n);i+;else q=p;p=p-next;j+;printf(“n“);void main()int n,m; /*人的个数*/printf(“Please Enter n amd m(nm):n“);scanf(“%d%d“,Joseph(n,m);