views:

37

answers:

2

I was working on a singularly-linked list. While creating my own linked list I got confused on printing the collection of nodes in my custom linked list.

I want to know, does a singularly-linked list display its collection in a LIFO manner like a stack?

below is my Own LinkedList AND node is A Class can anyone tell me Does Singular LinkedList Prints The Collection In Lifo Manner.

class MYlinklist
{
    Node header;

    public void Add(int a)
    {
        Node n = new Node();
        n.element = a;
        n.Next = header;
        header = n;
    }

    public void Print()
    {
        Node n = new Node();
        n = header;
        while (n != null)
        {
            Console.WriteLine(n.element.ToString());
            n = n.Next;
        }
    }
}
A: 

If you are referring to LinkedList<T>, the answer depends on how you add new members.

If you want to make the linked list iterate in LIFO, you can do so by always using AddFirst to add, and RemoveFirst to remove. This will cause it behave very much like a stack.

The nice thing about LinkedList<T>, however, is that you can add anywhere inside of the list as an O(1) operation.


Edit:

If you want this to be FIFO instead, you'll need to change how to add your nodes, and add them at the end of the list, not the start:

class MyLinkedList
{
    Node header;
    Node last;

    public void Add(int a) 
    { 
        Node n = new Node(); 
        n.element = a; 
        n.Next = null; // We'll put this at the end...
        if (last == null)
        { 
            header = n;
            last = n;
        }
        else
        {
             last.Next = n;
             last = n;
        }
    } 

    public void Print() 
    { 
        Node n = new Node(); 
        n = header; 
        while (n != null) 
        { 
            Console.WriteLine(n.element.ToString()); 
            n = n.Next; 
        } 
    } 
}
Reed Copsey
@Reed : I am creating Custom LinkedList, I am not referring it to the .net LinkedList !
Pro_Zeck
I waz Creating My own LinkedList N when I waz Iterating throught My customLinked List I found out that it display's Result in Lifo MANNER
Pro_Zeck
@Pro_Zeck: The way yours works now is going to print LIFO.
Reed Copsey
@Reed: Can You Help Me Out Where i AM going Wrong AND Why this is going in LIFO
Pro_Zeck
@Pro_Zeck: What is the problem? Do you want it to print in FIFO? If so, you need to add your new nodes to the END of the list, not the beginning...
Reed Copsey
@Pro_Zeck:" I just edited my answer to show you how to add at the end, and turn it into FIFO printing...
Reed Copsey
@Reed : I just Want My Print Function To Print The Collection In FIFO manner. Can You Pinpoint the problem in code why My Collection Is Getting Displayed In FIFO?
Pro_Zeck
Reed: Thanks Alot For The Help This Really Helped Me OUT :)
Pro_Zeck
A: 

You're adding nodes at the head of the list (note how you are always setting node.Next to the head of the list).

Then you're iterating through from the head (which is the last element inserted) to the tail.

If you want to iterate in FIFO order, you should do the following:

  1. Maintain a reference to the tail of the list (as well as the head, which you've put in header).
  2. When you add a node, set tail.Next to the new node, and then set tail to point to the new node.
  3. Your iteration function can be unchanged.

Your other option is, instead of maintaining a reference to the tail, just do an iteration through the list each time. But this comes with the tradeoff of needing to go through n-1 elements to add the nth element every time, which means adding many elements is an O(n^2) operation. I would not recommend doing this, but it might be fine for a beginning if you're learning the basics and you're not sure about the tail reference manipulation. In production code, though, you should always have a head and a tail reference for linked lists.

Platinum Azure