法一:
#include <stdio.h>
#include<malloc.h>
#define ERROR -1
#define MAXSIZE 100
typedef int ElementType;
//typedef struct QNode *PtrToLNode;
typedef struct QNode *Queue;
struct QNode
{
ElementType *Data;
int Front,Rear; //定义队头,队尾
int MaxSize; //定义最大空间
};
//typedef PtrToLNode Queue;
//建队列
Queue CreateQueue (int MaxSize)
{
Queue Q=(Queue)malloc(sizeof(struct QNode));
Q->Data=(ElementType *)malloc(MaxSize *sizeof(ElementType));
Q->Front=Q->Rear=0; //对队头队尾进行初始定位
Q->MaxSize=MaxSize; //对最大空间进行初始化
return Q;
}
//是不是满
bool IsFull(Queue Q)
{
return (Q->Rear+1)%Q->MaxSize==Q->Front;
}
//是不是空
bool IsEmpty(Queue Q)
{
return Q->Front==Q->Rear;
}
//插入
bool AddQ(Queue Q,ElementType X)
{
if(IsFull(Q))
{
printf("队列满!");
return false;
}
else
{
Q->Rear=(Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear]=X;
return true;
}
}
//弹出
ElementType DeleteQ(Queue Q)
{
if(IsEmpty(Q))
{
printf("队列空!");
return ERROR;
}
else
{
Q->Front=(Q->Front+1)%Q->MaxSize;//修改队头位置
return Q->Data[Q->Front]; //返回队头元素
}
}
//显示
void DispQueue(Queue Q)
{
int i;
//多于
if(IsEmpty(Q)) {
printf("队列空!");
return ;
}
if(Q->Front<Q->Rear) {
for (i=Q->Front+1; i<=Q->Rear; i++)
printf("%d ",Q->Data[i]);
printf("\n");
} else {
for (i=Q->Front+1; i<=Q->Rear;)
{
printf("%d ",Q->Data[i]);
i=(i+1) %Q->MaxSize;
}
printf("\n");
}
}
void yuesefu(Queue Q,int players,int number);
int main()
{
int Maxsize=100;
Queue Q=CreateQueue(Maxsize);
int i=1,j=1,choice,val,players,number;
int count=1,e;
char a;
while(i)
{
printf("\n\n\n\n");
printf("\n\t\t 队列子系统\n");
printf("\n\t\t************************************");
printf("\n\t\t* 1------入 队 *");
printf("\n\t\t* 2------出 队 *");
printf("\n\t\t* 3------显示队列中元素 *");
printf("\n\t\t* 4------约瑟夫生死问题 *");
printf("\n\t\t* 0------返 回 *");
printf("\n\t\t************************************");
printf("\n\t\t请选择菜单号(0--5):");
choice=getchar();
getchar();
switch(choice)
{
case '1':
j=1;
while(j)
{
printf("\t\t请键入一个整数(按回车输入下一个,输入'0'结束):");
scanf("%d",&val);
getchar();
if(val!=0)
AddQ(Q,val);
else
j=0;
}
break;
case '2':
printf("\t\t出队元素为:%6d\n",DeleteQ(Q));
break;
case '3':
DispQueue(Q);
break;
case '4':
printf("\n\t\t请输入总人数:");
scanf("%d",&players);
printf("\n\t\t请输入报数:");
scanf("%d",&number);
yuesefu(Q,players,number);
getchar();
break;
case '0':
i=0;
break;
}
}
return 0;
}
void yuesefu(Queue Q,int players,int number)
{
//max=players+1
//这里就不是填空啦,按照分析的思路要自己写代码了
int sum=players+1,data;
int k=0;
for(int i=1; i<=players; i++)
AddQ(Q,i);
while(players!=1){
for(int i=1;i<number;i++){
DeleteQ(Q);
AddQ(Q,Q->Data[Q->Front]);
}
DeleteQ(Q);
players--;
}
printf("幸存者是%d",Q->Data[Q->Rear]);
}
法二:
#include <stdio.h>
#include<malloc.h>
#define ERROR -1
#define MAXSIZE 100
typedef int ElementType;
//typedef struct QNode *PtrToLNode;
typedef struct QNode *Queue;
struct QNode
{
ElementType *Data;
int Front,Rear; //定义队头,队尾
int MaxSize; //定义最大空间
};
//typedef PtrToLNode Queue;
//建队列
Queue CreateQueue (int MaxSize)
{
Queue Q=(Queue)malloc(sizeof(struct QNode));
Q->Data=(ElementType *)malloc(MaxSize *sizeof(ElementType));
Q->Front=Q->Rear=0; //对队头队尾进行初始定位
Q->MaxSize=MaxSize; //对最大空间进行初始化
return Q;
}
//是不是满
bool IsFull(Queue Q)
{
return (Q->Rear+1)%Q->MaxSize==Q->Front;
}
//是不是空
bool IsEmpty(Queue Q)
{
return Q->Front==Q->Rear;
}
//插入
bool AddQ(Queue Q,ElementType X)
{
if(IsFull(Q))
{
printf("队列满!");
return false;
}
else
{
Q->Rear=(Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear]=X;
return true;
}
}
//弹出
ElementType DeleteQ(Queue Q)
{
if(IsEmpty(Q))
{
printf("队列空!");
return ERROR;
}
else
{
Q->Front=(Q->Front+1)%Q->MaxSize;//修改队头位置
return Q->Data[Q->Front]; //返回队头元素
}
}
//显示
void DispQueue(Queue Q)
{
int i;
//多于
if(IsEmpty(Q)) {
printf("队列空!");
return ;
}
if(Q->Front<Q->Rear) {
for (i=Q->Front+1; i<=Q->Rear; i++)
printf("%d ",Q->Data[i]);
printf("\n");
} else {
for (i=Q->Front+1; i<=Q->Rear;)
{
printf("%d ",Q->Data[i]);
i=(i+1) %Q->MaxSize;
}
printf("\n");
}
}
void yuesefu(Queue Q,int players,int number);
int main()
{
int Maxsize=100;
Queue Q=CreateQueue(Maxsize);
int i=1,j=1,choice,val,players,number;
int count=1,e;
char a;
while(i)
{
printf("\n\n\n\n");
printf("\n\t\t 队列子系统\n");
printf("\n\t\t************************************");
printf("\n\t\t* 1------入 队 *");
printf("\n\t\t* 2------出 队 *");
printf("\n\t\t* 3------显示队列中元素 *");
printf("\n\t\t* 4------约瑟夫生死问题 *");
printf("\n\t\t* 0------返 回 *");
printf("\n\t\t************************************");
printf("\n\t\t请选择菜单号(0--5):");
choice=getchar();
getchar();
switch(choice)
{
case '1':
j=1;
while(j)
{
printf("\t\t请键入一个整数(按回车输入下一个,输入'0'结束):");
scanf("%d",&val);
getchar();
if(val!=0)
AddQ(Q,val);
else
j=0;
}
break;
case '2':
printf("\t\t出队元素为:%6d\n",DeleteQ(Q));
break;
case '3':
DispQueue(Q);
break;
case '4':
printf("\n\t\t请输入总人数:");
scanf("%d",&players);
printf("\n\t\t请输入报数:");
scanf("%d",&number);
yuesefu(Q,players,number);
getchar();
break;
case '0':
i=0;
break;
}
}
return 0;
}
void yuesefu(Queue Q,int players,int number)
{
//max=players+1
//这里就不是填空啦,按照分析的思路要自己写代码了
int sum=players+1,data;
int k=0;
for(int i=1; i<=players; i++)
AddQ(Q,i);
Q->Data[0]=0;
while(players!=1) {
Q->Front=(Q->Front+1)%sum;
if(Q->Data[Q->Front]!=0) {
k++;
if(k==number) {
Q->Data[Q->Front]=0;
players--;
k=0;
}
}
}
for(int i=1; i<=sum-1; i++) {
if(Q->Data[i]!=0)
data=Q->Data[i];
}
printf("幸存者是%d",data);
}