使用 C# 獲取 LinkedList 的最後一個節點


要獲取 LinkedList 的最後一個節點,程式碼如下:

示例

 線上演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("D");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      list.AddLast("J");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = "+list.First.Value);
      Console.WriteLine("Last Node = "+list.Last.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

輸出

這將產生以下輸出:

Count of nodes = 10
First Node = A
Last Node = J
Count of nodes (updated) = 0

示例

我們來看另一個示例:

 線上演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      list.AddLast(400);
      list.AddLast(500);
      list.AddLast(600);
      list.AddLast(700);
      list.AddLast(800);
      list.AddLast(900);
      list.AddLast(1000);
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = "+list.First.Value);
      Console.WriteLine("Last Node = "+list.Last.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

輸出

這將產生以下輸出:

Count of nodes = 10
First Node = 100
Last Node = 1000
Count of nodes (updated) = 0

更新於:06-12-2019

243 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.