2014-12-24 63 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代碼中分離出來。