1

我想将 LinkedList 数据显示为三个单独的列,其中 List1、List2 和 List3 作为列标题。我无法在这些标题下对齐 LinkedList 数据。

        void LoopThruLinkedList(LinkedList<string> strList)
        {
            foreach(string str in strList)
            {
                Console.WriteLine(string.Format($"{str, -20}"));
            }
            //Console.WriteLine();
        }

        // Creating a linkedlist
        // Using LinkedList class
        LinkedList<string> my_list = new LinkedList<string>();

        // Adding elements in the LinkedList
        // Using AddLast() method
        my_list.AddLast("Zoya");
        my_list.AddLast("Shilpa");
        my_list.AddLast("Rohit");
        my_list.AddLast("Rohan");
        my_list.AddLast("Juhi");
        my_list.AddLast("Zoya");
        my_list.AddLast("Rohit");

        string List1 = "List One Students: ";
        string List2 = "List Two Students: ";
        string List3 = "List Three Students: ";

        Console.WriteLine($"{List1, -20}{List2, -20}{List3, -20}");


        // Accessing the elements of LinkedList using the foreach loop
        LoopThruLinkedList(my_list);
        LoopThruLinkedList(my_list);
        LoopThruLinkedList(my_list);
4

2 回答 2

1

这里有很多问题

  1. 为什么需要linkedList?他们没有索引器,使任何解决方案效率低下
  2. 为什么所有列表都相同,但这可能只是一个示例。
  3. 您需要以某种方式将其展平以按顺序将其写入控制台

但是,这可能会为您指明正确的方向

void LoopThruLinkedList(params LinkedList<string>[] strLists)
{
   var max = strLists.Max(x => x.Count());

   for (int i = 0; i < max; i++)
   {
      foreach (var item in strLists)
         Console.Write($"{(item.Count > i ? item.ElementAt(i) : ""),-20}");
      Console.WriteLine();
   }
}

// Creating a linkedlist
// Using LinkedList class
LinkedList<string> my_list = new LinkedList<string>();

// Adding elements in the LinkedList
// Using AddLast() method
my_list.AddLast("Zoya");
my_list.AddLast("Shilpa");
my_list.AddLast("Rohit");
my_list.AddLast("Rohan");
my_list.AddLast("Juhi");
my_list.AddLast("Zoya");
my_list.AddLast("Rohit");

string List1 = "List One Students: ";
string List2 = "List Two Students: ";
string List3 = "List Three Students: ";

Console.WriteLine($"{List1,-20}{List2,-20}{List3,-20}");


// Accessing the elements of LinkedList using the foreach loop
LoopThruLinkedList(my_list, my_list, my_list);

或在枢轴样式操作中使用枚举的另一种变体GroupBy

void LoopThruLinkedList(params LinkedList<string>[] strLists)
{
   var results = strLists
                .SelectMany(inner => inner.Select((item, index) => new { item, index }))
                .GroupBy(i => i.index, i => i.item)
                .Select(g => g.ToList());

   foreach (var list in results)
   {
      foreach (var item in list)
         Console.Write($"{item,-20}");
      Console.WriteLine();
   }
}
于 2019-06-20T02:34:36.153 回答
0

这是一个仅使用枚举器的变体,因此比通过索引访问元素更有效:

void PrintLists(LinkedList<string>[] lists, string[] captions)
{
    //Find the necessary column widths
    var columnWidths = new int[lists.Length];
    for(int i = 0; i < lists.Length; ++i)
    {
        columnWidths[i] = captions[i].Length;
        foreach (var s in lists[i])
            columnWidths[i] = Math.Max(columnWidths[i], s.Length);
        columnWidths[i] += 2; //spacing
    }

    //Print the headings
    for(int i = 0; i < lists.Length; ++i)
        Console.Write(captions[i].PadRight(columnWidths[i]));
    Console.WriteLine();

    //Initialize iterators
    var iterators = new LinkedList<string>.Enumerator[lists.Length];
    var iteratorsValid = new bool[lists.Length];
    for (int i = 0; i < lists.Length; ++i)
    {
        iterators[i] = lists[i].GetEnumerator();
        iteratorsValid[i] = iterators[i].MoveNext();
    }

    //Print the rest of the table
    while (iteratorsValid.Any(b => b))
    {
        for (int i = 0; i < lists.Length; ++i)
        {
            if (iteratorsValid[i])
            {
                var item = iterators[i].Current;
                Console.Write(item.PadRight(columnWidths[i]));
                iteratorsValid[i] = iterators[i].MoveNext();
            }
            else
                Console.Write(new String(' ', columnWidths[i]));
        }
        Console.WriteLine();
    }
}

你叫什么

PrintLists( new [] { my_list, my_list, my_list}, 
            new [] { "List One Students: ", "List Two Students: ", "List Three Students: " });     
于 2019-06-20T02:45:12.240 回答