#include<iostream>
using namespace std;
//约瑟夫循环
typedef struct node
{
int data;
int password;
struct node *next;
}Node,*LinkList;
LinkList p, t, L;//L为头节点,P为临时节点,t为新节点
int CreatList(int n)
{
int i;
if (!(L = (LinkList)malloc(sizeof(Node)))) //定义头节点并判断是否创建成功
return 0;
p = L; //定义指向头节点的指针
for (i = 1; i < n; i++)
{
if (!(t = (LinkList)malloc(sizeof(Node))))
return 0; /*创建循环链表并移动头指针*/
p->next = t;
p = t;
}
p->next = L;
t = L; //给头节点赋值
return 0;
}
int Insert(int n)
{
int i, j;
cout << "输入密码:";
for (i = 1; i <= n; i++)
{
cin >> j; /*默认序列号为1.2.3.。。并给序列号定义密码*/
t->data = i;
t->password = j;
t = t->next;
}
t = p;
cout << "原数序列为:";
for (i = 1; i <= n; i++)
cout << i << ' ';
cout << endl;
return 0;
}
int OutList(int m, int n)//遍历序列,m为其实位置,n为总数
{
int i, a;
for (i = 1; i <= n; i++)
{
for (a = 1; a < m; a++)
t = t->next;
p = t->next;
m = p->password;
cout << p->data << ' ';
t->next = p->next;
free(p);
}
return 0;
}
int main()
{
int n;
cout << "输入小于30的整数:";
cin >> n;
CreatList(n);
Insert(n);
int m;
cout << "输入开始位置:";
cin >> m;
cout << "出列编号序列:";
OutList(m, n);
cout << endl;
return 0;
}