C#在LinkedList中使用类
我一直在尝试将一个类添加到我的C#在LinkedList中使用类,c#,class,oop,linked-list,nodes,C#,Class,Oop,Linked List,Nodes,我一直在尝试将一个类添加到我的LinkedList,但当我显示全部时,我一直得到0。否则我会收到一个错误,说我无法将类转换为int。请帮帮我 我正在尝试制作一个程序,通过该程序,我可以将书籍输入到链接列表,然后使列表显示所有书籍。我将显示3个文件“Program.cs”、“LinkedList.cs”和“Node.cs”,我将不显示我的“Item.cs”,因为我认为这不是导致错误的原因 using System; using System.Collections.Generic; using S
LinkedList
,但当我显示全部时,我一直得到0
。否则我会收到一个错误,说我无法将类
转换为int
。请帮帮我
我正在尝试制作一个程序,通过该程序,我可以将书籍输入到链接列表
,然后使列表显示所有书籍。我将显示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
}
}
对于编译器错误:您试图将
项
分配为int
,这将不起作用
您可以用以下内容替换私有int数据[…]公共int数据
部分和构造函数:
public Item Data { get; set; }
public Node(BookApp.Item item)
{
Data = item;
}
至于为什么
DisplayAll()
返回0
,您必须自己调试该问题。在node.cs
中尝试替换:
private int data;
public int Data
{
get { return data; }
set { data = value; }
}
作者:
您不能将
项
分配给整数
,这就是您面临此错误的原因。如果出现此错误,则是正常的,变量p是项的类型。您不能将类型Item隐式转换为int。您的数据变量必须是Item变量。我知道您已经接受了答案,但是如果您希望创建自己的链表实现,我可以建议您使用泛型来允许您将代码用于任何数据类型吗
如果您修改了LinkedList使其成为LinkedListusing 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;
}
}
}
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
类链接列表
{
私有节点头;//链表中的第一个节点
私人整数计数;
公共整数计数
{
获取{返回计数;}
设置{count=value;}
}
公共节点头
{
获取{返回头;}
}
公共链接列表()
{
head=null;//创建一个空链表
计数=0;
}
公共空位正面(T z)
{
Node newNode=新节点(z);
Link=head;
头=新节点;
计数++;
}
公共前线
{
如果(计数>0)
{
head=head.Link;
计数--;
}
}
public void DisplayAll()
{
节点电流=头;
while(当前!=null)
{
Console.WriteLine(当前数据);
current=current.Link;
}
}
}
将您的节点连接到一个节点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;
}
}
类节点
{
私有T数据;
公共T数据
{
获取{返回数据;}
设置{data=value;}
}
专用节点链路;
内部节点链接
{
获取{返回链接;}
设置{link=value;}
}
公共节点(TP)
{
数据=p;
}
}
然后,您可以通过创建LinkedListclass 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();
}
}
类程序
{
静态void Main(字符串[]参数)
{
LinkedList Books=新建LinkedList();
项目book1=新项目(101,“阿凡达:科拉传奇”,13.50);
项目book2=新项目(102,“阿凡达:阿昂传奇”,10.60);
书籍。AddFront(第1册);
书籍。AddFront(第2册);
Books.DisplayAll();
}
}
这种方法的好处是,只需对原始代码进行很小的更改,LinkedList就可以保存任何类型的对象,但仍然是强类型的。它还将LinkedList和节点实现与BookApp代码分离。您对我们有什么期望?放置断点,开始调试,检查变量。您知道.NET附带了一个预构建的。。。你不觉得吗?
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();
}
}