【发布时间】: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