請解釋這個任務是關於什麼?C# - MCTS - 創建一個通用鏈接列表類創建一個對象鏈
「創建一個通用鏈接列表類,使我們能夠創建不同類型的鏈對象。」
我們是否需要創建一個類型的鏈表和實現列表接口的類?
class LinkedList<T>:IList<T>
{
//implement interface methods here?
}
請舉例。
TIA
請解釋這個任務是關於什麼?C# - MCTS - 創建一個通用鏈接列表類創建一個對象鏈
「創建一個通用鏈接列表類,使我們能夠創建不同類型的鏈對象。」
我們是否需要創建一個類型的鏈表和實現列表接口的類?
class LinkedList<T>:IList<T>
{
//implement interface methods here?
}
請舉例。
TIA
對於一個鏈表,我通常不會建議實施IList
,因爲IList
強烈暗示到列表中的任何成員,固定時間的訪問。我建議實施ICollection
,然後添加對鏈接列表不可或缺的其他方法,例如PushFront
,PopBack
等。您可以查看LinkedList<T>
類的MSDN文檔以進行比較(http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx),但應該分別實施您的類。
鏈接列表是一個特殊列表,其中列表中的每個元素(或每個元素的容器對象)都有一個直接引用(「鏈接」)到列表中的下一個項目。這種類型的列表不是使用數組實現的。
一個單鏈表通常只有一個鏈接到下一個項目,最後一個項目爲空以指示列表的結束。
一個雙向鏈接列表有一個指向下一個和前一個項目的空白鏈接,用於指示列表的每一端。
鏈接列表的優點是插入和刪除非常快。遍歷整個列表也具有良好的性能,但非線性搜索可能會很慢。
通常,鏈接列表的實現應實現接口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; }
}
是如何形成的?
這應該這樣做
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;
}
}
}
您需要創建通用鏈表您自己的新類。這裏是完整的解決方案。根據上述評論..希望它有助於..
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;
}
}
}
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();
}
}
}
}
它可以是愚蠢的,因爲這個代碼在某種程度上消除了通用的意思,但是我覺得他們的意思是這樣。
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;
}
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();
}
更多的功能與下面的實現 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;
}
}
}
u能闡述「一個沒有,通常情況下,要繼承不同的數據結構的一個現成的榜樣」嗎? – SoftwareGeek 2010-03-10 23:23:17
對不起,將它誤讀爲List。其實,我現在回過頭來看看編輯歷史... – 2010-03-10 23:27:48
請確保您的問題描述是準確的。你的意思是「可以創建列表類的不同實例,其中每個實例可以存放單一類型的元素,但列表的不同實例可以存放不同的元素」,或者「列表類的每個實例可以存放不同類型元素「?換句話說,你想要一個強類型的列表嗎? (我懷疑你想要一個強類型的列表,但在這方面文字措辭不佳) – 2010-05-13 22:09:26