⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 houwang.c

📁 用数据结构做的约瑟夫问题
💻 C
字号:
/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "stdlib.h"
#define NULL 0
#define OK 1;
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int Elemtype;
typedef struct Cnode
{
    Elemtype data;
    struct Cnode *next;
}CNode;
CNode *joseph;
Status Create_clist(CNode *clist,int n)
{
    CNode *p,*q ;
    int i;
    clist=NULL;
    for(i=n;i>=1;i--)
    {
        p=(CNode*)malloc(sizeof(CNode));
        if(p==NULL)
            return OVERFLOW;
        p->data=i;
        p->next=clist;
        clist=p;
        if(i==n)
        q=p;
    }
    q->next=clist;
    joseph=clist;
    return OK;
}
Status Joseph(CNode *clist,int m,int n,int k)
{
    int i;
    CNode *p,*q;
    if(m>n)return ERROR;
        if(!Create_clist(clist,n))
        return ERROR;
    p=joseph;
    for(i=1;i<m;i++)
        p=p->next;
    while(p)
    {
        for(i=1;i<k-1;i++)
            p=p->next;
        q=p->next;
        printf("%d  ",q->data);
        if(p->next==p)
            p=NULL;
        else
        {
            p->next=q->next;
            p=p->next;
            free(q);
        }
        clist=NULL;
    }
}
jixu()
{
    int m,n,k,i;
    CNode *clist;
    printf("\n请输入围坐在圆桌周围的人数1n:");
    scanf("%d",&n);
    m=1;
    k=13;
    Create_clist(clist,n);
    printf("\n出列的顺序如下4:\n");
    Joseph(clist,m,n,k);
}
void main()
{

    char f;
    CNode *clist;
    f='y';
    while((f=='y')||(f=='Y'))
        {
            jixu();
            printf("\n要继续测试吗(y/Y继续,其他键退出):\n");
            scanf("%s",&f);

        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -