Node order

In a Linked List, is the "First Node" the one you added last, or the one you added first?

When you print out a linked list, does it normally print out with the one you entered last as the top of the list, or the one you entered first at the top of the list?

thanks.
LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?
 
käµfm³d 👽Commented:
but I don't think the language matters, because wouldn't the answer apply to any linked list in any answer?
A linked list is just a data structure, and it has many varieties (e.g. singly-linked, doubly-linked, etc.). Essentially, it is the concept of one node pointing to the next node, which points to the next node, etc. It's up to the person creating the implementation as to how it behaves when you add nodes. So in short, no, the answer would not apply to any linked list in any answer.

Also, I asked:
>>When you print out a linked list, does it normally print out with the one you entered last as the top of the list, or the one you entered first at the top of the list?
I realize that, but my answer depended on your answer to my question  = )

It will print out in whatever order you added the nodes. However, this does not mean the order that you wrote them in code. The LinkedList class (I'm referring to the generic version now) has four "Add" methods:

AddAfter
AddBefore
AddFirst
AddLast

You could end up with many different orderings depending on which of the above four methods you used to insert into your list. If you only ever used AddLast, then you would print the list out in the order you added the nodes as written in your code. Make the appropriate assumptions as to ordering if you decided to use a method other than AddLast.
0
 
käµfm³d 👽Commented:
It depends on how the structure was designed, but I believe generally, the "first node" is the head of the list, and that would be the one you added first--barring any re-ordering.

Which language are you using? You indicated C# and C++ in the zones.
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
I happen to be using C#, but I don't think the language matters, because wouldn't the answer apply to any linked list in any answer?

Also, I asked:
>>When you print out a linked list, does it normally print out with the one you entered last as the top of the list, or the one you entered first at the top of the list?

thanks.
 
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You'd normally print them in the same order that they were entered.
0
 
käµfm³d 👽Commented:
As an example, here is what you get using AddFirst only:

class Program
{
    static void Main(string[] args)
    {
        LinkedList<int> myList = new LinkedList<int>();

        myList.AddFirst(9);
        myList.AddFirst(8);
        myList.AddFirst(7);
        myList.AddFirst(6);
        myList.AddFirst(5);
        myList.AddFirst(4);
        myList.AddFirst(3);
        myList.AddFirst(2);
        myList.AddFirst(1);

        foreach (var item in myList)
        {
            Console.WriteLine(item);
        }

        Console.ReadKey();
    }
}

Open in new window


AddFirst
...and if you used AddLast:

class Program
{
    static void Main(string[] args)
    {
        LinkedList<int> myList = new LinkedList<int>();

        myList.AddLast(9);
        myList.AddLast(8);
        myList.AddLast(7);
        myList.AddLast(6);
        myList.AddLast(5);
        myList.AddLast(4);
        myList.AddLast(3);
        myList.AddLast(2);
        myList.AddLast(1);

        foreach (var item in myList)
        {
            Console.WriteLine(item);
        }

        Console.ReadKey();
    }
}

Open in new window


AddLast
0
 
käµfm³d 👽Commented:
If you want to guarantee a FIFO or a LIFO ordering, then you need to use a Queue or Stack data structure, respectively.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good answers kaufmed.  I guess I should clarify my statement.  By "order that they were entered", I mean the order that they are linked, starting with the item at the beginning of the list (the "head").
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.