C#练习题答案: 转换一个链表串【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客提供了11种不同的解决方案,针对C#中将链表转换为字符串的初级编程练习题目。内容涵盖多种解答思路,适合C#初学者巩固链表和字符串操作的知识。
摘要由CSDN通过智能技术生成

转换一个链表串【难度:1级】:

答案1:

public class Kata
{
  public static string Stringify(Node list)
    =>  list == null ? "null" : list.Data + " -> " + Stringify(list.Next);
}

答案2:

using System;
using System.Collections.Generic;

public class Kata
{
  private static IEnumerable<string> Adapt(Node list)
  {
    for (; list != null; list = list.Next) yield return list.Data.ToString();
    yield return "null";
  }

  public static string Stringify(Node list) => String.Join(" -> ", Adapt(list));
}

答案3:

public class Kata
{
  public static string Stringify(Node list)
  {
    if (list == null)
      return "null";
      
    return list.Data.ToString() + " -> " + Stringify(list.Next);
  }
}

答案4:

public class Kata
{
    public static string Stringify(Node list)
    {
        if (list == null)
        {
            return "null";
        }

        return list.Data.ToString() + " -> " + Stringify(list.Next);
    }
}

答案5:

using System;

public class Kata
{
  public static string Stringify(Node list)
  {
  if (list == null)
            {
                return "null";
            }
            string result = String.Empty;
            Node tempnode = list;
            if (tempnode.Next == null)
            {
                result += tempnode.Data + " -> null";
                return result;
            }
            do
            {
                result += tempnode.Data + " -> ";
                if (tempnode.Next.Next == null)
                {
                    result += tempnode.Next.Data + " -> null";
                }
                tempnode = tempnode.Next;
            }
            while (tempnode.Next != null);


            return result;
  }
}

答案6:

using System.Text;

public static class Kata
{
  public static string Stringify(Node list)
  {
    var builder = new StringBuilder();

    for (; list != null; list = list.Next)
    {
      builder.Append(list.Data).Append(" -> ");
    }

    return builder.Append("null").ToString();
  }
}

答案7:

using System;

public class Kata
{
  public static string Stringify(Node list)
  {
    string resulat = "";
    while (list != null)
    {
        resulat = String.Concat(resulat, list.Data.ToString(), " -> ");
        list = list.Next;
    }
    resulat = String.Concat(resulat, "null");
    return resulat;
  }
}

答案8:

public class Kata
{
  public static string Stringify(Node list)
  {
    return (list == null) ? "null" : $"{list.Data} -> " + Stringify(list.Next);
  }
}

答案9:

public class Kata
{
  public static string Stringify(Node list) => (list==null)?"null":$"{list.Data} -> "+Stringify(list.Next);  
}

答案10:

public class Kata
{
  public static string Stringify(Node list)
  {
    return list == null ? "null" : list.Data + $" -> {Stringify(list.Next)}";
  }
}

答案11:

public class Kata
{
    public static string Stringify(Node list)
    {
        string representation = "";

        if (list == null)
            representation += "null";
        else
        {
            representation += $"{list.Data} -> ";

            if (list.Next != null)
                representation += Stringify(list.Next);
            else
                representation += "null";
        }

        return representation;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值