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

📄 约瑟夫.txt

📁 具体的要求和说明如下: (1)利用单向循环链表存储结构模拟此过程
💻 TXT
字号:
#include <stdio.h> 
#include <stdlib.h> 
#define LEN sizeof(struct node) 

struct node 
{ 
int num; 
int key; 
struct node *next; 
}; 

struct node *creat(int n) 
{ 
struct node *head; 
struct node *p1,*p2; 
int i; 

head=p2=(struct node*)malloc(LEN); 
head->num=1; 

printf("输入第1个人的密码:"); 
scanf("%d",&head->key); 
printf("\n"); 

for(i=2;i<=n;i++) 
{ 
p1=(struct node*)malloc(LEN); 
p1->num=i; 

printf("输入第%d个人的密码:",i); 
scanf("%d",&p1->key); 
printf("\n"); 

p2->next=p1; 
p2=p1; 
} 
p2->next=head; 
return(head); 
} 

void delete(struct node *head,int n,int m) 
{ 
struct node *p1,*p2; 
int j,i=1; 

p1=head; 
for(j=0;j<n;j++) 
{p1=p1->next;p2=p1;} 

while(n) 
{ 
while(i<m) 
{ 
p2=p1; 
p1=p1->next; 
i++; 
} 

printf("%3d",p1->num); 
m=p1->key; 
p2->next=p1->next; 
free(p1); 
p1=p2->next; 
i=1; 
n--; 
} 


} 

int main() 
{ 
struct node *head; 
int n,m; 
printf("\n输入环的人数:"); 
scanf("%d",&n); 
printf("输入初始密码:"); 
scanf("%d",&m); 
head=creat(n); 
delete(head,n,m); 
return 0; 
}

⌨️ 快捷键说明

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