Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public void display()
        {
            Node temp;
                temp = head;
            Console.WriteLine("\t\t\t\tLinked list\n");
            while (temp.next != null)
            {

                Console.WriteLine(temp.id);
                temp = temp.next;


            }

        }


What is wrong with this code ?? Im a newbie prgrammer so plz help me !!
Posted
Comments
[no name] 11-Dec-11 9:49am    
From where are you getting this 'head'
temp = head;
legend_123 11-Dec-11 9:53am    
class Node
{
public int id;
public Node next;
public Node(int _id)
{
id = _id;
next = null;
}
class link
{
public Node head;
public Node curr;
public link()
{
curr = head = null;
}
Sergey Alexandrovich Kryukov 11-Dec-11 21:42pm    
Why doing it at all? Bad idea!
--SA

A better start would be:

C#
public void display()
    {
    Node temp = head;
    Console.WriteLine("\t\t\t\tLinked list\n");
    while (temp != null)
        {
        Console.WriteLine(temp.id);
        temp = temp.next;
        }
    }
This way, you check if the current node is available, before using it! Your version makes sure the next is there, but doesn't check the head for empty.
 
Share this answer
 
Comments
legend_123 11-Dec-11 9:59am    
i hav inserted many nodes but this function is only displaying the head node not the others i want to display all the node which i hav inserted
OriginalGriff 11-Dec-11 11:13am    
How did you insert them?
legend_123 12-Dec-11 12:40pm    
public void add(int _id)
{
if (head == null)
{
head = new Node(_id);
curr = head;
}
else
{
curr.next = new Node(_id);
}
}
OriginalGriff 12-Dec-11 13:32pm    
There is your problem:
curr.next = new Node(_id);
You initially put a new node in as the head, that's fine.
The next time you add one, you put it into curr.next - that's fine.
The next time, you overwrite that node and put another new one into curr.next.
You need to move curr to curr.next when you add a new node to it.
Sergey Alexandrovich Kryukov 11-Dec-11 21:52pm    
This method itself should not be placed in the list type at all, please see my answer.
--SA
From the standpoint of architecture, the whole idea to make I/O a part of linked list code is totally wrong! I/O should be fully decoupled from data structures. All you need it the support of IEnumerable<out T> where T is the type of the list element, see http://msdn.microsoft.com/en-us/library/9eekhta0.aspx[^].

See also: http://msdn.microsoft.com/en-us/library/dd799517.aspx[^].

When this is done, you can display the list just using foreach statement, see http://msdn.microsoft.com/en-us/library/ttw7t8t6%28v=VS.100%29.aspx[^].

By the way, the linked list type should be generic, with the type of the list element being a generic parameter.

—SA
 
Share this answer
 

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