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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You'd normally print them in the same order that they were entered.
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.