约瑟夫环程序

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:52:43
#include<stdio.h>
#include<stdlib.h>
#define MAXNUM 30
#define TRUE 1
#define FALSE 0
typedef struct NodeType
{ int number; /* 编号 */
int password; /* 密码 */
struct NodeType *next;
} NodeType;

/* 创建单向循环链表 */
void CreaList(NodeType **,int);
/* 运行"约瑟夫环"问题 */
void jeseph(NodeType **, int);
/* 得到一个结点 */
NodeType *GetNode(int, int);

void main()
{ int n, m;
NodeType *pHead=NULL;
while(1)
{printf("请输入人数 n:");
scanf("%d",&n);
printf("输入初始密码 m:");
scanf("%d",&m);
if(n>MAXNUM)
{printf("人数超出范围,请重新输入!\n");
continue;
}
else
break;
}
CreaList(&pHead,n);
printf("出列顺序是:\n");
jeseph(&pHead, m);
}

void CreaList(NodeType **ppHead, int n)
{
int i,ipassword;
NodeType *pNew, *pCur;
for(i=1;i<=n;i++)

#include"stdio.h"

#define LEN sizeof(node)

typedef struct typedata{ //定义一个结构体“data”
int num; //序号
int code; //密码
}typedata;

typedef struct node{ // 定义结点
typedata data; // 结构体的嵌套
struct node *next;
}node;

node *creat(node *head,int num);
node *del(node *head,int code,int *psw);

main()
{
node *head,*p,*q;
int num,i,j,code,psw; //code 放密码
printf("请输入人数(num>=2): ");
scanf("%d",&num);
printf("初始的code :");
scanf("%d",&code);
head=creat(head,num);
for(i=1;i<num;i++){
head=del(head,code,&psw);
code=psw;
}
printf("\n最后一个是 %d ",head->data.num);
}

node *creat(node *head,int num) //建立链表
{
node *p,*q;
int i;
head=q=(node*)ma