C# 基础知识系列- 3 集合数组 (5)

这两个类是一对的,一个是泛型类,一个是非泛型类。该类中文名称是队列,如其名,队列讲究一个先进先出,所以队列每次取元素都是从头取,存放是放到队列尾。
操作代码如下:

加入队列

Queue queue = new Queue(); queue.Enqueue(1); queue.Enqueue("2"); Queue<string> queue1 = new Queue<string>(); queue1.Enqueue("stri");//

读取队首的元素
读取有两种:

读取但不移除元素:

object obj= queue.Peek(); string str = queue.Peek();

读取并移除元素:

object obj = queue.Dequeue(); string str = queue.Dequeue();

Count 获取元素数量

3.2 LinkedList<T>

LinkedList,链表。与List不同的地方是,LinkedList的元素是LinkedListNode对象,该对象有四个属性,分别是List
-指向列表对象,Previous指向前一个对象如果有的话,Next指向后一个对象如果有的话。所以根据元素的属性可以发现链表的工作方式,链表就像一条锁链一样,一个元素分三块,一个指向前一个元素,一个用来存放值,一个指向下一个元素,简单如下图所示:

file


所以可以明显的发现LinkedList在随机插取上比一般的要快,因为它不用维护一个数组,但是在查找和坐标操作上明显要慢很多。
LinkedList简单介绍这么多,可以看看它的一些常见操作:

First 第一个元素

获取第一个元素

Last 最后一个元素

获取最后一个元素

AddAfter/AddBefore
在某个节点后/在某个节点前插入数据
支持以下参数列表:

(LinkedListNode node, T value)

(LinkedListNode node, LinkedListNode newNode)

第一个参数表示要插入的节点位置,第二个表示要插入的节点/元素。第一个参数会校验是否属于该链表,如果不属于则会抛出一个异常。第二个可以是值,也可以是初始化好的节点对象。如果是节点对象,则判断是否归属其他链表,如果是其他链表抛出异常。

AddFirst/AddLast

添加元素到头或者尾,可以使用LinkedListNode或者添加值。

Remove

删除,可以传递某个节点,或者要删除的节点里存放的值。

RemoveFirst/RemoveLast
删除第一个节点,删除最后一个节点,不含参数

下面是微软官方的一些示例

