C# 中 LinkedList RemoveLast() 方法


首先,宣告一個 LinkedList 並新增節點。

int [] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
LinkedList<int> myList = new LinkedList<int>(num);

使用 RemoveLast() 方法從 LinkedList 中移除最後一個節點。

myList.RemoveLast();

示例

 即時演示

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
      LinkedList<int> myList = new LinkedList<int>(num);
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
      // removing last node
      myList.RemoveLast();
      Console.WriteLine("LinkedList after removing the last node...");
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
   }
}

輸出

92
98
110
130
145
170
230
240
250
300
330
LinkedList after removing the last node...
92
98
110
130
145
170
230
240
250
300

更新日期:2020-06-23

330 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始學習
廣告