需要帮助来找出此搜索程序中的错误。 [英] Need help to figure out the error in this search program.

查看:77
本文介绍了需要帮助来找出此搜索程序中的错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个链表c#代码。我无法弄清楚不打印任何输出的错误。我有一条错误消息,说LinkedList.LinkedList不包含PrintNodes的定义。任何人都可以指出我为什么会收到这个错误,以及我在做错的地方。



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.

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();
    }
  }
}

推荐答案

由于链接列表已在.NET BCL中可用( http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx [ ^ ]),很明显这个是家庭作业,或任何其他类型的学习练习。但是,如果学习是你的目的,你真的,真的需要自己做这个练习,从配方到正确工作的代码。这种结构和所需的技能是整个编程领域最基本的技能之一,所以你真的应该让自己完全适应解决这些问题的所有步骤。



只有一种方法可以实现这种舒适:自己动手做。你的代码的问题在于制作非通用版本毫无意义,但你可以在接下来的步骤中轻松地将其更改为通用版本。至于错误,它们是使用调试器显示和修复的。



此外,当你提问时,你需要解释你的bug是如何表现出来的:你的预期实际上会发生什么,为什么你觉得这种行为是不正确的。



对不起,如果你觉得我的答案令人沮丧。我确信我的观点比你可能得到的任何代码样本都重要。只有你自己完成所有工作才能真正了解。



-SA
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


这篇关于需要帮助来找出此搜索程序中的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