尝试用java的自定义双向循环链表实现约瑟夫算法。
在C语言里很简单的问题,知道算法的情况下竟然也搞了2个小时。。
首领元素:
View Code
1 package com.app;
2
3 public class man {
4 private int Id;
5
6 public int getId() {
7 return Id;
8 }
9
10 public void setId(int id) {
11 Id = id;
12 }
13
14 public void sayMan(){
15 System.out.println("I am the Leander" + Id);
16 }
17 }
节点类:
View Code
1 package com.app;
2
3 public class node<object> {
4 public object ob;
5 public node<object> next;
6 public node<object> pre;
7
8 public node(){
9
10 }
11
12 public node(object ob1){
13 ob = ob1;
14 next = null;
15 pre = null;
16 }
17 }
循环链表:
View Code
1 package com.app;
2
3 public class linkList<T> {
4
5 private node<T> list;
6 private int num;
7
8 /*-12
9 * 初始化循环队列
10 * @number:队列的大小
11 */
12 public linkList(){
13 list = new node<T>();
14 num = 0;
15 }
16
17 public void deleteNode(node<T> input){
18 node<T> temp;
19 if(num < 1){
20 System.out.println("error");
21
22 return;
23 }
24
25 if(list == input){
26 list = input.next;
27 }
28
29 temp = input;
30 input.pre.next = temp.next;
31 input.next.pre = temp.pre;
32
33 num--;
34
35 }
36
37 public void deleteNode(int index){
38 node<T> temp;
39 int count = 0;
40 if((0 == index)||(0 == index%num)){
41 /*删除队头 */
42 if(1 == num){
43 num = 0;
44 }else{
45 temp = list;
46 list.next.pre = temp.pre;
47 list.pre.next = temp.next;
48 list = temp;
49 }
50 }else{
51 temp = list;
52 while(0 != count){
53 temp = temp.next;
54 }
55
56 temp.pre.next = temp.next;
57 temp.next.pre = temp.pre;
58 num--;
59 }
60
61 return;
62 }
63
64 public node<T> getNode(int index){
65 node<T> temp;
66 int count = 0;
67
68 count = index;
69
70 temp = list;
71 while(0 != count){
72 temp = temp.next;
73 count--;
74 }
75
76 return temp;
77 }
78
79 public void addNode(T input){
80 node<T> temp;
81
82 if(0 == num){
83 list = new node<T>(input);
84 list.next = list;
85 list.pre = list;
86 num++;
87 }else{
88 node<T> nd;
89 nd = getNode(num - 1);
90
91 temp = new node<T>(input);
92 nd.next = temp;
93 temp.pre = nd;
94 temp.next = list;
95 list.pre = temp;
96 num++;
97 }
98 }
99
100 public int getNum() {
101 return num;
102 }
103
104 public node<T> getList() {
105 return list;
106 }
107 }
选首领:
View Code
1 package com.app;
2
3 public class pickApp {
4 private int queneNum;
5 private int pickNum;
6
7 public pickApp(int num1, int num2){
8 queneNum = num1;
9 pickNum = num2;
10 }
11
12 public void pickTheHeader(){
13
14 /*初始化队列 */
15 linkList<man> list = new linkList<man>();
16 man m;
17 //初始化队列
18 for(int i = 0; i < queneNum; i++){
19 m = new man();
20 m.setId(i + 1);
21 list.addNode(m);
22 }
23
24 System.out.println(list.getNum());
25
26 //循环遍历队列
27 for(int i = 0; i < list.getNum(); i++){
28 list.getNode(i).ob.sayMan();
29 }
30
31 //根据pickNum选择首领
32 int count = 0;
33 node<man> temp = list.getList();
34 node<man> temp2;
35 while(list.getNum() > 1){
36 if(pickNum - 1 == count){
37 temp2 = temp;
38 list.deleteNode(temp);
39 System.out.println("delete" + temp2.ob.getId());
40 temp = temp2.next;
41 count = 0;
42 }else{
43 temp = temp.next;
44 count++;
45 }
46 }
47
48 list.getNode(0).ob.sayMan();
49
50 return;
51 }
52 }