主页 > 程序人生 > 拙劣之作,约瑟夫环循环链表解法

拙劣之作,约瑟夫环循环链表解法

下学期的数据结构期末作业貌似要做这个,估计就是循环链表的模拟解法吧。。。
所以就凭着记忆写了一个比较简单的cpp版本。
大牛一笑哂之~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<cstdlib>
#include<iostream>
using namespace std;
struct node{
	int data;
	int index;
	node *next,*pre;
}*root,*tail;
void del(node* a)
{
	node *p;
	p=a->pre;
	p->next=a->next;
	a->next->pre=p;
	cout<<a->index<<endl;
	free(a);
	return ;
}
 
void add(int aas,int ind)
{
	node *aa=new node;
	aa->data=aas;
	aa->index=ind;
	aa->pre=tail;
	tail->next=aa;
	aa->next=root;
	tail=aa;
	root->pre=tail;
}
int main()
{
	int i;
	int a;
	int n,m;
	m=4;n=6;
	cin>>m>>n;
	for(i=0;i<n;i++)
	{
 
		cin>>a;
		if(i){
			add(a,i+1);
		}else{
			root=new node;
			tail=root;
			root->data=a;
			root->index=i+1;
			root->next=tail;
			root->pre=tail;
			tail->next=root;
			tail->pre=root;
		}
	}
	node *tt;
	tt=root;
	while(n){
		int kk=0;
		while(1)
		{
			kk++;
			if(kk==m)
			{
				m=tt->data;
				del(tt);
				tt=tt->next;
				break;
			}else {
				tt=tt->next;
			}
		}
			n--;
	}
}

相关日志

, , , , , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
拙劣之作,约瑟夫环循环链表解法 来自 混沌的云
顶部