Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Trying to print LinkedList with MessageBox. Typically I use this method to display arrays.

C#
private void DisplayList<T>(LinkedList<T> list)
        {
            string strDisplay = string.Join(Environment.NewLine, list);
            MessageBox.Show(strDisplay);
        }



However in this instance I want to reverse the order of LinkedList before displaying. The below doesn't compile. Error is that last strDisplay is unassigned. Any suggestions how best to do this?

XML
private void displayListReversed<T>(LinkedList<T> list)
        {
            LinkedListNode<T> currentNode = list.Last;
            string strDisplay;
            while (currentNode != null)
            {
                strDisplay = strDisplay + Environment.NewLine + currentNode.Value;
                currentNode = currentNode.Previous; // go backwards through nodes
            }

            MessageBox.Show(strDisplay); // doesn't work
        }
Posted
Comments
[no name] 29-Sep-14 16:15pm    
string strDisplay = string.Empty;
needyt 29-Sep-14 16:19pm    
Thanks for quick reply. Worked like a charm.

I have one more question if you'll bear with me... why did initializing it as empty work? Whats the difference?
Sergey Alexandrovich Kryukov 29-Sep-14 16:30pm    
There is no "difference". There is no default initialization for variables, not allowed by syntax (but type members are all initialized, say, with null).
—SA
[no name] 29-Sep-14 19:22pm    
The difference is that string strDisplay; initializes your string to nothing and string strDisplay = string.Empty; initializes the string to an empty string which is something and not nothing.
BillWoodruff 29-Sep-14 19:15pm    
CodeProject is your friend:

http://www.codeproject.com/Articles/27742/How-To-Reverse-a-Linked-List-Different-Ways

1 solution

Hi,

I would like to suggest a more elegant implementation.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace LinkedListTest
{
    public partial class MainWindow : Window
    {
        LinkedList<int> linkedList;

        public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            linkedList = new LinkedList<int>();

            for(int i=0; i<10; i++)
            {
                linkedList.AddLast(i);
            }

            DisplayList(linkedList);
            DisplayListReversed(linkedList);
        }

        private void DisplayList<T>(LinkedList<T> list)
        {
            string strDisplay = string.Join(Environment.NewLine, list);
            MessageBox.Show(strDisplay);
        }

        private void DisplayListReversed(LinkedList<int> linkedList)
        {
            linkedList = new LinkedList<int>(linkedList.Reverse());
            DisplayList(linkedList);
        }
    }
}


Happy coding,

Stephan
 
Share this answer
 
v2
Comments
BillWoodruff 14-Nov-14 9:10am    
Very dangerous around here to describe your own code as elegant :)

Note that in this case the OP (and you) use a generic method but no consideration is given to the possibility of this being called on some List of Types/objects that have no implementation of 'ToString.

You get my vote of #4 for use of string.Join which may be a bit slower than using a StringBuilder: see:

http://stackoverflow.com/a/585916/133321

But, is more expressive code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900