2014-12-24 33 views
0

我一直在尝试向我的LinkedList中添加一个类,但是当我全部显示时我总是收到0。无论是或我得到一个错误,说我不能将class转换为int。请帮助我。C#在LinkedList中使用类

我试图制作一个程序,让我可以将书籍放入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 
    } 
} 
+2

你对我们有什么期望?放置断点,开始调试,检查你的变量。 – CodeCaster

+0

*您知道.NET自带预构建的['LinkedList 'class](http://msdn.microsoft.com/en-us/library/he2s3bh7%28v=vs.110%29.aspx )...不是吗? – stakx

回答

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

不能分配Iteminteger,这就是为什么你面对的这个错误。

0

为你的编译错误:你想assing的Itemint ,这将无法正常工作。为什么DisplayAll()回报0,你必须自己调试问题

public Item Data { get; set; } 

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

至于:

您可以用下面的更换private int data [...] public int Data部分和构造。

0

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

1

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

如果你修改了LinkedList的,使其在LinkedList < T>:

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

} 

和你的节点到节点< T>:

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> ...'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代码中分离出来。