C# 中的 LinkedList RemoveFirst() 方法


假設我們的 LinkedList 如下,其中整數為節點。

int [] num = {29, 40, 67, 89, 198, 234};
LinkedList<int> myList = new LinkedList<int>(num);

現在,如果你想從列表中刪除第一個元素,那麼使用 RemoveFirst() 方法即可。

myList.RemoveFirst();

示例

 線上演示

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {29, 40, 67, 89, 198, 234};
      LinkedList<int> myList = new LinkedList<int>(num);
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
      // removing first node
      myList.RemoveFirst();
      Console.WriteLine("LinkedList after removing the first node...");
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
   }
}

輸出

29
40
67
89
198
234
LinkedList after removing the first node...
40
67
89
198
234

更新於:2020 年 6 月 23 日

464 個瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始學習
廣告