首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 上機題:雙向循環鏈表實作約瑟夫問題(三)

     編輯:關於C語言

未使用陣列儲存節點,索引處理較繁瑣(用陣列則有容量限制),歡迎高手提供更好作法。

完整實作

class Program {
 static void Main(string[] args) {
   CircleList<Person> list = new CircleList<Person>();
   for (int i = 0; i < 17; i++) list.AddLast(new Person(i + 1));
   DoubleLinkNode<Person> current = list.First;
   int k = 0;
   while (list.Count > 1) {
     k++;
     Console.WriteLine(string.Format("{0}:{1}", current.Value.Id, k));
     if (k % 3 == 0) list.Remove(current);
     current = current.Next;
   }
   Console.WriteLine(string.Format("Last Person:{0}", current.Value.Id));
   Console.Read();
 }
}
public class Person { public Person(int id) { this.Id = id; } public int Id { get; set; } }

執行結果圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved