Click here to Skip to main content
16,020,345 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this linked list c# codes. I couldn't figure out the error for not printing any output. I have an error message saying that LinkedList.LinkedList doesn't contain a definition for PrintNodes. Can anyone pointed out why I am getting that error, and where I am doing mistake.

C#
public class Node
{
    public object data;
    public Node next;
    public Node(object data)
    {
        this.data = data;
    }
}
public class LinkedList
{
    Node head;
    Node current;
    public Node Head
    {
        get { return head; }
    }
    public void Add(Node n)
    {
        if (head == null)
        {
            head = n; 
            current = head; 
        }
        else
        {
            current.next = n; 
            current = current.next;  
        }
    }

    public void MergeSortedList(Node first, Node second)
    {

        if (Convert.ToInt32(first.next.data.ToString())
                > Convert.ToInt32(second.data.ToString()))
        {
            Node t = first;
            first = second;
            second = t;
        }
        head = first;
        while ((first.next != null) && (second != null))
        {
            if (Convert.ToInt32(first.next.data.ToString())
                < Convert.ToInt32(second.data.ToString()))
            {
                first = first.next; 
            }
            else
            {
                Node n = first.next;
                Node t = second.next;
                first.next = second;
                second.next = n;
                first = first.next;
                second = t;
            }
        }
        if (first.next == null) 
            first.next = second;
    }

    static void Main()
    {
        LinkedList l1 = new LinkedList();

        l1.Add(new Node("2"));
        l1.Add(new Node("3"));
        l1.Add(new Node("4"));
        l1.Add(new Node("5"));
        l1.Add(new Node("8"));
        l1.Add(new Node("100"));
        l1.Add(new Node("120"));

        LinkedList l2 = new LinkedList();
        l2.Add(new Node("10"));
        l2.Add(new Node("30"));
        l2.Add(new Node("34"));
        LinkedList list = new LinkedList();
        list.MergeSortedList(l1.Head, l2.Head);
        list.PrintNodes();
        Console.ReadLine();
    }
  }
}
Posted
Comments
[no name] 14-Jul-14 18:44pm    
You error message means exactly what it says. Your LinkedList class does not contain a method named PrintNodes. Instead of trying to learn programming by copy/paste and then asking us to try and teach you programming (as evidenced by your comment) get yourself a decent book on programming and work through it. You will not learn programming by copy/paste and we cannot teach you programming one question at a time, especially in a forum posting.

1 solution

As linked list is already available in .NET BCL (http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx[^]), it's pretty apparent that this is a home assignment, or any other kind of learning exercise. But if learning is your purpose, you really, really need to do this exercises all by yourself, from the formulation to correctly working code. This kind of structure and required skills are one of the most basic in the whole programming field, so you really should made yourself fully comfortable with all the steps in solving of such problems.

And there is one and the only one way to achieve this comfort: doing it all by yourself. The problem of your code is that making non-generic version makes no sense, but you can easily change it to generic on next steps. As to the mistakes, they are revealed and fixed using the debugger.

Also, when you ask questions, you need to explain how your bug is manifested: what you expected to get, what happens in fact, and why do you feel this behavior is incorrect.

Sorry if you find my answer frustrating. I am really sure that my points are more important than any code sample you could possibly get. You only really learn when you do all the job by yourself.

—SA
 
Share this answer
 
v2
Comments
RalvarezHose 14-Jul-14 18:30pm    
I understand your point. This is not an homework assignment. I got this code from online and just wanted to see how it works. I found lots of other codes that work and doesn't work. But for that doesn't work that error fixed in the comment by another user. My programming is not good. In Main list.PrintNodes() has been called but LinkedList class has no such method defined. But I don't know where exactly to add it. I have been trying for last two hours. I just wanted to know, that way if I find something like this in next problem I will be able to solve it.
Matt T Heffron 14-Jul-14 18:49pm    
OK, so this isn't homework, but IT IS a LEARNING EXERCISE! (Self-imposed, but a learning exercise just the same.)
So Sergey's comments about the best thing being to work it out yourself still stand, but I'll provide a hint.
In Main, right before calling list.PrintNodes(), is a call to list.MergeSortedList(l1.Head, l2.Head).
So the PrintNodes() method would seem to belong in the same place in the code structure as MergeSortedList()...
Sergey Alexandrovich Kryukov 14-Jul-14 19:09pm    
I really appreciate that you created a self-imposed learning exercise on this really important topic. (I added in my answer "or any other kind of learning exercises", not necessarily an assignment.) I also really appreciate your self-criticizing estimation of your programming skills. Those are really important prerequisites to success. Many inquirers of this forum lack all this, but you have it. I am pretty much sure you will be able to do a little more effort and debug it, and also get considerable debugging skills.

Wish you the best of luck.

—SA

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