我一直在嘗試向我的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