以非連續(xù)方式存儲(chǔ)元素的線性數(shù)據(jù)結(jié)構(gòu)稱為 LinkedList,其中指針用于將鏈表中的元素相互鏈接,System.Collections.Generic 命名空間由 LinkedList C# 中的類,可以以非??焖俚姆绞綇闹袆h除或插入元素,實(shí)現(xiàn)經(jīng)典鏈表,并且每個(gè)對(duì)象的分配在鏈表中是單獨(dú)的,并且不需要復(fù)制整個(gè)集合來執(zhí)行某些操作在鏈接列表上。
語法:
C#中LinkedList類的語法如下:
LinkedList<Type> linkedlist_name = new LinkedList <Type>();
其中Type代表鏈表的類型。
C# 中 LinkedList 類的工作
- 鏈表中存在節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)由兩部分組成,即數(shù)據(jù)字段和指向鏈表中下一個(gè)節(jié)點(diǎn)的鏈接。
- 鏈表中每個(gè)節(jié)點(diǎn)的類型都是LinkedListNode
;類型。 - 節(jié)點(diǎn)可以從鏈表中刪除,也可以插入回同一個(gè)鏈表,也可以插入到另一個(gè)鏈表,因此堆上沒有額外的分配。
- 向鏈表中插入元素、從鏈表中刪除元素以及獲取點(diǎn)贊列表維護(hù)的內(nèi)部屬性count屬性都是O(1)操作。
- 鏈表類支持枚舉器,因?yàn)樗峭ㄓ面湵怼?/li>
- 鏈表不支持任何使鏈表不一致的東西。
- 如果鏈表是雙向鏈表,那么每個(gè)節(jié)點(diǎn)都有兩個(gè)指針,一個(gè)指向鏈表中的前一個(gè)節(jié)點(diǎn),另一個(gè)指向鏈表中的下一個(gè)節(jié)點(diǎn)。
LinkedList 類的構(gòu)造函數(shù)
C#中的LinkedList類中有幾個(gè)構(gòu)造函數(shù)。他們是:
- LinkedList(): 鏈表類的新實(shí)例被初始化,該實(shí)例為空。
- LinkedList(IEnumerable):初始化鏈表類的新實(shí)例,該實(shí)例取自 IEnumerable 的指定實(shí)現(xiàn),其容量足以累積所有復(fù)制的元素。
- LinkedList(SerializationInfo, StreamingContext): 鏈表類的新實(shí)例被初始化,可以使用指定為參數(shù)的serializationInfo和StreamingContext進(jìn)行序列化。
C#中LinkedList類的方法
C#中的LinkedList類中有幾個(gè)方法。他們是:
- AddAfter: A value or new node is added after an already present node in the linked list using the AddAfter method.
- AddFirst: A value or new node is added at the beginning of the linked list using the AddFirst method.
- AddBefore: A value or new node is added before an already present node in the linked list using the AddBefore method.
- AddLast: A value or new node is added at the end of the linked list using the AddLast method.
- Remove(LinkedListNode): A node specified as a parameter will be removed from the linked list using Remove(LinkedListNode) method.
- RemoveFirst(): A node at the beginning of the linked list will be removed from the linked list using RemoveFirst() method.
- Remove(T): The first occurrence of the value specified as a parameter in the linked list will be removed from the linked list using the Remove(T) method.
- RemoveLast(): A node at the end of the linked list will be removed from the linked list using the RemoveLast() method.
- Clear(): All the nodes from the linked list will be removed using the Clear() method.
- Find(T): The value specified as the parameter present in the very first node will be identified by using the Find(T) method.
- Contains(T): We can use the Contains(T) method to find out if a value is present in the linked list or not.
- ToString(): A string representing the current object is returned by using the ToString() method.
- CopyTo(T[], Int32): The whole linked list is copied to an array which is one dimensional and is compatible with the linked list and the linked list begins at the index specified in the array to be copied to using CopyTo(T[], Int32) method.
- OnDeserialization(Object): After the completion of deserialization, an event of deserialization is raised and the ISerializable interface is implemented using OnDeserialization(Object) method.
- Equals(Object): If the object specified as the parameter is equal to the current object or not is identified using Equals(Object) method.
- FindLast(T): The value specified as the parameter present in the last node will be identified by using FindLast(T) method.
- MemberwiseClone(): A shallow copy of the current object is created using MemeberwiseClone() method.
- GetEnumerator(): An enumerator is returned using GetEnumerator() method and the returned enumerator loops through the linked list.
- GetType(): The type of the current instance is returned using GetType() method.
- GetHashCode(): The GetHashCode() method is the hash function by default.
- GetObjectData(SerializationInfo, StreamingContext): The data which is necessary to make the linked list serializable is returned by using GetObjectData(SerializationInfo, StreamingContext) method along with implementing the ISerializable interface.
Example of LinkedList Class in C#
C# program to demonstrate AddLast() method, Remove(LinkedListNode) method, Remove(T) method, RemoveFirst() method, RemoveLast() method and Clear() method in Linked List class:
Code:
using System; using System.Collections.Generic; //a class called program is defined public class program { // Main Method is called static public void Main() { //a new linked list is created LinkedList<String> list = new LinkedList<String>(); //AddLast() method is used to add the elements to the newly created linked list list.AddLast("Karnataka"); list.AddLast("Mumbai"); list.AddLast("Pune"); list.AddLast("Hyderabad"); list.AddLast("Chennai"); list.AddLast("Delhi"); Console.WriteLine("The states in India are:"); //Using foreach loop to display the elements of the newly created linked list foreach(string places in list) { Console.WriteLine(places); } Console.WriteLine("The places after using Remove(LinkedListNode) method are:"); //using Remove(LinkedListNode) method to remove a node from the linked list list.Remove(list.First); foreach(string place in list) { Console.WriteLine(place); } Console.WriteLine("The places after using Remove(T) method are:"); //using Remove(T) method to remove a node from the linked list list.Remove("Chennai"); foreach(string plac in list) { Console.WriteLine(plac); } Console.WriteLine("The places after using RemoveFirst() method are:"); //using RemoveFirst() method to remove the first node from the linked list list.RemoveFirst(); foreach(string pla in list) { Console.WriteLine(pla); } Console.WriteLine("The places after using RemoveLast() method are:"); //using RemoveLast() method to remove the last node from the linked list list.RemoveLast(); foreach(string pl in list) { Console.WriteLine(pl); } //using Clear() method to remove all the nodes from the linked list list.Clear(); Console.WriteLine("The count of places after using Clear() method is: {0}", list.Count); } }
The output of the above program is as shown in the snapshot below:
In the above program, a class called program is defined. Then the main method is called. Then a new linked list is created. Then AddLast() method is used to add the elements to the newly created linked list. Then foreach loop is used to display the elements of the newly created linked list. Then Remove(LinkedListNode) method is used to remove a node from the linked list. Then Remove(T) method is used to remove a node from the linked list. Then RemoveFirst() method is used to remove the first node from the linked list. Then RemoveLast() method is used to remove the last node from the linked list. Then Clear() method is used to remove all the nodes from the linked list. The output of the program is shown in the snapshot above.
以上是C# 鏈表的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)

