namespace LinkedList
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console
WriteLine(
請輸入你需要得到倒數第幾個元素
最大為
輸入其它程序將出錯
);
string iNum = Console
ReadLine()
ToString();
if (iNum ==
Q
)
{
return ;
}
int index = Convert
ToInt
(iNum);
Node node = new Node(
);
LinKHead link = new LinKHead(node);
for (int i =
; i (i);
node = node
NexNode;
}
Console
WriteLine(
倒數第N個元素為
+link
GetNode(index
true)
Value
ToString());
Console
WriteLine(
退出請按輸入
Q
繼續測試請輸入測試位置Num
);
}
}
}
class Node
{
private T value;
public T Value
{
get { return this
value; }
set { this
value = value; }
}
private Node nextNode;
public Node NexNode
{
get { return nextNode; }
set { nextNode = value; }
}
public Node(T value)
{
this
value = value;
}
}
class LinKHead
{
private Node headNode;
internal Node HeadNode
{
get { return headNode; }
set { headNode = value; }
}
public LinKHead(Node node)
{
headNode = node;
}
public Node GetValue(Node node
Node node
)
{
while (true)
{
if (node
NexNode == null)
{
return node
;
}
node
= node
NexNode;
node
= node
NexNode;
}
}
public Node GetNode(int index
bool flag)
{
if (!flag)
{
return null;
}
else
{
Node node = headNode;
Node node
node
;
node
= node
= headNode;
int p
p
tmp;
p
= p
= tmp =
;
while (true)
{
if (node
NexNode == null)
{
p
= tmp;
node = node
;
return GetValue(node
node);
}
else if (p
tmp == index)
{
node
= node
;
p
= tmp;
node
= node;
tmp = p
;
p
++;
node = node
NexNode;
}
else
{
p
++;
node = node
NexNode;
}
}
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11676.html