📄 约瑟夫环问题link.c
字号:
#include <stdio.h>
#include <malloc.h>
struct linknode
{
int data;
struct linknode *next;
};
typedef struct linknode nodetype;
nodetype *create(int n)
//建立一个单向循环链表,且不带头节点,节点长度为n
{
int i=1;
nodetype *h=NULL,*s=NULL,*t;
h=(nodetype *)malloc(sizeof(nodetype));
if(!h) exit("分配失败");
printf("输入第1个节点的data域:");
scanf("%d",&h->data);
t=h;
for(i=2;i<=n;i++)
{
s=(nodetype *)malloc(sizeof(nodetype));
if(!s) exit("分配失败");
printf("输入第%d个节点的data域:",i);
scanf("%d",&s->data);
s->next=NULL;
t->next=s;
t=s;//t始终指向最后一个节点
}
t->next=h;//t指向最后一个节点
return t;
}
void display(nodetype *t,int n,int s,int m )
{
nodetype *p=t,*q=NULL;
int i,j,k;
for(i=1;i<s;i++)p=p->next;
//使p指向开始出列节点的前驱
for(j=1;j<=n;j++)
{
for(k=1;k<=m-1;k++)p=p->next;//使p指向要出列的节点的前驱
q=p->next;
printf("第%d个报数的节点元素是:%d\n",j,q->data);
p->next=q->next;//使q节点出列
free(q);
}
}
void main()
{
nodetype *create(int );
void display(nodetype *,int,int,int );
nodetype *rear;
rear=create(8);
display(rear,8,1,4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -