1.函数代码:
void JosephCircle(SqQueue&Q,int n,int m)
{
ElemType e;
int i=1;
int Count = n;
while(Count != 1)
{
i = 1;
while(i != m)
{
Q.front = (Q.front+1)%(Q.MAXSIZE-1);
if(Q.base[Q.front] != 0)
{
i++;
}
}
DeQueue(Q,e);
while(Q.base[Q.front] == 0)
{
Q.front = (Q.front+1)%(Q.MAXSIZE-1);
}
printf("%d\t",e);
Count--;
}
DeQueue(Q,e);
printf("%d\t",e);
}
2.主函数以及其他结构体定义:
#include<malloc.h>
#include<stdio.h>
typedef int ElemType;
typedef struct
{
ElemType *base;
int front;
int rear;
int MAXSIZE;
}SqQueue;
bool InitQueue(SqQueue& Q,int n)
{
Q.base = new ElemType[100];
if(!Q.base)
{
printf("创建失败!\n");
return false;
}
Q.front=Q.rear=0;
Q.MAXSIZE = n+1;
return true;
}
void QueueTraverse(SqQueue Q)
{
int i;
i=Q.front;
while(i!=Q.rear)
{
printf("%d\t",Q.base[i]);
i=(i+1)%Q.MAXSIZE;
}
printf("\n");
}
bool EnQueue(SqQueue& Q,ElemType e)
{
if((Q.rear+1)%Q.MAXSIZE==Q.front)
{
printf("队列已满!\n");
return false;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%Q.MAXSIZE;
return true;
}
bool DeQueue(SqQueue& Q,ElemType& e)
{
if(Q.front==Q.rear)
{
printf("队列已满!\n");
return false;
}
e = Q.base[Q.front];
Q.base[Q.front] = 0;
Q.front = (Q.front+1)%(Q.MAXSIZE-1);
return true;
}