如何在c#中将链表转换为数组?

时间:2015-06-01 12:37:30

标签: c# arrays linked-list

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

这是我用于链接列表的代码。

langs = {
    "CRA": {flag: '',cs:'4262',as:''},
    "Reviews": {flag: '',cs:'72074301',as:''},
    "Europe": {
        "Bulgarian": {flag: 'bg',cs:'4668',as:''},
        "Croatian": {flag: 'hr',cs:'4671',as:'4707'},
        "Czech": {flag: 'cz',cs:'4365',as:'4553'},
        "Danish": {flag: 'dk',cs:'4357',as:'4526'},
        "Dutch": {flag: 'nl',cs:'4343',as:'4511'},
        "English": {flag: 'gb',cs:'4164',as:'4482'},
        "Finnish": {flag: 'fi',cs:'4092',as:''},
        "Flemish": {flag: 'be',cs:'4500',as:''},
        "French": {flag: 'fr',cs:'4344',as:'4781'},
        "German": {flag: 'de',cs:'4345',as:'4473'},
        "Hungarian": {flag: 'hu',cs:'4221',as:'4729'},
        "Italian": {flag: 'it',cs:'4348',as:'4485'},
        "Latvian": {flag: 'lv',cs:'4674',as:''},
        "Lithuanian": {flag: 'lt',cs:'4673',as:''},
        "Norwegian": {flag: 'no',cs:'4356',as:'4533'},
        "Polish": {flag: 'pl',cs:'4351',as:'4512'},
        "Spanish": {flag: 'es',cs:'4346',as:'4524'},
        "Swedish": {flag: 'se',cs:'4349',as:'4554'},
        "Portuguese (PT)": {flag: 'pt',cs:'4347',as:'4522'},
        "Romanian": {flag: 'ro',cs:'4675',as:''},
        "Turkish": {flag: 'tr',cs:'4355',as:'4530'},
    },
    "Cantonese": {flag: 'cn',cs:'',as:''},
    "Chinese": {flag: 'cn',cs:'4353',as:'4752'},
    "English": {flag: 'us',cs:'4164',as:'4482'},
    "Estonian": {flag: 'ee',cs:'4669',as:''},
    "Hebrew": {flag: 'il',cs:'4670',as:'4727'},
    "Icelanding": {flag: 'is',cs:'4672',as:''},
    "Indonesia": {flag: 'id',cs:'4368',as:'4713'},
    "Japanese": {flag: 'jp',cs:'4265',as:'4717'},
    "Korean": {flag: 'kp',cs:'4366',as:''},
    "Malay": {flag: 'ms',cs:'4367',as:''},
    "Portuguese (BR)": {flag: 'br',cs:'4552',as:'4781'},
    "Russian": {flag: 'ru',cs:'4350',as:'4559'},
    "Serbian": {flag: 'rs',cs:'4677',as:''},
    "Slovenian": {flag: 'si',cs:'4676',as:''},
    "Tagalog": {flag: '',cs:'4678',as:''},
    "Thai": {flag: 'th',cs:'4354',as:'4555'},
    "Vietnamese": {flag: 'vn',cs:'4679',as:''}
};


$.each( langs, function( key, l ) {
    //console.log('length L:',Object.keys(l).length,l);
    if(Object.keys(l).length > 3)
    {
            if(key == "Europe") flag = 'eu';
            else flag = '';
            html  = '<li class="dropdown-submenu">';
            html += '  <a href="#"><span class="flag flag-'+flag+'"></span> '+key+'</a>';
            html += '  <ul class="dropdown-menu">';

            $.each( l, function( key2, l2 ) {
                console.log(key2,l2);
                if(l2.cs.length < 8)
                    l2.cs = '7108' + l.cs;
                if(l2.as.length < 8 && l2.as.length > 0)
                    l.as = '7108' + l.as;
                if(l2.as !== '')
                {
                    html += '<li class="dropdown-submenu">';
                    html += '  <a href="tel:'+l.cs+'"><span class="flag flag-'+l2.flag+'"></span> '+key2+'</a>';
                    html += '  <ul class="dropdown-menu">';
                    html += '    <li><a href="tel:'+l2.cs+'">CS: '+l2.cs.substring(4,8)+'</a></li>';
                    html += '    <li><a href="tel:'+l2.as+'">AS: '+l2.as.substring(4,8)+'</a></li>';
                    html += '  </ul>';
                    html += '</li>';
                }
                else
                {
                    html += '<li data-lang="'+l2.flag+'">';
                    html += '  <a href="tel:'+l2.cs+'"><span class="flag flag-'+l2.flag+'"></span> '+key2+'</a>';
                    html += '</li>';
                }


            });

            $('#btb_lang_list').append(html);
    }
    else
    {
        if(l.cs.length < 8)
            l.cs = '7108' + l.cs;
        if(l.as.length < 8 && l.as.length > 0)
            l.as = '7108' + l.as;
        if(l.as !== '')
        {
            html  = '<li class="dropdown-submenu">';
            html += '  <a href="tel:'+l.cs+'"><span class="flag flag-'+l.flag+'"></span> '+key+'</a>';
            html += '  <ul class="dropdown-menu">';
            html += '    <li><a href="tel:'+l.cs+'">CS: '+l.cs.substring(4,8)+'</a></li>';
            html += '    <li><a href="tel:'+l.as+'">AS: '+l.as.substring(4,8)+'</a></li>';
            html += '  </ul>';
            html += '</li>';
        }
        else
        {
            html  = '<li data-lang="'+l.flag+'">';
            html += '  <a href="tel:'+l.cs+'"><span class="flag flag-'+l.flag+'"></span> '+key+'</a>';
            html += '</li>';
        }

        $('#btb_lang_list').append(html);
    }
});

4 个答案:

答案 0 :(得分:0)

  1. 在您的LinkedList上实施IEnumerable
  2. using System.Linq;
  3. 在LinkedList上调用ToArray
  4. 实施IEnumerable是微不足道的。只需从列表中的根节点yield return Node.data;开始,然后移动到下一个节点。冲洗并重复,直到下一个节点为null

答案 1 :(得分:0)

为您的班级添加新方法。

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;
  }

答案 2 :(得分:0)

呃......那样的东西?

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;
  }
}

P.S。 公共字段(例如next类中的Node不良做法。将它们转换为属性

答案 3 :(得分:0)

正如@Will建议的那样,我会在你的班级实施IEnumerable<int>。这将带您进入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);