数据结构:约瑟夫环的顺序和链式实现(C/C++)

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

#define FALSE 0
#define TRUE 1
typedef int Datatype;
struct Node;
typedef struct Node*PNode;
struct Node{
    Datatype info;
    PNode link;
    };
typedef struct Node*LinkList;
typedef LinkList*PLinkList;

typedef int DataType;

typedef struct seq
 {
    int Maxnum;
    int n;
    int *element;
}*PSeqList;
PSeqList palist;

/*创建空表*/
create_seq(int m)
{
  palist=(PSeqList)malloc(sizeof(struct seq));
  if(palist!=NULL)
     {
      palist->element=(int *)malloc(sizeof(int)*m);
      if(palist->element!=NULL)
          {
          palist->Maxnum=m;
          palist->n=0;
          }
      else free(palist);
      }
   else
      printf("Out of space!!!\n");

}

/*插入函数*/
insert_seq(int p,int x)
{
   int q;
   if(palist->n>=palist->Maxnum)
        { printf("Overflow!!\n");
		  exit(0);
		}
   if(p<0||p>palist->n)
        { printf("The location is not exist,illegal!\n");
		  exit(0);
		}
   for(q=palist->n-1;q>=p;q--)
		{  palist->element[q+1]=palist->element[q];
		} //ºóÒÆ
   palist->element[p]=x;
   palist->n=palist->n+1;

}

/*删除函数*/
int deleteP_seq(int p)
{
  int q;
  int x;
  if(p<0||p>palist->n-1)
      printf("NOT exist!\n");
  else
    {
      x=palist->element[p];
      for(q=p;q<palist->n-1;q++)
          palist->element[q]=palist->element[q+1];  //Ç°ÒÆ
      palist->n=palist->n-1;
     }
  return x;
}

int locate_seq(int x)
{
    int q;
    for(q = 0;q < palist ->n;q++)
    {
        if(palist ->element[q] == x)
        {
            return q;
        }
    }
    return -1;
}

void josephus_seq(int s,int m)
{
    int s1,i,w;
    s1 = s - 1;
    for(i = palist->n;i>0;i--)
    {
        s1 = (s1 + m - 1) % i;
        w = palist ->element[s1];
        printf("Out element %d \n",w);
        deleteP_seq(s1);
    }
}
int jos_Link()
{
  PSeqList jos_alist;
  int i,k;
  int n,s,m;
  printf("please input the values(<100) of n= \n");
  scanf("%d",&n);
  printf("please input the values of s = \n");
  scanf("%d",&s);
  printf("please input the values of m = \n");
  scanf("%d",&m);
  jos_alist = create_seq(n);
  if(jos_alist!=NULL)
  {
      for(i = 0;i < n;i++)
        insert_seq(i,i+1);
      josephus_seq(s,m);
      free(jos_alist->element);
      free(jos_alist);
  }
}

int init_clist(PLinkList pclist,int n)
/* 用1,2...,n为*pclist所示的循环表初始化*/
{
    PNode p,q;
    int i;
    q = (PNode)malloc(sizeof(struct Node));
    if(q== NULL)return FALSE;
    *pclist = q;
    q->info=1;
    q->link=q;
    if(n==1)return TRUE;
    for(i = 2;i<n+1;i++)
    {
        p = (PNode)malloc(sizeof(struct Node));
        if(p==NULL)return FALSE;
        p->info = i;
        p->link= q->link;
        q->link=p;
        q=p;
    }
    return TRUE;
}
void josephus_clist(PLinkList pclist,int s,int m)
{
    PNode p,pre;
    int i;
    p = *pclist;
    /*找第s个元素*/
    if(s==1)
    {
        pre = p;
        p = p->link;
        while(p!= *pclist)
        {
            pre = p;
            p = p->link;
        }
    }
    else for(i=1;i<s;i++)
    {
        pre = p;
        p = p->link;
    }
    while(p!=p->link)
    {
        for(i=1;i<m;i++)
        {
            pre = p;
            p = p->link;
        }
        printf("out element:%d\n",p->info);
        if(*pclist == p)
            *pclist = p->link;
        pre ->link = p->link;
        free(p);
        p=pre->link;
    }
    printf("out element:%d\n",p->info);
    *pclist = NULL;
    free(p);
}
int jos_Seq()
{
     LinkList jos_clist;
     int n,s,m;
     do{
        printf("\n please input the values of n=");
        scanf("%d",&n);
     }while(n<1);
     do{
        printf("\n please input the values of s=");
        scanf("%d",&s);
     }while(s<1);
     do{
        printf("\n please input the values of m=");
        scanf("%d",&m);
     }while(m<1);
     if(init_clist(&jos_clist,n))
        josephus_clist(&jos_clist,s,m);
     else
        printf("Out of space! \n");
}
int main()
{
  int c,m,n,i,x,p;
  int xx;
  while(1)
  {
    printf("\n---------------");
    printf("\n1------jos_Link");
    printf("\n2------jos_Seq");
    printf("\n0------Exit");
    printf("\n---------------");
    printf("\nPlease choose menu(0-2):");
    scanf("%d",&c);
    switch(c)
     {
       case 1: {
                  jos_Link();
                  break;
               }
       case 2: {  jos_Seq();
                  break;
               }
       case 0:return 0;
       defalult: printf("\nYour choice number is error!!!");
     }
  }
}

还有相同密码和不同密码的两种约瑟夫环,欢迎练习

qq:1351006594

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aaron_Liu0730

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值