1

I would like to show the LinkedList data into three separate columns with List1, List2, and List3 as the column heading. I am unable to align the LinkedList data under these headings.

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

2 Answers2

1

There are a lot of issues here

  1. Why do you need linkedList? they have no indexers and makes any solution inefficent
  2. Why are all the lists the same, however maybe this is just an example.
  3. You need to flatten this somehow to write it to the console sequentially

However this might point you in the right direction

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

Or another variation using enumeration in a pivot style operation with 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();
   }
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thank you! I wouldn't use a LinkedList either but this was more for a learning exercise. I didn't think about params. This is helpful. – LearningCSharp Jun 20 '19 at 03:01
0

Here is a variation that uses only enumerators and is therefore more efficient than accessing elements by index:

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

Which you call like

PrintLists( new [] { my_list, my_list, my_list}, 
            new [] { "List One Students: ", "List Two Students: ", "List Three Students: " });     
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70