请解释一下这项任务的内容?
“创建一个通用链表类,使我们能够创建不同类型的链对象。”
我们是否需要创建一个类型的链表并实现列表界面?
class LinkedList<T>:IList<T>
{
//implement interface methods here?
}
请举例。
答案 0 :(得分:2)
链表是一个特殊列表,列表中的每个元素(或每个元素容器对象)都有一个直接引用(“链接”)到列表中的下一个项目。这种类型的列表不是使用数组实现的。
单链表通常只有一个指向下一项的链接,最后一项为空,表示列表的结尾。
双向链接列表具有指向下一个和上一个项目的链接,并带有空值以指示列表的每一端。
链表的优点是插入和删除非常快。迭代整个列表也具有良好的性能,但非线性搜索可能很慢。
通常,链表的实现应该实现IEnumerable<T>
接口。实施IList<T>
会促使使用效率低下的线性搜索。
链接列表的.NET实现具有以下声明(减去一些无关紧要的内容)。
LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
与IList<T>
界面一样,我不明白为什么ICollection
&amp; ICollection<T>
接口已经实现,但它们已经实现了。
元素容器对象(具有链接)如下所示:
public sealed class LinkedListNode<T>
{
public LinkedListNode<T> Next { get; }
public LinkedListNode<T> Previous { get; }
public T Value { get; set; }
}
怎么样?
答案 1 :(得分:2)
您需要创建自己的新类通用链接列表。这是完整的解决方案。根据以上评论..希望它有所帮助..
class Program
{
static void Main(string[] args)
{
// string linked List
GenericsLinkedList<string> stringLinkedList = new GenericsLinkedList<string>(); //object 1
string s1 = "Yes";
string s2 = "No";
string s3 = "True";
string s4 = "False";
stringLinkedList.AddHead(s1);
stringLinkedList.AddHead(s2);
stringLinkedList.AddHead(s3);
stringLinkedList.AddHead(s4);
//display List
foreach (string str in stringLinkedList)
{
Console.WriteLine("----"+str);
}
//Integer LinkedList
GenericsLinkedList<int> integerList = new GenericsLinkedList<int>();
int n1 = 1;
int n2 = 2;
int n3 = 3;
integerList.AddHead(n1);
integerList.AddHead(n2);
integerList.AddHead(n3);
foreach (int Intger in integerList)
{
Console.WriteLine("----" + Intger);
}
Console.ReadKey();
}
}
// Generic Linked List
class GenericsLinkedList<T>
{
class LinkedlistNode
{
private LinkedlistNode next;
private T item;
public LinkedlistNode(T t)
{
next = null;
item = t;
}
public LinkedlistNode Next
{
get
{
return next;
}
set
{
next = value;
}
}
public T Item
{
get
{
return item;
}
set
{
item = value;
}
}
}
private LinkedlistNode head;
public GenericsLinkedList()
{
head = null;
}
public void AddHead(T t)
{
LinkedlistNode node = new LinkedlistNode(t);
node.Next = head;
head = node;
}
public IEnumerator<T> GetEnumerator()
{
LinkedlistNode current = head;
while(current != null)
{
yield return current.Item;
current = current.Next;
}
}
}
答案 2 :(得分:1)
namespace GenericLinkedList
{
// generic linked list node
public class GenericNode<T>
{
public T data;
public GenericNode<T> nextNode = null;
public GenericNode(T data)
{
this.data = data;
}
}
// generic linked list
public class GenericLinkedList<T>
{
private GenericNode<T> head = null;
public void Add(T newListItem)
{
if (head == null)
{
head = new GenericNode<T>(newListItem);
}
else
{
GenericNode<T> curr = head;
while (curr.nextNode != null)
{
curr = curr.nextNode;
}
curr.nextNode = new GenericNode<T>(newListItem);
}
}
public void DisplayNodes()
{
GenericNode<T> curr = head;
while (curr != null)
{
System.Console.WriteLine(curr.data);
curr = curr.nextNode;
}
}
}
class TestGenericLinkedList
{
static void Main(string[] args)
{
GenericLinkedList<System.Object> gll = new GenericLinkedList<System.Object>();
gll.Add(12);
gll.Add("string");
gll.Add(false);
gll.DisplayNodes();
}
}
}
}
答案 3 :(得分:1)
这可能很愚蠢,因为这段代码以某种方式消除了泛型的含义,但我认为它们的意思是这个。
class Generic<T>
{
public T t;
}
static void Main(string[] args)
{
Generic<object>[] genericarray = new Generic<object>[3];
for (int i = 0; i < genericarray.Length; i++)
genericarray[i] = new Generic<object>();
int a = 0;
double b = 0.515151513163;
string c = "s.dçfslsfn";
genericarray[0].t = a;
genericarray[1].t = b;
genericarray[2].t = c;
}
答案 4 :(得分:0)
对于链接列表,我通常不建议实现IList
,因为IList
强烈暗示对列表中任何成员的持续时间访问。我建议实施ICollection
,然后添加对链接列表不可或缺的其他方法,例如PushFront
,PopBack
等。您可以查看{{1}的MSDN文档用于比较的类(http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx),尽管你应该单独实现你的类。
答案 5 :(得分:0)
这应该这样做
http://msdn.microsoft.com/en-us/library/0x6a29h6.aspx
// type parameter T in angle brackets
公共类GenericList { //嵌套类在T上也是通用的 私有类Node { // T用于非泛型构造函数。 公共节点(T t) { next = null; data = t; }
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
// T as private member data type.
private T data;
// T as return type of property.
public T Data
{
get { return data; }
set { data = value; }
}
}
private Node head;
// constructor
public GenericList()
{
head = null;
}
// T as method parameter type:
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
答案 6 :(得分:0)
static void Main()
{
var list = new LinkedList<object>();
list.AddLast("My string");
list.AddLast(1.5);
list.AddLast(2);
list.AddLast(true);
var en = list.GetEnumerator();
while (en.MoveNext())
Console.WriteLine(en.Current);
Console.ReadKey();
}
答案 7 :(得分:0)
以下实施的更多功能 http://msdn.microsoft.com/en-us/library/0x6a29h6.aspx
public class GenericList<T>
{
private class Node
{
public Node(T t)
{
next = null;
data = t;
}
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
}
private Node head;
private Node tail;
private int count;
public GenericList()
{
head = null;
tail = null;
count = 0;
}
public void AddHead(T t)
{
if (head == null)
head = tail = new Node(t);
else
{
Node n = new Node(t);
n.Next = head;
head = n;
}
count++;
}
public void AddTail(T t)
{
if(tail == null)
{
head = tail = new Node(t);
}
else
{
Node n = new Node(t);
tail.Next = n;
tail = n;
}
count++;
}
public void InsertAt(T t,int index)
{
if (index < 0 || index > count)
throw new ArgumentOutOfRangeException();
else if (index == 0)
AddHead(t);
else if (index == count)
AddTail(t);
else
{
Node currentNode = head;
for (int i = 0; i < index - 1; i++)
{
currentNode = currentNode.Next;
}
Node newNode = new Node(t);
newNode.Next = currentNode.Next;
currentNode.Next = newNode;
}
count++;
}
public void Reverse()
{
if (head == null || head.Next == null)
return;
tail = head;
Node previousNode = null;
Node currentNode = head;
Node nextNode = head.Next;
while (currentNode != null)
{
currentNode.Next = previousNode;
if (nextNode == null)
break;
previousNode = currentNode;
currentNode = nextNode;
nextNode = nextNode.Next;
}
head = currentNode;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}