如何在链表中的位置x处找到整数?

时间:2017-05-01 11:21:42

标签: c#

if (choice == 1)
            {   for (int x = 1; x <= Small.Count()-1;x++)
            {
                Number min = (Number)Small[x-1]; // i know this how you do it for an array list
                int minpos = x - 1;

Number是我要查找的列表中的值,small是列表

1 个答案:

答案 0 :(得分:1)

获取单链表中的第n个元素(假设SinglyLinkedList包含Next变量)

public SinglyLinkedList ElementAt(int position)
{
    SinglyLinkedList node = Head;
    int counter = 0;
    while (node != null && counter++ != position)
    {
        node = node.Next;
    }
    return node;
}

假设单链表:37 - &gt; 29 - &gt; 45 - &gt; 90 - &gt; 10 - &gt; 4 - &gt; 21 - &gt; 7

list.ElementAt(0)     // Output: 37
list.ElementAt(5)     // Output: 4
list.ElementAt(20)    // Output: null