C#练习题答案: 链表 - 获取第N个节点【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

链表 - 获取第N个节点【难度:1级】:

答案1:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    if (node==null)
      throw new ArgumentException();
    if (index==0)
      return node;
    else
    {
    return GetNth(node.Next,index-1);}
  }
}

答案2:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
   if (node==null){
   throw new ArgumentException();
   }
   if (index == 0)
   return node;
   else
   return GetNth(node.Next, index - 1);
  }
}

答案3:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    if (index < 0 || node == null) 
    {
      throw new ArgumentException();
    }
    else 
    {
      int currentIndex = 0;
      Node currentNode = node;
      while (currentNode != null)
      {
        if (currentIndex == index)
        {
          return currentNode;
        }
        else
        {
          currentIndex++;
          currentNode = currentNode.Next;
        }
      }
      throw new ArgumentException();
    }
  }
}

答案4:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static int Length(Node head)
  {
    int length = 0;
    Node currentNode = head;
    while (currentNode != null)
    {
      length++;
      currentNode = currentNode.Next;
    }            
    return length;
  }
  
  public static Node GetNth(Node node, int index)
  {
    Node nodeToReturn = node;
    if (node == null || 0 > index || index > Node.Length(node) - 1)
    {
      throw new ArgumentException();
    }
    else
    {
      for (int i = 0; i < index; i++)
      {
        nodeToReturn = nodeToReturn.Next;
      }
      return nodeToReturn;
    }  
  }
}

答案5:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    while (node != null &amp;&amp; index > 0) {
      node = node.Next;
      index--;
    }
    if (node == null || index != 0)
      throw new ArgumentException();
    return node;
  }
}

答案6:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    int i = 0;
    while(i!=index)
    {
      if(node==null) throw new ArgumentException();
      else {++i; node = node.Next;}   
    }
    if(node==null) throw new ArgumentException();
    return node;

  }
}

答案7:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
        public static Node GetNth(Node node, int index)
        {
            var current = node;
            var counter = 0;

            while (current != null)
            {
                if (counter++ == index)
                {
                    return current;
                }

                current = current.Next;
            }

            throw new ArgumentException();
        }
}

答案8:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    var current = node;
    var count = 0;
    while (current != null)
    {
      if (count == index)
        return current;
      current = current.Next;
      count++;
    }
    throw new ArgumentException();
  }
}

答案9:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    if (node == null || index < 0)
      {
        throw new ArgumentException();
      }

      var tempNode = node;
      
      for (int i = 0; i < index; i++)
      {
        if (tempNode.Next != null)
        {
          tempNode = tempNode.Next;
        }
        else
        {
          throw new ArgumentException();
        }
      }

      return tempNode;
  }
}

答案10:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
        {
            if (node == null) throw new ArgumentException();
            
            var nextNode = node;
            int indexRes = 0;

            while (nextNode != null)
            {
                if (indexRes == index)
                {
                    return nextNode;
                }

                indexRes++;
                nextNode = nextNode.Next;
            }

            throw new ArgumentException();
        }
}

答案11:

using System;

public partial class Node
{
  public int Data;
  public Node Next;
  
  public Node(int data)
  {
    this.Data = data;
    this.Next = null;
  }
  
  public static Node GetNth(Node node, int index)
  {
    while (index > 0 &amp;&amp; node != null) {
      node = node.Next;
      index--;
    }
    
    if (node == null || index != 0) {
      throw new System.ArgumentException("Invalid index", "index");
    }
    
    return node;
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值