using System; using System.Text; using System.Collections.Generic; public class Example { public static void Main() { // Create the link list. string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); Display(sentence, "The linked list values:"); Console.WriteLine("sentence.Contains(\"jumps\") = {0}", sentence.Contains("jumps")); // Add the word 'today' to the beginning of the linked list. sentence.AddFirst("today"); Display(sentence, "Test 1: Add 'today' to beginning of the list:"); // Move the first node to be the last node. LinkedListNode<string> mark1 = sentence.First; sentence.RemoveFirst(); sentence.AddLast(mark1); Display(sentence, "Test 2: Move first node to be last node:"); // Change the last node to 'yesterday'. sentence.RemoveLast(); sentence.AddLast("yesterday"); Display(sentence, "Test 3: Change the last node to 'yesterday':"); // Move the last node to be the first node. mark1 = sentence.Last; sentence.RemoveLast(); sentence.AddFirst(mark1); Display(sentence, "Test 4: Move last node to be first node:"); // Indicate the last occurence of 'the'. sentence.RemoveFirst(); LinkedListNode<string> current = sentence.FindLast("the"); IndicateNode(current, "Test 5: Indicate last occurence of 'the':"); // Add 'lazy' and 'old' after 'the' (the LinkedListNode named current). sentence.AddAfter(current, "old"); sentence.AddAfter(current, "lazy"); IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':"); // Indicate 'fox' node. current = sentence.Find("fox"); IndicateNode(current, "Test 7: Indicate the 'fox' node:"); // Add 'quick' and 'brown' before 'fox': sentence.AddBefore(current, "quick"); sentence.AddBefore(current, "brown"); IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':"); // Keep a reference to the current node, 'fox', // and to the previous node in the list. Indicate the 'dog' node. mark1 = current; LinkedListNode<string> mark2 = current.Previous; current = sentence.Find("dog"); IndicateNode(current, "Test 9: Indicate the 'dog' node:"); // The AddBefore method throws an InvalidOperationException // if you try to add a node that already belongs to a list. Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:"); try { sentence.AddBefore(current, mark1); } catch (InvalidOperationException ex) { Console.WriteLine("Exception message: {0}", ex.Message); } Console.WriteLine(); // Remove the node referred to by mark1, and then add it // before the node referred to by current. // Indicate the node referred to by current. sentence.Remove(mark1); sentence.AddBefore(current, mark1); IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):"); // Remove the node referred to by current. sentence.Remove(current); IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:"); // Add the node after the node referred to by mark2. sentence.AddAfter(mark2, current); IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):"); // The Remove method finds and removes the // first node that that has the specified value. sentence.Remove("old"); Display(sentence, "Test 14: Remove node that has the value 'old':"); // When the linked list is cast to ICollection(Of String), // the Add method adds a node to the end of the list. sentence.RemoveLast(); ICollection<string> icoll = sentence; icoll.Add("rhinoceros"); Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':"); Console.WriteLine("Test 16: Copy the list to an array:"); // Create an array with the same number of // elements as the inked list. string[] sArray = new string[sentence.Count]; sentence.CopyTo(sArray, 0); foreach (string s in sArray) { Console.WriteLine(s); } // Release all the nodes. sentence.Clear(); Console.WriteLine(); Console.WriteLine("Test 17: Clear linked list. Contains 'jumps' = {0}", sentence.Contains("jumps")); Console.ReadLine(); } private static void Display(LinkedList<string> words, string test) { Console.WriteLine(test); foreach (string word in words) { Console.Write(word + " "); } Console.WriteLine(); Console.WriteLine(); } private static void IndicateNode(LinkedListNode<string> node, string test) { Console.WriteLine(test); if (node.List == null) { Console.WriteLine("Node '{0}' is not in the list.\n", node.Value); return; } StringBuilder result = new StringBuilder("(" + node.Value + ")"); LinkedListNode<string> nodeP = node.Previous; while (nodeP != null) { result.Insert(0, nodeP.Value + " "); nodeP = nodeP.Previous; } node = node.Next; while (node != null) { result.Append(" " + node.Value); node = node.Next; } Console.WriteLine(result); Console.WriteLine(); } } //This code example produces the following output: // //The linked list values: //the fox jumps over the dog //Test 1: Add 'today' to beginning of the list: //today the fox jumps over the dog //Test 2: Move first node to be last node: //the fox jumps over the dog today //Test 3: Change the last node to 'yesterday': //the fox jumps over the dog yesterday //Test 4: Move last node to be first node: //yesterday the fox jumps over the dog //Test 5: Indicate last occurence of 'the': //the fox jumps over (the) dog //Test 6: Add 'lazy' and 'old' after 'the': //the fox jumps over (the) lazy old dog //Test 7: Indicate the 'fox' node: //the (fox) jumps over the lazy old dog //Test 8: Add 'quick' and 'brown' before 'fox': //the quick brown (fox) jumps over the lazy old dog //Test 9: Indicate the 'dog' node: //the quick brown fox jumps over the lazy old (dog) //Test 10: Throw exception by adding node (fox) already in the list: //Exception message: The LinkedList node belongs a LinkedList. //Test 11: Move a referenced node (fox) before the current node (dog): //the quick brown jumps over the lazy old fox (dog) //Test 12: Remove current node (dog) and attempt to indicate it: //Node 'dog' is not in the list. //Test 13: Add node removed in test 11 after a referenced node (brown): //the quick brown (dog) jumps over the lazy old fox //Test 14: Remove node that has the value 'old': //the quick brown dog jumps over the lazy fox //Test 15: Remove last node, cast to ICollection, and add 'rhinoceros': //the quick brown dog jumps over the lazy rhinoceros //Test 16: Copy the list to an array: //the //quick //brown //dog //jumps //over //the //lazy //rhinoceros //Test 17: Clear linked list. Contains 'jumps' = False // 3.3 Stack<T> 和 Stack

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgzzyw.html