需要帮助来找出此搜索程序中的错误

时间:2014-07-14 21:53:46

标签: c#

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

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

1 个答案:

答案 0 :(得分:0)

Main中,您调用list.PrintNodes(),但您的LinkedList类没有定义此类方法,因此异常,完全如下所示:

  

LinkedList.LinkedList不包含PrintNodes的定义