刚开始m值为20
循环链表文章来源地址https://www.toymoban.com/news/detail-721583.html
#include<iostream>
using namespace std;
typedef struct LNode
{
int data;
int num;
struct LNode *next;
}LNode ,*LinkList;
int m = 20;
int n;
void Init(LinkList &L)
{
cin>>n;
LinkList p = L;
p ->data = n;
for(int i = 0 ; i < n ; i++)
{
LinkList s = new LNode;
cin>>s ->data;
s ->num = i+1;
p -> next = s;
p = s;
}
p ->next = L ->next;
}
/*
void print(LinkList L)
{
LinkList p = L ->next;
int i = 0;
while(p)
{
cout<<p ->num<<" ";
p = p ->next;
i++;
if(i == 10) break;
}
}
*/
void f(LinkList L)
{
int count = 0;
LinkList p = L;
LinkList pre = L;
while(1)
{
for(int i = 0 ; i < m ;)
{
p = p -> next;
if(i >= 1) pre = pre ->next;
i++;
}
count++;
cout<<p ->num<<" ";
m = p ->data;
pre ->next = p ->next;
if(count == n) break;
}
}
int main()
{
// int n;
// cin>>n;
LinkList head;
head = new LNode;// 头节点
Init(head);
// print(head);
f(head);
return 0;
}
文章来源:https://www.toymoban.com/news/detail-721583.html
到了这里,关于数据结构实验1约瑟夫环的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!