如何使用 C# 在單鏈表中實現遍歷?
設定帶連結的集合 -
var list = new LinkedList<string>();
現在,新增元素 -
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Four");現在,讓我們在已建立的 LinkedList 中新增新元素 -
LinkedListNode<String> node = list.Find("Four");
list.AddBefore(node, "Three");
list.AddAfter(node, "Five");現在,讓我們看看如何在單鏈表中遍歷節點 -
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var list = new LinkedList < string > ();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Four");
Console.WriteLine("Travering...");
foreach(var res in list) {
Console.WriteLine(res);
}
LinkedListNode < String > node = list.Find("Four");
list.AddBefore(node, "Three");
list.AddAfter(node, "Five");
Console.WriteLine("Travering after adding new elements...");
foreach(var res in list) {
Console.WriteLine(res);
}
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP