C# 程式:移除連結串列中開頭的節點


要移除連結串列開頭的節點,請使用 RemoveFirst() 方法。

string [] employees = {"Peter","Robert","John","Jacob"};
LinkedList<string> list = new LinkedList<string>(employees);

現在,要移除第一個元素,請使用 RemoveFirst() 方法。

list.RemoveFirst();

我們來看一個完整的例子。

示例

 即時演示

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      string [] employees = {"Peter","Robert","John","Jacob"};
      LinkedList<string> list = new LinkedList<string>(employees);
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
      // removing first node
      list.RemoveFirst();
      Console.WriteLine("LinkedList after removing first node...");
      foreach (var emp in list) {
         Console.WriteLine(emp);
      }
   }
}

輸出

Peter
Robert
John
Jacob
LinkedList after removing first node...
Robert
John
Jacob

更新於: 2020 年 6 月 23 日

156 次瀏覽

啟動你的 職業

完成課程,獲得證書

開始
廣告