约瑟夫环

程序:

难点:

1.如何建立循环链表

2.如何删除链表

 

核心代码:

//三个参数:节点,前一个删除节点的符号,目前环的节点数目
//从当前节点算1开始数数,
//特例论证,用2论证
//由于1是节点本身,所有用一个循环


void joseph(NODE p,int data,int count)
{
int i=1;
NODE temp;
if(count==0)return;
temp=p;
data%=count;
if(data==0||data==1)data+=count;
while(i++<data)
{
p=temp;
temp=temp->next;            
}    
data=temp->num;
printf("%d",temp->num);
p->next=temp->next;
free(temp);
joseph(p->next,data,count-1);    
}


//固定节点个数的循环链表的建立

NODE creat(int n)
{
NODE temp;
NODE head==NULL;
temp=(NODE)malloc(sizeof(struct node));
if(temp==NULL){printf("Out of memory!");exit(1);}
head=temp;

while(i++<n)
{

temp=(NODE)malloc(sizeof(struct node));
if(temp==NULL){printf("Out of memory!");exit(1);}
printf("pls input your data:");
scanf("%d",&temp->data);
if(head==NULL){head=temp;head->next=head;p=head;continue;}
else
p->next=temp;
temp->next=p->next;
p=temp;
}
return head;
}

 

 

 

/*************************************************/

/*************************************************/

 

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

/**************************************************/
//定义结构体
/**************************************************/
struct snode
{
int num;
struct snode * next;      
};

typedef struct snode * SNODE;
SNODE head=NULL;

/**************************************************/
//建立链表
/**************************************************/
SNODE creat()
{
SNODE temp;
temp=(SNODE)malloc(sizeof(struct snode));
if(temp==NULL){printf("Out of memory!");exit(1);}
printf("pls put your data:");
scanf("%d",&temp->num);
if(head==NULL){head=temp;head->next=head;}
else
{
temp->next=head->next;
head->next=temp;
head=temp;     
}                 
}
/**************************************************/
//建立链表
/**************************************************/
SNODE display()
{
SNODE temp;
if(head==NULL)printf("The linkedlist is empty!/n");
temp=head;
printf("%d ",temp->num);
while(temp->next!=head)
{
temp=temp->next;
printf("%d ",temp->num);
}
return NULL;     
}
/*
int count;
SNODE deletenode(SNODE listnode,int num1,int count)
{
SNODE temp,pretemp;
int i=1;
temp=listnode;
if(count==1){printf("%d ",temp->num);return NULL;}
while(i<num1)
{
pretemp=temp;
temp=temp->next;
i++;
}
printf("%d ",temp->num);
num1=temp->num;
count--;
if(num1==1)
{
pretemp->next=temp->next->next;
printf("%d ",temp->next->num);
listnode=temp->next->next,delete(temp);
delete(temp->next);
deletenode(listnode,temp->next->num,count--);
}
else{pretemp->next=temp->next;listnode=temp->next;
delete(temp);
deletenode(listnode,num1,count);}

     
     
     
     
}
*/

 

/**************************************************/
//约瑟夫环
/**************************************************/

void joseph(SNODE p,int data,int count)
{
SNODE temp;
int i=1;
if(count==0)return;
temp=p;
data%=count;
if(data==0||data==1)data+=count;
while(i++<data)
{
p=temp;
temp=temp->next;           
}    
data=temp->num;
printf("%d ",temp->num);
p->next=temp->next;
free(temp);
joseph(p->next,data,count-1);    
}

/**************************************************/
//主函数
/**************************************************/
int main(void)
{
creat();
creat();
creat();
creat();
display();
printf("/n");
display();
printf("/n");
joseph(head,1,4);
//deletenode(head,4,4);


getchar();
getchar(); 

   
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值