class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen()
{
}
public string DisplayList()
{
Console.WriteLine(test);
foreach (string word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
Console.WriteLine();
}
public void AddItem(T item)
{
list = new LinkGen<T>(item);
}
public int NumberOfItems()
{
public int Count { get; }
}
public bool IsPresentItem(T item)
{
Link temp = list;
bool result = false;
while (temp != null)
{
if (temp.Data == item)
{
result = true;
break;
}
else
{
temp = temp.Next;
}
}
return result;
}
}
我一直在尝试制作通用链接列表,但不确定为什么会给我一个错误。该错误来自IsPresentItem。另外我不确定何时需要添加到linkListGen。谢谢您的时间
答案 0 :(得分:0)
由于在其中声明了Count属性,因此您缺少了NumberOfItems方法的右括号:
public int NumberOfItems()
{
public int Count { get; }
}
应该是:
public int NumberOfItems()
{
//Do Something
}
public int Count { get; }
整个类应如下所示:
class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen()
{
}
public string DisplayList()
{
Console.WriteLine(test);
foreach (string word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
Console.WriteLine();
}
public void AddItem(T item)
{
list = new LinkGen<T>(item);
}
public int NumberOfItems()
{
//Do Something
}
public int Count { get; }
public bool IsPresentItem(T item)
{
Link temp = list;
bool result = false;
while (temp != null)
{
if (temp.Data == item)
{
result = true;
break;
}
else
{
temp = temp.Next;
}
}
return result;
}
}