【问题标题】:How to convert a linkedlist to an array in c#?如何将链表转换为c#中的数组?
【发布时间】:2015-06-01 12:37:30
【问题描述】:

对于一个学校项目,我正在制作一个 RPN 计算器。这个计算器有一个抽象类 Stack,其中三种不同的堆栈实现派生自其中。其中之一是链表堆栈。对于这些堆栈,我必须显示堆栈上的值。为此,我使用了一个数组。对于我的数组堆栈和列表堆栈来说,这很容易,但我不知道如何将链表转换为数组。最好的方法是什么?

这是我用于链表的代码。

public class Node
{
    public int data;
    public Node next;

    public Node(int i)
    {
        data = i;
        next = null;
    }

    public void Add(int i)
    {
        if (next == null)
        {
            next = new Node(i);
        }
        else
        {
            next.Add(i);
        }
    }        
}
public class MyLinkedList
{
    private Node headNode;

    private int count;

    public MyLinkedList()
    {
        headNode = null;
        count = 0;
    }

    public int Count()
    {
        return count;
    }

    public void Add(int i)
    {
        if (headNode == null)
        {
            headNode = new Node(i);
        }
        else
        {
            headNode.Add(i);
        }
        count++;
    }

【问题讨论】:

  • 你大概知道如何访问你的链表的每个节点。因此:(1)通过访问每个节点来计算所有节点(如果存在,则使用现有的count),(2)创建该大小的数组,(3)再次访问每个节点,这次将每个节点复制到数组的连续元素。

标签: c# arrays linked-list


【解决方案1】:
  1. 在您的 LinkedList 上实现 IEnumerable
  2. using System.Linq;
  3. 在 LinkedList 上调用 ToArray

实现IEnumerable 很简单。只需从列表中的根节点yield return Node.data; 开始,然后移动到下一个节点。冲洗并重复,直到下一个节点为null

【讨论】:

    【解决方案2】:

    为你的类添加一个新方法。

    public class MyLinkedList
    {
      ... keep existing methods here ...
    
      public int[] ToArray() {
        var result = new int[count]();
        var index = 0;
        var node = headnode;
        while (node != null) {
          result[index] = node.data;
          node = node.next;
        }
    
        return result;
      }
    

    【讨论】:

      【解决方案3】:

      呃……是这样的吗?

      public class MyLinkedList {
        ...
        public Node[] ToArray() {
          // You´ve got pre-computed count - let´s use it
          Node[] result = new Node[count]; 
      
          Node node = headNode;
      
          for (int i = 0; i < result.Length; ++i) {  
            result[i] = node;
            node = node.next;
          }
      
          return result;
        }
      }
      

      附: 公共字段Node 类中的next 是一种不好的做法。将它们转换为属性

      【讨论】:

        【解决方案4】:

        正如@Will 所建议的,我将在您的班级实施IEnumerable&lt;int&gt;。这将带您进入强大的 LINQ 世界,您可以在其中将链表转换为数组、列表或仅过滤节点:

        public class MyLinkedList : IEnumerable<int>
        {
            // your code here
        
            public IEnumerator<int> GetEnumerator()
            {
                Node current = headNode;
        
                while(current != null)
                {
                    yield return current.data;
                    current = current.next;
                }
            }
        
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
        

        现在您可以在您的班级中使用所有 LINQ 扩展:

        MyLinkedList myList = new MyLinkedList();
        myList.Add(10);
        myList.Add(15);
        myList.Add(20);
        
        int[] array = myList.ToArray();
        List<int> list = myList.ToList();
        // items above 13
        var items = myList.Where(i => i > 13);
        

        【讨论】:

        • 从未有意识地使用过 Linq,也没有使用过 IEnumberable。为什么要使用&lt;int&gt;
        猜你喜欢
        • 1970-01-01
        • 2017-05-09
        • 2011-01-19
        • 2022-06-30
        • 2015-08-26
        • 2020-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多