我一直在尝试向我的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
}
}
你对我们有什么期望?放置断点,开始调试,检查你的变量。 – CodeCaster
*您知道.NET自带预构建的['LinkedList'class](http://msdn.microsoft.com/en-us/library/he2s3bh7%28v=vs.110%29.aspx )...不是吗? –
stakx