使用C#链表简单实现的约瑟夫环

前几天随便用C#写了一个约瑟夫环 
谈不上效率 因为使用的是C#自带的双向链表

 1   // count  代表总数
      
// order  代表报号出列的号码
       //begId   代表起始 位置
         class  Program
 2      {
 3        static void Main(string[] args)
 4        {
 5            Console.WriteLine("Please Enter Total:");
 6            int count = int.Parse(Console.ReadLine());
 7
 8            LinkedList<Person> list = InitList(count);
 9
10            Console.WriteLine("Please Enter Index:");
11            int order = int.Parse(Console.ReadLine());
12
13            Console.WriteLine("Please Enter Start Index:");
14            int begid = int.Parse(Console.ReadLine());
15
16
17            LinkedListNode<Person> first = list.First;
18            for (int i = 0; i < begid - 1; i++)
19                first = (first == list.Last ? list.First : first.Next);
20
21            LinkedListNode<Person> start;
22            do
23            {
24                for (int i = 0; i < order - 1; i++)
25                    first = (first == list.Last ? list.First : first.Next);
26
27                start = first;
28
29                first = (first == list.Last ? list.First : first.Next);
30                
31                Console.WriteLine(start.Value.Id);
32                list.Remove(start);
33            }

34            while (list.Count >= 1);
35
36            Console.ReadLine();
37        }
38
39        static LinkedList<Person> InitList(int count)
40        {
41            LinkedList<Person> list = new LinkedList<Person>();
42            for (int i = 1; i <= count; i++)
43            {
44                Person p = new Person();
45                p.Id = i;
46                p.Name = "Person-" + i.ToString();
47                list.AddLast(p);
48            }

49            return list;
50        }

51    }
52
53      class  Person
54      {
55        private int _id;
56
57        public int Id
58        {
59            get return _id; }
60            set { _id = value; }
61        }

62        private string _name;
63
64        public string Name
65        {
66            get return _name; }
67            set { _name = value; }
68        }

69    }

70

运行结果如下:




转载于:https://www.cnblogs.com/rthy/archive/2008/04/09/1144180.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值