C# 程式在連結串列中查詢節點


首先,建立一個新的連結串列 −

LinkedList<string> myList = new LinkedList<string>();

現在在連結串列中新增一些元素 −

// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");

現在讓我們找到一個節點並在其後新增一個新節點 −

LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");

示例

你可以嘗試執行以下程式碼來查詢連結串列中的一個節點。

線上演示

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      LinkedList<string> myList = new LinkedList<string>();
      // Add 6 elements in the linked list
      myList.AddLast("P");
      myList.AddLast("Q");
      myList.AddLast("R");
      myList.AddLast("S");
      myList.AddLast("T");
      myList.AddLast("U");
      LinkedListNode<string> node = myList.Find("R");
      myList.AddAfter(node, "ADDED");
      foreach (var i in myList) {
         Console.WriteLine(i);
      }
   }
}

輸出

P
Q
R
ADDED
S
T
U

更新於: 19-Jun-2020

475 次瀏覽

開啟您的 職業

透過完成培訓課程獲得認證

開始
廣告
© . All rights reserved.