数据结构实验1约瑟夫环

刚开始m值为20
循环链表

#include

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<

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; }

你可能感兴趣的:(严蔚敏数据结构,数据结构)