叶革质的树木:joseph环

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 17:26:14
任务:编号是1,2,……,n的n个人按照顺时针方向围坐一圈,每个人只有一个密码(正整数)。一开始任选一个正整数作为报数上限值m,从第一个仍开始顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人开始重新从1报数,如此下去,直到所有人全部出列为止。设计一个程序来求出出列顺序。
要求:利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。
测试数据:
m的初值为20,n=7 ,7个人的密码依次为3,1,7,2,4,7,4,首先m=6,则正确的输出是什么?
要求:
输入数据:建立输入处理输入数据,输入m的初值,n ,输入每个人的密码,建立单循环链表。
输出形式:建立一个输出函数,将正确的输出序列

typedef struct LNode
{
int password; //密码
int No; //序号
struct LNode *next; //下一成员指针
}member; //成员结构体

typedef int status;

#define OVERFLOW -2
#define OK 1
#define ERROR 0

#include <stdio.h>
#include <stdlib.h>

status CreateList_Circle(member **,int);
status DeleteNode(member **);

status main()
{
int n,m;
member *head=NULL,*p=NULL; //头指针即首成员地址,遍历指针p
printf ("Please enter number of people:\n");
scanf ("%d",&n); //总成员数
while (n<=0)
{
printf ("n must be positive, please enter again:\n");
scanf ("%d",&n);
}
if(!CreateList_Circle(&head,n)) //创建循环链表,返回头指针head
return OVERFLOW;
printf ("Please enter initial m:\n");
scanf ("%d",&m); //初始m
while (m<=0)
{
printf ("m must be positive, please enter again:\n");
scanf ("%d",&m);
}
printf ("\nThe order is:\n");
p=head;
while (n>=2) //寻找出列成员
{
int i;
m=(m%n==0)?n:m%n; //化简m值
for (i=1;i<m;i++)
p=p->next; //p指向出列成员
printf ("%d\n",p->No); //输出出列成员序号
m=p->password; //修改m
DeleteNode(&p); //删除链表中的出列成员
n--; //成员数自减
}
printf ("%d\n",p->No); //输出最后一个成员序号
return OK;
}

status CreateList_Circle(member **p_head,int n)
{
//此算法创建一个无头结点的循环链表,结点数n,*p_head返回链表头指针即首结点地址
int i;
member *tail,*p;
*p_head=(member *)malloc(sizeof(member));
if (!(*p_head)) return OVERFLOW;
(*p_head)->No=1; //储存成员一序号
printf ("Please enter password of No. 1:\n");
scanf ("%d",&(*p_head)->password); //储存成员一密码
tail=*p_head;
tail->next=NULL;
for (i=2;i<n+1;i++)
{
p=(member *)malloc(sizeof(member));
if (!p) return OVERFLOW;
p->No=i; //储存成员序号
printf ("Please enter password of No. %d:\n",i);
scanf("%d",&(p->password)); //储存成员密码
tail->next=p;
tail=p;
}
tail->next=*p_head;
return OK;
}

status DeleteNode(member **pp)
{
//此算法删除链表中的结点*pp,操作实质是将*pp下一结点复制到*pp后将其free
member *temp;
(*pp)->password=((*pp)->next)->password;
(*pp)->No=((*pp)->next)->No;
temp=(*pp)->next;
(*pp)->next=(*pp)->next->next;
free(temp);
return OK;
}

include<stdio.h>
typedef struct Node
{
int number;
int cipher;
struct Node *next;
}node,*hu;
hu H;//定义头结点为H;
init(int n)
{
int i;
int cipher;
hu L;
if(n>=1)
{
scanf("%d",&cipher);
H=(hu)malloc(sizeof(node));//生成头结点;
H->number=1;
H->cipher=cipher;
H->next=H;
for(i=1;i<n;i++)
{
scanf("%d",&cipher);
L=(hu)malloc(sizeof(node));//生成副结点;
L->number=i+1;
L->cipher=cipher;
L->next=H->next;
H->next=L;
H=L;
}
H=H->next;//循环单链表的生成;
}
else
printf("The N's value that you inputted is invalid!");
}
Joseph(int m,hu h)//进行程序的循环,使顺序出列;
{
int i;
hu l;
l==h;
i=1;
while(i!=m)
{
i=i+1;
l=h;
h=h->next;
}
printf("%3d",h->number);
m=h->cipher;
l->next=h->next;
free(h);
h=l->next;
if(h!=l)
Joseph(m,h);
else
{
printf("%3d",h->number);
free(h);
}
}
main()
{
int m;
int n;
int i;
clrscr();
printf("Please input the starting value of M ( the upper limit worth of M ) : ");
scanf("%d",&m);
printf("Please input the man's figure who have a hand in: ");
scanf("%d",&n);
printf("Please input the cipher from number1 to number%d:",n);
init(n);
printf("The order of Dequeue is :");
Joseph(m,H);
}

yesemiren 你怎么和我的课程设计一样呀