代码
1 namespace LinkedList
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7
8
9 while (true)
10 {
11 Console.WriteLine("请输入你需要得到倒数第几个元素,最大为99,输入其它程序将出错");
12 string iNum = Console.ReadLine().ToString();
13 if (iNum =="Q")
14 {
15 return ;
16 }
17 int index = Convert.ToInt32(iNum);
18
19 Node<int> node = new Node<int>(0);
20 LinKHead<int> link = new LinKHead<int>(node);
21 for (int i = 1; i <= 100; i++)
22 {
23
24 node.NexNode = new Node<int>(i);
25 node = node.NexNode;
26 }
27
28 Console.WriteLine( "倒数第N个元素为:"+link.GetNode(index, true).Value.ToString());
29 Console.WriteLine("退出请按输入“Q”,继续测试请输入测试位置Num");
30
31 }
32 }
33 }
34 class Node<T>
35 {
36
37 private T value;
38
39 public T Value
40 {
41 get { return this.value; }
42 set { this.value = value; }
43 }
44 private Node<T> nextNode;
45
46 public Node<T> NexNode
47 {
48 get { return nextNode; }
49 set { nextNode = value; }
50 }
51 public Node(T value)
52 {
53 this.value = value;
54 }
55 }
56 class LinKHead<T>
57 {
58 private Node<T> headNode;
59
60 internal Node<T> HeadNode
61 {
62 get { return headNode; }
63 set { headNode = value; }
64 }
65
66 public LinKHead(Node<T> node)
67 {
68 headNode = node;
69 }
70 public Node<T> GetValue(Node<T> node1, Node<T> node2)
71 {
72 while (true)
73 {
74 if (node2.NexNode == null)
75 {
76 return node1;
77 }
78 node1 = node1.NexNode;
79 node2 = node2.NexNode;
80 }
81 }
82 public Node<T> GetNode(int index, bool flag)
83 {
84 if (!flag)
85 {
86 return null;
87 }
88 else
89 {
90 Node<T> node = headNode;
91 Node<T> node1, node2;
92 node2 = node1 = headNode;
93 int p1, p2, tmp;
94 p1 = p2 = tmp = 0;
95 while (true)
96 {
97 if (node.NexNode == null)
98 {
99 p1 = tmp;
100 node = node2;
101 return GetValue(node1, node);
102 }
103 else if (p1 - tmp == index)
104 {
105 node1 = node2;
106 p2 = tmp;
107 node2 = node;
108 tmp = p1;
109 p1++;
110
111 node = node.NexNode;
112 }
113 else
114 {
115
116 p1++;
117 node = node.NexNode;
118
119 }
120
121 }
122
123 }
124 }
125 }
126 }
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7
8
9 while (true)
10 {
11 Console.WriteLine("请输入你需要得到倒数第几个元素,最大为99,输入其它程序将出错");
12 string iNum = Console.ReadLine().ToString();
13 if (iNum =="Q")
14 {
15 return ;
16 }
17 int index = Convert.ToInt32(iNum);
18
19 Node<int> node = new Node<int>(0);
20 LinKHead<int> link = new LinKHead<int>(node);
21 for (int i = 1; i <= 100; i++)
22 {
23
24 node.NexNode = new Node<int>(i);
25 node = node.NexNode;
26 }
27
28 Console.WriteLine( "倒数第N个元素为:"+link.GetNode(index, true).Value.ToString());
29 Console.WriteLine("退出请按输入“Q”,继续测试请输入测试位置Num");
30
31 }
32 }
33 }
34 class Node<T>
35 {
36
37 private T value;
38
39 public T Value
40 {
41 get { return this.value; }
42 set { this.value = value; }
43 }
44 private Node<T> nextNode;
45
46 public Node<T> NexNode
47 {
48 get { return nextNode; }
49 set { nextNode = value; }
50 }
51 public Node(T value)
52 {
53 this.value = value;
54 }
55 }
56 class LinKHead<T>
57 {
58 private Node<T> headNode;
59
60 internal Node<T> HeadNode
61 {
62 get { return headNode; }
63 set { headNode = value; }
64 }
65
66 public LinKHead(Node<T> node)
67 {
68 headNode = node;
69 }
70 public Node<T> GetValue(Node<T> node1, Node<T> node2)
71 {
72 while (true)
73 {
74 if (node2.NexNode == null)
75 {
76 return node1;
77 }
78 node1 = node1.NexNode;
79 node2 = node2.NexNode;
80 }
81 }
82 public Node<T> GetNode(int index, bool flag)
83 {
84 if (!flag)
85 {
86 return null;
87 }
88 else
89 {
90 Node<T> node = headNode;
91 Node<T> node1, node2;
92 node2 = node1 = headNode;
93 int p1, p2, tmp;
94 p1 = p2 = tmp = 0;
95 while (true)
96 {
97 if (node.NexNode == null)
98 {
99 p1 = tmp;
100 node = node2;
101 return GetValue(node1, node);
102 }
103 else if (p1 - tmp == index)
104 {
105 node1 = node2;
106 p2 = tmp;
107 node2 = node;
108 tmp = p1;
109 p1++;
110
111 node = node.NexNode;
112 }
113 else
114 {
115
116 p1++;
117 node = node.NexNode;
118
119 }
120
121 }
122
123 }
124 }
125 }
126 }
算法L题目上已经说明,我只是实现一下,提高自己敲代码的熟练能力。