【问题标题】:C# Using Class in LinkedList [closed]C#在LinkedList中使用类[关闭]
【发布时间】:2014-12-24 09:53:16
【问题描述】:

我一直试图在我的LinkedList 中添加一个类,但是当我显示全部时,我不断收到0。要么,要么我得到一个错误,说我无法将class 转换为int。请帮帮我。

我正在尝试制作一个程序,以便我可以将书籍输入LinkedList,然后使列表全部显示。我将展示 3 个文件“Program.cs”、“LinkedList.cs”和“Node.cs”,我将保留“Item.cs”,因为我认为它不是导致错误的原因。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookApp
{
    class Program
{
        static void Main(string[] args)
        {
            LinkedList Books = new LinkedList();
            Item book1 = new Item(101, "Avatar: Legend of Korra", 13.50);
            Item book2 = new Item(102, "Avatar: Legend of Aang", 10.60);
            Books.AddFront(book1);
            Books.AddFront(book2);

            Books.DisplayAll();
        }
    }
}

这是我的 LinkedList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BookApp;

class LinkedList
{
    private Node head;  // 1st node in the linked list
    private int count;

    public int Count
    {
        get { return count; }
        set { count = value; }
    }

    public Node Head
    {
        get { return head; }
    }
    public LinkedList()
    {
        head = null;    // creates an empty linked list
        count = 0;
    }

    public void AddFront(Item z)
    {
        Node newNode = new Node(z);
        newNode.Link = head;
        head = newNode;
        count++;

    }

    public void DeleteFront()
    {
        if (count > 0)
        {
            head = head.Link;
            count--;
        }
    }

    public void DisplayAll()
    {
        Node current = head;
        while (current != null)
        {
            Console.WriteLine(current.Data);
            current = current.Link;
        }
    }

}

最后是我的 node.cs

class Node
{
    private int data;

    public int Data
    {
        get { return data; }
        set { data = value; }
    }
    private Node link;
    private BookApp.Item p;

    internal Node Link
    {
        get { return link; }
        set { link = value; }
    }

    public Node(BookApp.Item p)
    {
        // TODO: Complete member initialization
        this.data = p; //Where I got my error about how I cannot convert type BookApp.Item to int
    }
}

【问题讨论】:

  • 您对我们有什么期望?放置断点,开始调试,检查你的变量。
  • 确实知道 .NET 带有一个预构建的 LinkedList<T> class...不是吗?

标签: c# class oop linked-list nodes


【解决方案1】:

node.cs 尝试替换:

private int data;
public int Data
{
    get { return data; }
    set { data = value; }
}

作者:

private BookApp.Item data;
public BookApp.Item Data
{
    get { return data; }
    set { data = value; }
}

您无法将Item 分配给integer,这就是您遇到此错误的原因。

【讨论】:

    【解决方案2】:

    我知道您已经接受了答案,但是如果您希望创建自己的链表实现,我可以建议您使用泛型来允许您将代码与任何数据类型一起使用吗?

    如果您修改了您的 LinkedList 以使其成为 LinkedList

    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    class LinkedList<T>
    {
        private Node<T> head;  // 1st node in the linked list
        private int count;
    
        public int Count
        {
            get { return count; }
            set { count = value; }
        }
    
        public Node<T> Head
        {
            get { return head; }
        }
        public LinkedList<T>()
        {
            head = null;    // creates an empty linked list
            count = 0;
        }
    
        public void AddFront(T z)
        {
            Node<T> newNode = new Node<T>(z);
            newNode.Link = head;
            head = newNode;
            count++;
    
        }
    
        public void DeleteFront()
        {
            if (count > 0)
            {
                head = head.Link;
                count--;
            }
        }
    
        public void DisplayAll()
        {
            Node<T> current = head;
            while (current != null)
            {
                Console.WriteLine(current.Data);
                current = current.Link;
            }
        }
    
    }
    

    和你的节点到一个节点

    class Node<T>
    {
        private T data;
    
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        private Node<T> link;
    
        internal Node<T> Link
        {
            get { return link; }
            set { link = value; }
        }
    
        public Node<T>(T p)
        {
            data = p; 
        }
    }
    

    然后你可以像这样在你的代码中使用它,方法是创建一个 LinkedList ...一个“Item”对象的 LinkedList。

    class Program
    {
            static void Main(string[] args)
            {
                LinkedList<Item> Books = new LinkedList<Item>();
                Item book1 = new Item(101, "Avatar: Legend of Korra", 13.50);
                Item book2 = new Item(102, "Avatar: Legend of Aang", 10.60);
                Books.AddFront(book1);
                Books.AddFront(book2);
    
                Books.DisplayAll();
            }
    }
    

    这种方法的好处是,只需对原始代码进行非常小的更改,您的 LinkedList 现在就可以保存任何类型的对象 - 但仍然是强类型的。它还将您的 LinkedList 和 Node 实现与 BookApp 代码分离。

    【讨论】:

      【解决方案3】:

      对于您的编译器错误:您尝试将 Item 分配给 int,但这是行不通的。

      您可以将private int data [...] public int Data 部分和构造函数替换为以下内容:

      public Item Data { get; set; }
      
      public Node(BookApp.Item item)
      {
          Data = item;
      }
      

      至于DisplayAll()为什么返回0,还得自己调试问题。

      【讨论】:

        【解决方案4】:

        你得到这个错误是正常的,变量 p 是 Item 的类型。您不能将类型 Item 隐式转换为 int。您的 Data 变量必须是 Item 变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多