java

位置:IT落伍者 >> java >> 浏览文章

一道求单向链表倒数第N个结点的算法题


发布日期:2023年12月24日
 
一道求单向链表倒数第N个结点的算法题
namespace LinkedList

{

class Program

{

static void Main(string[] args)

{

while (true)

{

ConsoleWriteLine(请输入你需要得到倒数第几个元素最大为输入其它程序将出错);

string iNum = ConsoleReadLine()ToString();

if (iNum ==Q)

{

return ;

}

int index = ConvertToInt(iNum);

Node node = new Node();

LinKHead link = new LinKHead(node);

for (int i = ; i (i);

node = nodeNexNode;

}

ConsoleWriteLine( 倒数第N个元素为+linkGetNode(index true)ValueToString());

ConsoleWriteLine(退出请按输入Q继续测试请输入测试位置Num);

}

}

}

class Node

{

private T value;

public T Value

{

get { return thisvalue; }

set { thisvalue = value; }

}

private Node nextNode;

public Node NexNode

{

get { return nextNode; }

set { nextNode = value; }

}

public Node(T value)

{

thisvalue = 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 (nodeNexNode == null)

{

return node;

}

node = nodeNexNode;

node = nodeNexNode;

}

}

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 (nodeNexNode == null)

{

p = tmp;

node = node;

return GetValue(node node);

}

else if (p tmp == index)

{

node = node;

p = tmp;

node = node;

tmp = p;

p++;

node = nodeNexNode;

}

else

{

p++;

node = nodeNexNode;

}

}

}

}

}

}

               

上一篇:Java版本和C++版本简单Stack程序

下一篇:NAnt build文件编辑器