C# 隨機(jī)數(shù)生成器指南。在這里,我們討論隨機(jī)數(shù)生成器的工作原理、偽隨機(jī)數(shù)和安全數(shù)的概念。

多線程和異步的區(qū)別在于,多線程同時(shí)執(zhí)行多個(gè)線程,而異步在不阻塞當(dāng)前線程的情況下執(zhí)行操作。多線程用于計(jì)算密集型任務(wù),而異步用于用戶交互操作。多線程的優(yōu)勢(shì)是提高計(jì)算性能,異步的優(yōu)勢(shì)是不阻塞 UI 線程。選擇多線程還是異步取決于任務(wù)性質(zhì):計(jì)算密集型任務(wù)使用多線程,與外部資源交互且需要保持 UI 響應(yīng)的任務(wù)使用異步。

C#和C 的歷史與演變各有特色,未來前景也不同。1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向?qū)ο缶幊桃隒語言,其演變歷程包括多次標(biāo)準(zhǔn)化,如C 11引入auto關(guān)鍵字和lambda表達(dá)式,C 20引入概念和協(xié)程,未來將專注于性能和系統(tǒng)級(jí)編程。2.C#由微軟在2000年發(fā)布,結(jié)合C 和Java的優(yōu)點(diǎn),其演變注重簡(jiǎn)潔性和生產(chǎn)力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注于開發(fā)者的生產(chǎn)力和云計(jì)算。

C# 素?cái)?shù)指南。這里我們討論c#中素?cái)?shù)的介紹和示例以及代碼實(shí)現(xiàn)。

可以采用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進(jìn)行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進(jìn)行自動(dòng)格式化;使用 XML 轉(zhuǎn)換工具(如 XSLT)定義轉(zhuǎn)換規(guī)則;或者使用編程語言(如 Python)進(jìn)行解析和操作。修改時(shí)需謹(jǐn)慎,并備份原始文件。

將 XML 轉(zhuǎn)換為 JSON 的方法包括:使用編程語言(如 Python、Java、C#)編寫腳本或程序進(jìn)行轉(zhuǎn)換;使用在線工具(如 XML 轉(zhuǎn)換為 JSON、Gojko's XML 轉(zhuǎn)換器、XML 在線工具)粘貼或上傳 XML 數(shù)據(jù)并選擇 JSON 格式輸出;使用 XML 到 JSON 轉(zhuǎn)換器(如 Oxygen XML Editor、Stylus Studio、Altova XMLSpy)執(zhí)行轉(zhuǎn)換任務(wù);使用 XSLT 樣式表將 XML 轉(zhuǎn)換為 JSON;使用數(shù)據(jù)集成工具(如 Informatic

C# 多線程編程是一種讓程序同時(shí)執(zhí)行多項(xiàng)任務(wù)的技術(shù),它可以通過提升性能、提高響應(yīng)能力和實(shí)現(xiàn)并行處理來提高程序效率。雖然 Thread 類提供了直接創(chuàng)建線程的方法,但 Task 和 async/await 等高級(jí)工具可以提供更安全的異步操作和更簡(jiǎn)潔的代碼結(jié)構(gòu)。多線程編程中常見的難題包括死鎖、競(jìng)態(tài)條件和資源泄漏,需要仔細(xì)設(shè)計(jì)線程模型和使用適當(dāng)?shù)耐綑C(jī)制來避免這些問題。

有三種將 XML 轉(zhuǎn)換為 Word 的方法:使用 Microsoft Word、使用 XML 轉(zhuǎn)換器或使用編程語言。
