static void MyCircularLinkedListTest() { MyCircularLinkedList <int> linkedList = new MyCircularLinkedList <int>(); // 顺序插入5个节点 linkedList.Add(1); linkedList.Add(2); linkedList.Add(3); linkedList.Add(4); linkedList.Add(5); Console.WriteLine("All nodes in the circular linked list:"); Console.WriteLine(linkedList.GetAllNodes()); Console.WriteLine("--------------------------------------"); // 当前节点:第一个节点 Console.WriteLine("Current node in the circular linked list:"); Console.WriteLine(linkedList.CurrentItem); Console.WriteLine("--------------------------------------"); // 移除当前节点(第一个节点) linkedList.Remove(); Console.WriteLine("After remove the current node:"); Console.WriteLine(linkedList.GetAllNodes()); Console.WriteLine("Current node in the circular linked list:"); Console.WriteLine(linkedList.CurrentItem); // 移除当前节点(第二个节点) linkedList.Remove(); Console.WriteLine("After remove the current node:"); Console.WriteLine(linkedList.GetAllNodes()); Console.WriteLine("Current node in the circular linked list:"); Console.WriteLine(linkedList.CurrentItem); Console.WriteLine("--------------------------------------"); }
static void JosephusTest() { MyCircularLinkedList <int> linkedList = new MyCircularLinkedList <int>(); string result = string.Empty; Console.WriteLine("Step1:请输入人数N"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Step2:请输入数字M"); int m = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Step3:报数游戏开始"); // 添加参与人员元素 for (int i = 1; i <= n; i++) { linkedList.Add(i); } // 打印所有参与人员 Console.Write("所有参与人员:{0}", linkedList.GetAllNodes()); Console.Write("\r\n" + "-------------------------------------"); result = string.Empty; while (linkedList.Count > 1) { // 依次报数:移动 linkedList.Move(m); // 记录出队人员 result += linkedList.CurrentItem + " "; // 移除人员出队 linkedList.Remove(); Console.WriteLine(); Console.Write("剩余报数人员:{0}", linkedList.GetAllNodes()); Console.Write(" 开始报数人员:{0}", linkedList.CurrentItem); } Console.WriteLine("\r\n" + "Step4:报数游戏结束"); Console.WriteLine("-------------------------------------"); Console.WriteLine("出队人员顺序:{0}", result + linkedList.CurrentItem); }