2010-03-10 95 views
0

请解释这个任务是关于什么?C# - MCTS - 创建一个通用链接列表类创建一个对象链

“创建一个通用链接列表类,使我们能够创建不同类型的链对象。”

我们是否需要创建一个类型的链表和实现列表接口的类?

class LinkedList<T>:IList<T> 
{ 
    //implement interface methods here? 
} 

请举例。

TIA

+0

u能阐述“一个没有,通常情况下,要继承不同的数据结构的一个现成的榜样”吗? – SoftwareGeek 2010-03-10 23:23:17

+0

对不起,将它误读为List。其实,我现在回过头来看看编辑历史... – 2010-03-10 23:27:48

+0

请确保您的问题描述是准确的。你的意思是“可以创建列表类的不同实例,其中每个实例可以存放单一类型的元素,但列表的不同实例可以存放不同的元素”,或者“列表类的每个实例可以存放不同类型元素“?换句话说,你想要一个强类型的列表吗? (我怀疑你想要一个强类型的列表,但在这方面文字措辞不佳) – 2010-05-13 22:09:26

回答

0

对于一个链表,我通常不会建议实施IList,因为IList强烈暗示到列表中的任何成员,固定时间的访问。我建议实施ICollection,然后添加对链接列表不可或缺的其他方法,例如PushFront,PopBack等。您可以查看LinkedList<T>类的MSDN文档以进行比较(http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx),但应该分别实施您的类。

2

链接列表是一个特殊列表,其中列表中的每个元素(或每个元素的容器对象)都有一个直接引用(“链接”)到列表中的下一个项目。这种类型的列表不是使用数组实现的。

一个单链表通常只有一个链接到下一个项目,最后一个项目为空以指示列表的结束。

一个双向链接列表有一个指向下一个和前一个项目的空白链接,用于指示列表的每一端。

链接列表的优点是插入和删除非常快。遍历整个列表也具有良好的性能,但非线性搜索可能会很慢。

通常,链接列表的实现应实现接口IEnumerable<T>。实施IList<T>会促进使用低效的线性搜索。

链接列表的.NET实现具有以下声明(减去一些不相关的东西)。

LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable 

如同IList<T>接口,我不明白为什么ICollection & ICollection<T>接口已经实现,但他们有。

元素容器对象(即有链接)看起来是这样的:

public sealed class LinkedListNode<T> 
{ 
    public LinkedListNode<T> Next { get; } 
    public LinkedListNode<T> Previous { get; } 
    public T Value { get; set; } 
} 

是如何形成的?

0

这应该这样做

http://msdn.microsoft.com/en-us/library/0x6a29h6.aspx

// type parameter T in angle brackets 

公共类GenericList {// 嵌套类也是通用的T. private class Node // T用于非泛型构造函数。 public Node(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; 
    } 
} 

}

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; 
     } 

    } 

} 
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(); 
    } 
    } 
} 
} 
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; 
} 
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(); 
    } 
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; 
     } 
    } 
}